https://github.com/thecodingmachine/definition-discovery
[EXPERIMENTAL] Allows automatic discovery of definition provider objects
https://github.com/thecodingmachine/definition-discovery
Last synced: 3 months ago
JSON representation
[EXPERIMENTAL] Allows automatic discovery of definition provider objects
- Host: GitHub
- URL: https://github.com/thecodingmachine/definition-discovery
- Owner: thecodingmachine
- License: mit
- Created: 2015-11-27T16:43:21.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-30T14:03:24.000Z (over 10 years ago)
- Last Synced: 2025-02-16T12:30:28.602Z (over 1 year ago)
- Language: PHP
- Size: 3.91 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Definitions discovery for *definition-interop*
This package contains an interface and Puli binding-types to automatically discover definition providers.
## Introduction
Modules (aka packages or bundles) are widespread in modern frameworks. Unfortunately each framework has its own convention and tools for writing them. The goal of *container-interop* and more specifically [*definition-interop*](https://github.com/container-interop/definition-interop/) is to help developers write modules that can work in any framework.
In *definition-interop*, definition provider objects (implementing the `DefinitionProviderInterface`) can return sets of
definitions. This package proposes a default interface for factories of definition providers.
This factory is static and can be automatically detected by Puli using class discovery.
The goal is to allow containers to automatically detect and create definition provider instances.
## Installation
```
composer require container-interop/definition-discovery@dev
```
This package adheres to the [SemVer](http://semver.org/) specification and will be fully backward compatible between minor versions.
## Definitions discovery
The goal of this package is to enable a package to automatically publish or discover **definitions**.
Definitions are generated by **definition providers** (implementing the `DefinitionProviderInterface`), so what we
really want to do is to get a list of **definition providers**.
To automatically provide a *definition provider* to your application, we use [Puli's discovery mechanism](http://docs.puli.io/en/latest/discovery/introduction.html).
This package contains a Puli **binding-type** named `container-interop/DefinitionProviderFactories`.
This binding-type should contain fully qualified class names implementing the `DefinitionProviderFactoryInterface` interface.
## Providing definition providers
To provide a definition provider, write a `DefinitionProviderFactory` that will return an instance of your `DefinitionProvider`.
For instance (using [mnapoli/assembly](https://github.com/mnapoli/assembly)):
```php
namespace My\Package;
use Interop\Container\Definition\Factory\DefinitionProviderFactoryInterface;
use Assembly\ArrayDefinitionProvider;
class MyDefinitionProviderFactory implements DefinitionProviderFactoryInterface {
public static function buildDefinitionProvider(Discovery $discovery) {
return new ArrayDefinitionProvider([
'logger' => \Assembly\instance('MyLogger')
->setConstructorArguments('warning')
->addMethodCall('setDebug', true),
]);
}
}
```
Once your class is written, use Puli to bind it to the list of available definition providers:
```sh
$ puli bind "My\\Package\\MyDefinitionProviderFactory" container-interop/DefinitionProviderFactories
```
Note: by convention, you can add a "priority" parameter to the binding. Default priority is 0. Lower priorities
are processed first (and therefore, higher priorities are overloading lower priorities).
```sh
$ puli bind "My\\Package\\MyDefinitionProviderFactory" container-interop/DefinitionProviderFactories --param priority=42
```
## Consuming definition providers
In your code, you can find all classes of the `container-interop/DefinitionProviderFactories` binding-type using:
```php
use Interop\Container\Definition\Factory\DefinitionProviderFactoryInterface;
// $discovery is the Puli Discovery object.
$factories = $discovery->findByType('container-interop/DefinitionProviderFactories');
// TODO: sample code to sort by priority.
```