/php-composer-reader

A PHP library to read and manipulate the composer.json file.

Primary LanguagePHPMIT LicenseMIT

Read and manipulate the composer.json file

Build Status Latest Stable Version Total Downloads License Test Coverage Maintainability

A PHP library to manipulate and read the composer.json files.

Install

Install via Composer

composer require nadar/php-composer-reader

Usage

Load the composer.json file into the ComposerReader:

require 'vendor/autoload';

$reader = new ComposerReader('path/to/composer.json');

if (!$reader->canRead()) {
   throw new Exception("Unable to read json.");
}

if (!$reader->canWrite()) {
   throw new Exception("Unable to write to existing json.");
}

// dump full content
var_dump($reader->getContent());

Get an array of objects for each Package in the require section of the composer.json file:

$reader = new ComposerReader('path/to/composer.json');
$section = new RequireSection($reader);

foreach($section as $package) {
    echo $package->name . ' with ' . $package->constraint;

    // check if package version gerate then a version constraint.
    if ($package->greaterThan('^6.5')) {
        echo "A lot of releases already!";
    }
}

Get an array of objects for each PSR defintion in the autoload section of the composer.json file:

$reader = new ComposerReader('path/to/composer.json');
$section = new AutoloadSection($reader, AutoloadSection::TYPE_PSR4);

foreach ($section as $autoload) {
    echo $autoload->namespace . ' with ' . $autoload->source;
}

Add a new psr autoload definition into an existing composer.json file and save it:

// create reader
$reader = new ComposerReader('path/to/composer.json');

// generate new autoload section object
$new = new Autoload($reader, 'Foo\\Bar\\', 'src/foo/bar', AutoloadSection::TYPE_PSR4);

// store the new autoload object into the autoload section
$section = new AutoloadSection($reader);
$section->add($new)->save();