Download the PHP package thecodingmachine/service-provider-registry without Composer
On this page you can find all versions of the php package thecodingmachine/service-provider-registry. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thecodingmachine/service-provider-registry
More information about thecodingmachine/service-provider-registry
Files in thecodingmachine/service-provider-registry
Package service-provider-registry
Short Description This package provides a service provider registry that can lazily instantiate the service providers it contains.
License MIT
Informations about the package service-provider-registry
What is it?
This project contains a registry that stores service providers. The registry implements \ArrayAccess
and behaves like an array.
However, service providers in this object can be instantiated only when the key in the array is fetched, so you don't have to create the instance right away. This is useful for performance considerations, especially for compiled or cached containers.
This class is meant to be used by compiled/cached containers that want to implement container-interop service providers. It is not meant for the mere mortals.
How does it work?
Easy, you create a new Registry
object, and then, you push objects in it.
You can also pass parameters to the constructor of the object:
And because we are kind, you can also push into the lazy array an already instantiated object:
Finally, if you are performance oriented (and I'm sure you are, otherwise you wouldn't be looking at this package), you can create the whole registry in one call:
Iterating the registry
The registry implements the \Traversable
interface, so iterating it is as simple as a foreach
:
Discovery with thecodingmachine/discovery
The registry supports a discovery mechanism based on thecodingmachine/discovery (to automatically find and attach service providers to your application).
As a second parameter, the Registry
accepts the Discovery
object from thecodingmachine/discovery. Pass this object and thecodingmachine/discovery will be used to fetch service providers from your packages.
Caching of getFactories
and getExtensions
You can use the shortcut Registry::getFactories($key)
or Registry::getExtensions($key)
methods to call the getFactories
and getExtensions
methods on a service provider. The result is cached: 2 successive calls will not call the getFactories
or getExtensions
methods twice.
Using the registry to create services
Even better, using the createService
method of the registry, you can directly call the service factory:
Using the extendService
method of the registry, you can directly call the service extension:
Why?
This was built for improving the performance of service providers loading (in compiled container environment).