Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar authored Feb 7, 2024
1 parent 91a35c3 commit 7ae5ca9
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,56 @@
[![Test Coverage](https://api.codeclimate.com/v1/badges/3d695b2ba5d4298e28fe/test_coverage)](https://codeclimate.com/github/nadar/php-composer-reader/test_coverage)
[![Maintainability](https://api.codeclimate.com/v1/badges/3d695b2ba5d4298e28fe/maintainability)](https://codeclimate.com/github/nadar/php-composer-reader/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.
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.

## Install
## Installation

Install via Composer
Install via Composer:

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

## Usage

Load the composer.json file into the ComposerReader:
To load the composer.json file into ComposerReader:

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

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

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

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

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

### Read section data
### Reading Section Data

Get an array of objects for each Package in the require section of the composer.json file:
Retrieve an array of objects for each package in the `require` section of the composer.json file:

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

foreach($section as $package) {
foreach ($section as $package) {
echo $package->name . ' with ' . $package->constraint;

// check if the package version greater then a given version constraint.
// Check if the package version is greater than a given version constraint.
if ($package->greaterThan('^6.5')) {
echo "A lot of releases already!";
echo "Numerous releases available!";
}
}
```

Get an array of objects for each PSR defintion in the autoload section of the composer.json file:
Retrieve an array of objects for each PSR definition in the `autoload` section of the composer.json file:

```php
$reader = new ComposerReader('path/to/composer.json');
Expand All @@ -67,39 +67,41 @@ foreach ($section as $autoload) {
}
```

The following section readers are available for the [composer schema](https://getcomposer.org/doc/04-schema.md):
The following section readers are available for the composer schema ([Composer Schema Documentation](https://getcomposer.org/doc/04-schema.md)):

|Section|Class
|-------|-----
|`require`|RequireSection
|`require-dev`|RequireDevSection
|`autoload`|AutoloadSection
|`autoload-dev`|AutoloadDevSection
| 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);`
Additional schema information can be retrieved from the ComposerReader object with: `$reader->contentSection('extra', null);`

### Change section data
### Changing Section Data

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

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

// generate new autoload section object
// Generate a 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
// Store the new autoload object in the autoload section and save
$section = new AutoloadSection($reader);
$section->add($new)->save();
```

## Run commands
## Running Commands

In order to perform composer operations you can use the `runCommand()` method:
To perform composer operations, use the `runCommand()` method:

```php
$reader = new ComposerReader('path/to/composer.json');
$reader->runCommand('dump-autoload'); // equals to `composer dump-autoload`
$reader->runCommand('dump-autoload'); // This is equivalent to running `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: https://getcomposer.org/doc/00-intro.md#globally)
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)).

---

0 comments on commit 7ae5ca9

Please sign in to comment.