dev-develop
dev-develop http://www.berlioz-framework.comBerlioz Service Container
MIT
The Requires
- php >=7.1
- psr/container 1.0.0
The Development Requires
by Ronan Giron
Wallogit.com
2017 © Pedro Peláez
Berlioz Service Container
Berlioz Service Container is a PHP library to manage your services with dependencies injection, respecting PSR-11 ( Container interface) standard., (*2)
For more information, and use of Berlioz Framework, go to website and online documentation : https://getberlioz.com, (*3)
You can install Berlioz Service Container with Composer, it's the recommended installation., (*4)
$ composer require berlioz/service-container
Methods available from PSR-11:, (*5)
get($id), (*6)
PSR-11: Finds an entry of the container by its identifier and returns it., (*7)
Accept a class name., (*8)
has($id), (*9)
PSR-11: Returns true if the container can return an entry for the given identifier. Returns false otherwise., (*10)
You can add a service with Container::add() method., (*11)
use Berlioz\ServiceContainer\Container; $container = new Container(); $service = $container->add(MyService::class, 'alias'); // Returns a Berlioz\ServiceContainer\Service\Service object
Or with method Container::addService(), who accept a Service object., (*12)
use Berlioz\ServiceContainer\Container; use Berlioz\ServiceContainer\Service\Service; $container = new Container(); $service = new Service(MyService::class, 'alias'); $container->addService($service);
Service::public function __construct( string|object $class, ?string $alias = null, callable|string|null $factory = null, ?CacheStrategy $cacheStrategy = null, )
Constructor of Service object., (*13)
Service::setNullable(bool $nullable = true): Service, (*14)
Service can be null after factory execution (false by default)., (*15)
Service::setShared(bool $shared = true): Service, (*16)
Share a service, always same instance returned for a shared service., (*17)
Service::addProvide(string ...$provide): Service, (*18)
Add provided class/interfaces/alias by service., (*19)
Service::addArgument(string $name, mixed $value): Service, (*20)
Add argument to make an instance of service class., (*21)
Service::addArguments(array $arguments): Service, (*22)
It's an array of arguments, the key must be the name of argument, and the value of key, must be the argument value., (*23)
Service::addCall(string $method, array $arguments = []): Service, (*24)
Method name (and arguments) called just after the construction of object class., (*25)
Service::addCalls(array $calls = []): void, (*26)
It's an array of calls, the key must be the name of called method and value an array of arguments., (*27)
Service::setFactory(string $factory): Service, (*28)
It's the factory static method used to make object., (*29)
Example:
MyProject\Name\Space\MyFactory::service, (*30)
New instance of a class or object:, (*31)
use Berlioz\ServiceContainer\Instantiator;
$instantiator = new Instantiator();
$object = $instantiator->newInstanceOf(
MyClass::class,
[
'argument1' => 'Value',
'argument3' => 'Value',
'argument2' => 'Value'
]
);
Invoke a method:, (*32)
use Berlioz\ServiceContainer\Instantiator;
$instantiator = new Instantiator();
$instantiator->invokeMethod(
$myObject,
'myMethodName',
[
'argument1' => 'Value',
'argument3' => 'Value',
'argument2' => 'Value'
]
);
Invoke a function:, (*33)
use Berlioz\ServiceContainer\Instantiator;
$instantiator = new Instantiator();
$instantiator->invokeFunction(
'myFunctionName',
[
'argument1' => 'Value',
'argument3' => 'Value',
'argument2' => 'Value'
]
);
In all examples cases, the last argument is an array of parameters to give to the constructor, method or function. The order of arguments is not important., (*34)
If parameter is an object, the system get this into the container or try to instantiate the class., (*35)
The method Container::call() call the good method of the instantiator according value:, (*36)
use Berlioz\ServiceContainer\Container; $container = new Container(); $container->call(fn() => 'test'); // Call closure $container->call(MyClass::class); // Instantiate the class $container->call(MyClass::method); // Call static method or instantiate the class and call method
The inflector is util if you want to inject some dependencies by methods implemented by an interface., (*37)
use Berlioz\ServiceContainer\Container;
use Berlioz\ServiceContainer\Inflector\Inflector;
$inflector = new Inflector(
MyInterface::class, // Interface implemented by object
'setFoo', // Method to call
[/*...*/] // Arguments
);
$container = new Container();
$container->addInflector($inflector);
In some case, like performances constraints, you need to add a service provider., (*38)
A service provider need to implement \Berlioz\ServiceContainer\Provider\ServiceProviderInterface interface. An
abstract class \Berlioz\ServiceContainer\Provider\AbstractServiceProvider can help you., (*39)
Example of a service provider:, (*40)
use Berlioz\ServiceContainer\Container;
use Berlioz\ServiceContainer\Provider\AbstractServiceProvider;
class MyServiceProvider extends AbstractServiceProvider
{
// Declare services class and alias
protected array $provides = [stdClass::class, 'service'];
public function boot(Container $container) : void
{
// This method is called when provider is added to container.
// Add inflectors here.
}
public function register(Container $container) : void
{
// Add services here
$container->add(stdClass::class, 'service');
}
}
Add your service provider:, (*41)
use Berlioz\ServiceContainer\Container;
$container = new Container();
$container->addProvider(new MyServiceProvider());
$container->has('service'); // Returns TRUE
$container->get('service'); // Returns an `stdClass` instance
Berlioz Service Container
MIT