2017 © Pedro Peláez
 

library service-provider-registry

This package provides a service provider registry that can lazily instantiate the service providers it contains.

image

thecodingmachine/service-provider-registry

This package provides a service provider registry that can lazily instantiate the service providers it contains.

  • Wednesday, September 20, 2017
  • by mouf
  • Repository
  • 6 Watchers
  • 1 Stars
  • 1,878 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 7 Versions
  • 1 % Grown

The README.md

Scrutinizer Code Quality Build Status Coverage Status, (*1)

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., (*2)

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., (*3)

How does it work?

Easy, you create a new Registry object, and then, you push objects in it., (*4)

$registry = new Registry();

$key = $registry->push(MyServiceProvider::class);

// This will trigger the creation of the MyServiceProvider object and return it.
$serviceProvider = $registry[$key];

You can also pass parameters to the constructor of the object:, (*5)

$registry = new Registry();

$key = $registry->push(MyServiceProvider::class, "param1", "param2");

And because we are kind, you can also push into the lazy array an already instantiated object:, (*6)

$registry = new Registry();

// This is possible, even if we loose the interest of the Registry.
$key = $registry->push(new MyServiceProvider());

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:, (*7)

$registry = new Registry([
    MyServiceProvider::class, // Is you simply want to create an instance without passing parameters
    [ MyServiceProvider2::class, [ "param1", "param2 ] ],  // Is you simply want to create an instance and pass parameters to the constructor
    new MyServiceProvider4('foo') // If you directly want to push the constructed instance.
]);

Iterating the registry

The registry implements the \Traversable interface, so iterating it is as simple as a foreach:, (*8)

foreach ($registry as $serviceProvider) {
    // Do stuff for each service provider.
    // Service providers will be instantiated on the fly if needed.
}

Discovery with thecodingmachine/discovery

The registry supports a discovery mechanism based on thecodingmachine/discovery (to automatically find and attach service providers to your application)., (*9)

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., (*10)

$registry = new Registry([], TheCodingMachine\Discovery::getInstance());

// The registry now contains all the service providers discoverable by thecodingmachine/discovery.

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., (*11)

$factories = $registry->getFactories(0);

$extensions = $registry->getExtensions(0);

Using the registry to create services

Even better, using the createService method of the registry, you can directly call the service factory:, (*12)

$myService = $registry->createService(0, 'serviceName', $container);

Using the extendService method of the registry, you can directly call the service extension:, (*13)

$myService = $registry->extendService(0, 'serviceName', $container, $previousService);

Why?

This was built for improving the performance of service providers loading (in compiled container environment)., (*14)

The Versions

20/09 2017

3.0.x-dev

3.0.9999999.9999999-dev

This package provides a service provider registry that can lazily instantiate the service providers it contains.

  Sources   Download

MIT

The Requires

 

The Development Requires

17/09 2017

2.0.x-dev

2.0.9999999.9999999-dev

This package provides a service provider registry that can lazily instantiate the service providers it contains.

  Sources   Download

MIT

The Requires

 

The Development Requires

17/09 2017

v2.0.0

2.0.0.0

This package provides a service provider registry that can lazily instantiate the service providers it contains.

  Sources   Download

MIT

The Requires

 

The Development Requires

10/07 2017

1.1.x-dev

1.1.9999999.9999999-dev

This package provides a service provider registry than can lazily instantiate the service providers it contains.

  Sources   Download

MIT

The Requires

 

The Development Requires

10/07 2017

v1.1.0

1.1.0.0

This package provides a service provider registry than can lazily instantiate the service providers it contains.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/12 2016

1.0.x-dev

1.0.9999999.9999999-dev

This package provides a service provider registry than can lazily instantiate the service providers it contains.

  Sources   Download

MIT

The Requires

 

The Development Requires

12/12 2016

v0.3.0

0.3.0.0

This package provides a service provider registry than can lazily instantiate the service providers it contains.

  Sources   Download

MIT

The Requires

 

The Development Requires