PHPackages                             soundasleep/component-discovery - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. soundasleep/component-discovery

ActiveLibrary

soundasleep/component-discovery
===============================

PHP Component Discovery for Composer

01803PHP

Since Apr 1Pushed 11y ago1 watchersCompare

[ Source](https://github.com/soundasleep/component-discovery)[ Packagist](https://packagist.org/packages/soundasleep/component-discovery)[ RSS](/packages/soundasleep-component-discovery/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

component-discovery
===================

[](#component-discovery)

*component-discovery* is a Composer-enabled PHP script to define *types* of PHP components which can then be loaded automatically, generated from your resolved dependencies.

Instances of components are defined with a key/class instance mapping. For ease of use, components loaded through Composer should define their own autoload mapping in their `composer.json`; however you can still define your own `spl_autoload_register` function.

Configuring
-----------

[](#configuring)

First include `component-discovery` as a requirement in your project `composer.json`, and run `composer update` to install it into your project:

```
{
  "require": {
    "soundasleep/component-discovery": "dev-master"
  }
}
```

Now create a `discovery.json` in your project, to define the types of components to discover:

```
{
  "components": {
    "currencies": "currencies.json",
    "jobs": "jobs.json"
  },
  "src": "vendor/*/*",
  "dest": "inc/components"
}
```

*component-discovery* will look in all the `src` folders for the definition files (e.g. `currencies.json`) to find keys and class mappings. For example, in your `vendor/my/package/currencies.json`:

```
{
  "btc": "\\Currency\\Bitcoin",
  "ltc": "\\Currency\\Litecoin"
}
```

Note that you need to escape out the namespace characters in the class names. You can now call *component-discovery* in your build script or manually.

You can also define your component file as an array, rather than a dictionary, and values of this array will also be used as the keys for each value.

```
[
  "\\Currency\\Bitcoin",
  "\\Currency\\Litecoin"
]
```

Building
--------

[](#building)

Run the generate script, either with your build script or manually, with a given root directory:

```
php -f vendor/soundasleep/component-discovery/generate.php .

```

This will generate various files under the `src` directory, that provide all of the runtime mappings between discovery types, keys and class instances.

For example, this can generate the following include `inc/components/Currencies.php`, based on the components you have loaded with Composer in `vendor/`, listing all of the *Currency* components:

```
