railt/discovery

Cross-package composer-based configuration loader

1.3.0 2019-01-03 14:34 UTC

README

Railt

Travis CI 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38323239373636633233343161316332663139322f746573745f636f766572616765 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38323239373636633233343161316332663139322f6d61696e7461696e6162696c697479

PHP 7.1+ railt.org Discord Latest Stable Version Total Downloads License MIT

Discovery

Installation

  • Install package using composer.
composer require railt/discovery
  • Add discovering event into your composer.json.
{
    "scripts": {
         "post-autoload-dump": [
             "Railt\\Discovery\\Manifest::discover"
         ]
     }
}

Usage

Discovery provides the ability to implement a cross-package configuration using composer.json.

In order to access the configuration group, you must specify the key name in the extra section:

{
    "extra": {
        "discovery": ["your-key"]
    }
}

Values Export

Any group that is listed inside the {"extra": {"discovery": ...}} section will be available, exported and readable.

{
    "extra": {
        "discovery": ["example-2"],
        "example-1": "value", // This section will be IGNORED
        "example-2": "value" // Only this section will be exported
    }
}

Reading Exported Values

After updating the composer dependencies, an object with the specified configs will be formed. In order to further read this data - you need to use the Discovery class.

{
    "extra": {
        "discovery": ["config"],
        "config": {
            "commands": [
                "ExampleCommand1",
                "ExampleCommand2"
            ]
        }
    }
}
<?php

$discovery = new Railt\Discovery\Discovery(__DIR__ . '/vendor');

$discovery->get('config.commands'); 
// array(2) { "ExampleCommand1", "ExampleCommand2" }

Auto Detect

You can try to create a Discovery instance using automatic logic to determine the paths to the vendor directory.

<?php

$discovery = Railt\Discovery\Discovery::auto();

From ClassLoader

You can create a new Discovery instance from the Composer ClassLoader.

<?php
// Composer ClassLoader
$loader = require __DIR__ . '/vendor/autoload.php';

$discovery = Railt\Discovery\Discovery::fromClassLoader($loader);

From Composer

You can create instances of Discovery from Composer plugins using the appropriate static constructor.

<?php
use Composer\Composer;
use Railt\Discovery\Discovery;

class ComposerPlugin
{
    public function __construct(Composer $composer)
    {
        $discovery = Discovery::fromComposer($composer);
    }
}

Export Removal

In order to exclude any value from the export data - you need to register the necessary paths in the section except:discovery.

Please note that this rule is valid only in the root package composer.json.

{
    "extra": {
        "discovery:except": [
            "example-1",
            "example-2:child-1:a",
            "example-2:test:value-2"
        ],
        "example-1": { // This value should be skipped by rule "example-1"
            "key": "value"
        },
        "example-2": {
            "child-1": {
                "a": 1, // This value should be skipped by rule "example-2:child-1:a"
                "b": 2
            },
            "test": [
                "value-1",
                "value-2" // This value should be skipped by rule "example-2:test:value-2"
            ]
        }
    }
}