Download the PHP package berlioz/service-container without Composer
On this page you can find all versions of the php package berlioz/service-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download berlioz/service-container
More information about berlioz/service-container
Files in berlioz/service-container
Package service-container
Short Description Berlioz Service Container is a PHP library to manage your services with dependencies injection, respecting PSR-11 (Container interface) standard.
License MIT
Homepage https://getberlioz.com
Informations about the package service-container
Berlioz Service Container
Berlioz Service Container is a PHP library to manage your services with dependencies injection, respecting PSR-11 ( Container interface) standard.
For more information, and use of Berlioz Framework, go to website and online documentation : https://getberlioz.com
Installation
Composer
You can install Berlioz Service Container with Composer, it's the recommended installation.
Dependencies
- PHP ^8.0
- Packages:
- psr/container
- psr/simple-cache
Usage
Container
Methods available from PSR-11:
-
get($id)
PSR-11: Finds an entry of the container by its identifier and returns it.
Accept a class name.
-
has($id)
PSR-11: Returns true if the container can return an entry for the given identifier. Returns false otherwise.
Add a service
You can add a service with Container::add()
method.
Or with method Container::addService()
, who accept a Service
object.
Service object
-
Constructor of Service object.
-
Service::setNullable(bool $nullable = true): Service
Service can be null after factory execution (false by default).
-
Service::setShared(bool $shared = true): Service
Share a service, always same instance returned for a shared service.
-
Service::addProvide(string ...$provide): Service
Add provided class/interfaces/alias by service.
-
Service::addArgument(string $name, mixed $value): Service
Add argument to make an instance of service class.
-
Service::addArguments(array $arguments): Service
It's an array of arguments, the key must be the name of argument, and the value of key, must be the argument value.
-
Service::addCall(string $method, array $arguments = []): Service
Method name (and arguments) called just after the construction of object class.
-
Service::addCalls(array $calls = []): void
It's an array of calls, the key must be the name of called method and value an array of arguments.
-
Service::setFactory(string $factory): Service
It's the factory static method used to make object.
Example:
MyProject\Name\Space\MyFactory::service
Instantiator
New instance of a class or object:
Invoke a method:
Invoke a function:
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.
If parameter is an object, the system get this into the container or try to instantiate the class.
The method Container::call()
call the good method of the instantiator according value:
Inflector
The inflector is util if you want to inject some dependencies by methods implemented by an interface.
Service provider
In some case, like performances constraints, you need to add a service provider.
A service provider need to implement \Berlioz\ServiceContainer\Provider\ServiceProviderInterface
interface. An
abstract class \Berlioz\ServiceContainer\Provider\AbstractServiceProvider
can help you.
Example of a service provider:
Add your service provider:
All versions of service-container with dependencies
psr/container Version ^1.0 || ^2.0
psr/simple-cache Version ^1.0 || ^2.0 || ^3.0