evilfreelancer/composer-json-generator

Small library for generating composer.json file with validation by composer's schema

1.0 2019-09-24 18:25 UTC

This package is auto-updated.

Last update: 2024-10-25 07:30:31 UTC


README

Latest Stable Version Build Status Total Downloads License Code Climate Code Coverage Scrutinizer CQ

Composer JSON generator

Small PHP 7.4 library for generating composer.json file with validation by composer's schema

composer require evilfreelancer/composer-json-generator

Project inspired by spatie/schema-org.

How to use

All available examples is here.

Create new composer.json file in OOP style

require __DIR__ . '/vendor/autoload.php';

use \ComposerJson\Generator;
use \ComposerJson\Schemas\Composer;
use \ComposerJson\Schemas\Author;
use \ComposerJson\Schemas\Psr4;

// Initiate generator
$generator = new Generator();

// Initiate composer object
$composer = new Composer();

/*
 * Set basic parameters of new composer.json file
 */

$composer->name        = 'evilfreelancer/composer-json-generator';
$composer->type        = 'library';
$composer->description = 'Small library for generating composer.json file with validation by composer\'s schema';
$composer->keywords    = ['composer', 'json', 'generator'];
$composer->license     = 'MIT';

/*
 * Autoloader details
 */

// For normal usage
$psr4 = new Psr4();

$psr4->options = [
    "ComposerJson\\" => './src/',
];

$composer->autoload[] = $psr4;

// For tests
$psr4 = new Psr4();

$psr4->options = [
    "ComposerJson\\Tests\\" => './tests/',
];

$composer->autoloadDev[] = $psr4;

/*
 * Authors of project
 */

$author           = new Author();
$author->name     = 'Paul Rock';
$author->email    = 'paul@drteam.rocks';
$author->homepage = 'https://twitter.com/EvilFreelancer';
$author->role     = 'Developer';

$composer->authors[] = $author;

/*
 * Require rules
 */

$composer->require = [
    'php'      => '^7.4',
    'ext-json' => '*'
];

$composer->requireDev = [
    'phpunit/phpunit' => '^8.0',
];

/*
 * Load composer into the generator
 */

$generator->load($composer);

/*
 * Generate result
 */

$array = $generator->toArray();
$json  = $generator->toJson();

var_dump($array);
echo $json . PHP_EOL;

Results is

{
    "name": "evilfreelancer/composer-json-generator",
    "description": "Small library for generating composer.json file with validation by composer's schema",
    "type": "library",
    "keywords": [
        "composer",
        "json",
        "generator"
    ],
    "license": "MIT",
    "authors": [
        {
            "name": "Paul Rock",
            "email": "paul@drteam.rocks",
            "homepage": "https://twitter.com/EvilFreelancer",
            "role": "Developer"
        }
    ],
    "require": {
        "php": "^7.4",
        "ext-json": "*"
    },
    "require-dev": {
        "phpunit/phpunit": "^8.0"
    },
    "autoload": {
        "psr-4": {
            "ComposerJson\\": "./src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "ComposerJson\\Tests\\": "./tests/"
        }
    }
}

Btw, it used in current project.

Read existing composer.json file

require __DIR__ . '/vendor/autoload.php';

use \ComposerJson\Generator;

$generator = new Generator();
$generator->read(__DIR__ . '/composer.json');

$array = $generator->toArray();
$json  = $generator->toJson();

//var_dump($array);
echo $json . PHP_EOL;

Links