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 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 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.
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):
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:
$ 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).
$ 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:
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.