2017 © Pedro Peláez
 

library composer-parser

A composer.json and lockfile parser.

image

mcstreetguy/composer-parser

A composer.json and lockfile parser.

  • Sunday, June 3, 2018
  • by MCStreetguy
  • Repository
  • 1 Watchers
  • 0 Stars
  • 125 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Composer Parser

A dependency-free parser library for composer.json and composer.lock files., (*1)

Installation

``` bash composer require mcstreetguy/composer-parser, (*2)


## Usage ### Parsing I recommend using the provided magic Factory method for parsing: ``` php use MCStreetguy\ComposerParser\Factory as ComposerParser; $composerJson = ComposerParser::parse('/path/to/composer.json'); $lockfile = ComposerParser::parse('/path/to/composer.lock');

Even if this is the easiest way to retrieve an instance, it may be that you for some reason cannot rely on these automations (e.g. if you got a differing filename). In this case you can also call the parse methods directly:, (*3)

``` php $composerJson = ComposerParser::parseComposerJson('/some/file'); $lockfile = ComposerParser::parseLockfile('/another/file');, (*4)


_Please note_ that ComposerParser will not complain about any missing fields, instead it fills all missing values with default ones to ensure integrity. #### Exception Handling During instatiation through the Factory, two exceptions may occur: ``` php try { $composerJson = ComposerParser::parse('/path/to/composer.json'); } catch (InvalidArgumentException $e) { // The given file could not be found or is not readable } catch (RuntimeException $e) { // The given file contained an invalid JSON string }

Doing it the manual way

If you can not rely on the Factory for any reason, you can also instantiate the class directly.
This is however not recommended and may lead to unexpected behaviour., (*5)

``` php use \MCStreetguy\ComposerParse\ComposerJson;, (*6)

$rawData = file_get_contents('/path/to/composer.json'); $parsedData = json_decode($rawData, true);, (*7)

$composerJson = new ComposerJson($parsedData);, (*8)


As you can see the constructor needs an array with the parsed json data. This applies to all constructor methods throughout the library. ##### Sub-components You can also create sub-components directly. In this case you have to keep in mind, that their constructors only accept the isolated data from the composer manifest (e.g. the `Author` class expects you to pass only the contents of one of the objects in the `author`-field). ``` php use \MCStreetguy\ComposerParse\Json\Author; $rawData = file_get_contents('/path/to/composer.json'); $parsedData = json_decode($rawData, true); $author = new Author($parsedData['authors'][0]);

Data Retrieval

All class instances provide getters for their properties. The structure is directly adapted from the composer.json schema. The corresponding property names of wrapper classes have been converted to camelCase (see the PSR-1 standard for further information)., (*9)

For any property you can use the provided getter methods., (*10)

``` php $license = $composerJson->getLicense(); $version = $composerJson->getVersion();, (*11)


It's also possible to directly access properties. _Please note_ that this is read-only! ``` php $description = $composerJson->description; $require = $composerJson->require;

You may also call empty() or isset() on the class properties.
To check if the whole wrapper is empty (useful for nested classes) there is an isEmpty() method inherited:, (*12)

``` php if ($composerJson->config->isEmpty()) { // Do something }, (*13)


#### Special Classes ComposerParser uses some special classes for parts of the json schema. ##### PackageMap The PackageMap class is used for the fields `require`, `require-dev`, `conflict`, `replace`, `provide` and `suggest`. It converts a json structure like this: ``` json { "require": { "vendor/package": "^2.3", "foo/bar": "dev-master" } }

into an array structure like this:, (*14)

``` php $require = [ [ "package" => "vendor/package", "version" => "^2.3" ], [ "package" => "foo/bar", "version" => "dev-master" ] ], (*15)


Additionally it implements the Iterator and ArrayAccess interfaces, thus you may directly access it's contents or put it in a foreach loop: ``` php $require = $composerJson->getRequire(); $fooBar = $require[1]; foreach ($require as $requirement) { $package = $requirement['package']; $version = $requirement['version']; echo "I need $package at version $version!"; }

If you for some reason need the original mapped data you can retrieve it as following:, (*16)

``` php $map = $require->getData();, (*17)


##### NamespaceMap The NamespaceMap is used for the fields `autoload.psr-0` and `autoload.psr-4`. In fact this class is identical to the [PackageMap](#packagemap). The only difference are the map keys: ``` json { "psr-4": { "MCStreetguy\\ComposerParser\\": "src/" } }

php $psr4 = [ [ "namespace" => "MCStreetguy\\ComposerParser\\", "source" => "src/" ] ], (*18)

Global Configuration

By default, the library tries to load your global configuration file, if there is any. It follows the location rules for the composer home directory as defined in the documentation. If there is no readable global configuration file present, this step is silently skipped., (*19)

Just as composer itself, the following precedence rule applies to global configuration files:, (*20)

In case global configuration matches local configuration, the local configuration in the project's composer.json always wins. (source: https://getcomposer.org/doc/03-cli.md#composer-home-config-json), (*21)

You may suppress this behaviour by passing true as second parameter to the parse, parseComposerJson and parseLockfile methods., (*22)

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository., (*23)

Testing

If you contribute to this project, you have to ensure that your changes don't mess up the existing functionality. Therefore this repository comes with a PhpUnit testing configuration, that you can execute by running make test. See their documentation on more information on how to install the tool., (*24)

Authors

See also the list of contributors who participated in this project., (*25)

Acknowledgement

Special thanks go to antonkomarev who helped discovering and fixing several deeply nested bugs in the libraries architecture., (*26)

License

This project is licensed under the MIT License - see the LICENSE file for details, (*27)

The Versions

03/06 2018

dev-master

9999999-dev https://github.com/MCStreetguy/ComposerLockParser

A composer.json and lockfile parser.

  Sources   Download

MIT

The Development Requires

by Maximilian Schmidt