Skip to content
A PHP library to read and manipulate the composer.json file.
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.
src add remove function #7 Jun 9, 2019
.gitignore init commit Jan 3, 2018
LICENSE Update May 11, 2019
composer.json fix platform config Jun 8, 2019

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 via Composer

composer require nadar/php-composer-reader


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

Read section data

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;

There are a few built in commands to read certain sections from the composer schema:

Section Class
require RequireSection
require-dev RequireDevSection
autoload AutoloadSection
autoload-dev AutoloadDevSection

All the other schema can be retrieved from the ComposerReader object trough: $reader->contentSection('extra', null);

Change section data

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

$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);

Run commands

In order to perform composer operations you can use the runCommand() method:

$reader = new ComposerReader('path/to/composer.json');
$reader->runCommand('dump-autoload'); // equals to `composer dump-autoload`

This will try to run the dump-autoload command on the specific composer.json file, of course this requires the the composer global binary is installed on the current system (install composer globally:

You can’t perform that action at this time.