2017 © Pedro Peláez
 

library php-composer-reader

Read and manipulate composer.json

image

nadar/php-composer-reader

Read and manipulate composer.json

  • Sunday, March 11, 2018
  • by nadar
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,778 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 28 % Grown

The README.md

Composer JSON Reader and Manipulator

Tests Latest Stable Version Total Downloads License Test Coverage Maintainability, (*1)

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., (*2)

Installation

Install via Composer:, (*3)

composer require nadar/php-composer-reader

Usage

To load the composer.json file into ComposerReader:, (*4)

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:, (*5)

$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:, (*6)

$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):, (*7)

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);, (*8)

Changing Section Data

Add a new PSR autoload definition to an existing composer.json file and save it:, (*9)

$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:, (*10)

$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)., (*11)


The Versions

11/03 2018

dev-master

9999999-dev

Read and manipulate composer.json

  Sources   Download

MIT

The Development Requires

03/01 2018

1.0.0

1.0.0.0

Read and manipulate composer.json

  Sources   Download

MIT

The Development Requires