Read and manipulate composer.json

2.0.0 2023-04-02 18:34 UTC

This package is auto-updated.

Last update: 2023-06-02 18:49:11 UTC


Tests Latest Stable Version Total Downloads License Test Coverage Maintainability

A small PHP library to manipulated and read the composer.json file. Add new sections, see whether its writeable/readable or just get some informations from the composer schema like description, title and others.


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 the package version greater then a given 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;

The following section readers are available for the composer schema:

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

All the other schema informationscan be retrieved from the ComposerReader object with: $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 for the given composer.json file. this requires a global installed composer command on the system (install composer globally: