Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Composer Reader Laravel Package

nadar/php-composer-reader

Small PHP library to read and manipulate composer.json. Load and validate readability/writability, dump full content, and work with typed section readers (e.g., require, autoload PSR-4) to iterate packages/namespaces, inspect constraints, and add sections.

View on GitHub
Deep Wiki
Context7

Composer JSON Reader and Manipulator

Tests Latest Stable Version Total Downloads License Test Coverage 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

Install via Composer:

composer require nadar/php-composer-reader

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

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):

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

Additional schema information can be retrieved from the ComposerReader object with: $reader->contentSection('extra', null);

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

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).


Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
aimeos/prisma
besmartand-pro/php-quality-config
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views