PHPackages                             nadar/php-composer-reader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nadar/php-composer-reader

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nadar/php-composer-reader
=========================

Read and manipulate composer.json

2.1.0(1y ago)2378.2k↑11.9%4[1 issues](https://github.com/nadar/php-composer-reader/issues)[1 PRs](https://github.com/nadar/php-composer-reader/pulls)10MITPHPPHP &gt;=8CI passing

Since Jan 3Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/nadar/php-composer-reader)[ Packagist](https://packagist.org/packages/nadar/php-composer-reader)[ RSS](/packages/nadar-php-composer-reader/feed)WikiDiscussions master Synced yesterday

READMEChangelog (7)Dependencies (5)Versions (9)Used By (10)

Composer JSON Reader and Manipulator
====================================

[](#composer-json-reader-and-manipulator)

[![Tests](https://github.com/nadar/php-composer-reader/workflows/Tests/badge.svg)](https://github.com/nadar/php-composer-reader/workflows/Tests/badge.svg)[![Latest Stable Version](https://camo.githubusercontent.com/a6219439b87ebb2779c54f751435c1a32abbc1ba74b7b7a87830f5b8b53eaf9e/68747470733a2f2f706f7365722e707567782e6f72672f6e616461722f7068702d636f6d706f7365722d7265616465722f762f737461626c65)](https://packagist.org/packages/nadar/php-composer-reader)[![Total Downloads](https://camo.githubusercontent.com/311ebdfb4fae3491c1006f1e2be28a61dd41ebe053ad8520513823d5113ac5d1/68747470733a2f2f706f7365722e707567782e6f72672f6e616461722f7068702d636f6d706f7365722d7265616465722f646f776e6c6f616473)](https://packagist.org/packages/nadar/php-composer-reader)[![License](https://camo.githubusercontent.com/9482e2cc69100c4d2f69997140b14252ad439e2a7f3fd88aac5d2490fb677d04/68747470733a2f2f706f7365722e707567782e6f72672f6e616461722f7068702d636f6d706f7365722d7265616465722f6c6963656e7365)](https://packagist.org/packages/nadar/php-composer-reader)[![Test Coverage](https://camo.githubusercontent.com/dafd74bafcb33926f0912b39c74b2ae3fb872e1383a29b6e335252a849b43084/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f33643639356232626135643432393865323866652f746573745f636f766572616765)](https://codeclimate.com/github/nadar/php-composer-reader/test_coverage)[![Maintainability](https://camo.githubusercontent.com/658015b682bddcf9b7f06d93b9d75fcadb3b9d488ea1d3b07090fe5e4cd06904/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f33643639356232626135643432393865323866652f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/nadar/php-composer-reader/maintainability)

A small PHP library for manipulating and reading the **composer.json** file. It allows you to add new sections, check if it's writable/readable, or retrieve information from the composer schema such as description, title, and more.

Installation
------------

[](#installation)

Install via Composer:

```
composer require nadar/php-composer-reader
```

Usage
-----

[](#usage)

To load the composer.json file into ComposerReader:

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

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

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

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

// Dump the full content
var_dump($reader->getContent());
```

### Reading Section Data

[](#reading-section-data)

Retrieve 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 is greater than a given version constraint.
    if ($package->greaterThan('^6.5')) {
        echo "Numerous releases available!";
    }
}
```

Retrieve an array of objects for each PSR definition 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 ([Composer Schema Documentation](https://getcomposer.org/doc/04-schema.md)):

SectionClass`require`RequireSection`require-dev`RequireDevSection`autoload`AutoloadSection`autoload-dev`AutoloadDevSectionAdditional schema information can be retrieved from the ComposerReader object with: `$reader->contentSection('extra', null);`

### Changing Section Data

[](#changing-section-data)

Add a new PSR autoload definition to an existing composer.json file and save it:

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

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

// Store the new autoload object in the autoload section and save
$section = new AutoloadSection($reader);
$section->add($new)->save();
```

Running Commands
----------------

[](#running-commands)

To perform composer operations, use the `runCommand()` method:

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

This attempts to execute the dump-autoload command for the specified composer.json file. **This requires a globally installed Composer command** on your system ([Install Composer globally](https://getcomposer.org/doc/00-intro.md#globally)).

---

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance67

Regular maintenance activity

Popularity40

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 97.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~426 days

Recently: every ~551 days

Total

7

Last Release

547d ago

Major Versions

1.3.0 → 2.0.02023-04-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/86184bf08843ed8fcc4aedb2fdecd8a9e832e47e89a7166cebfda529c176f5ce?d=identicon)[nadar](/maintainers/nadar)

---

Top Contributors

[![nadar](https://avatars.githubusercontent.com/u/3417221?v=4)](https://github.com/nadar "nadar (89 commits)")[![jeroenherczeg](https://avatars.githubusercontent.com/u/759412?v=4)](https://github.com/jeroenherczeg "jeroenherczeg (1 commits)")[![tacman](https://avatars.githubusercontent.com/u/619585?v=4)](https://github.com/tacman "tacman (1 commits)")

---

Tags

composercomposer-libraryhacktoberfestphp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/nadar-php-composer-reader/health.svg)

```
[![Health](https://phpackages.com/badges/nadar-php-composer-reader/health.svg)](https://phpackages.com/packages/nadar-php-composer-reader)
```

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.5k196.2M3.1k](/packages/composer-composer)[friendsofphp/php-cs-fixer

A tool to automatically fix PHP code style

13.5k251.2M25.2k](/packages/friendsofphp-php-cs-fixer)[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[symfony/asset-mapper

Maps directories of assets &amp; makes them available in a public directory with versioned filenames.

1678.8M237](/packages/symfony-asset-mapper)[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
