A service definition acts as a simple description of a service. But it does not hold container relevant informations like the alias name or if the service will be shared or not.
This package comes already with an implementation of the ServiceDefinitionInterface
simply called ServiceDefinition
.
Construct a new service definition with the given classname and optional arguments as array.
public function __construct(string $className, array $arguments = [])
Data type | Variable name | Comment |
---|---|---|
string | $className | The full class name of the desired service. |
array | $arguments | An array of constructor arguments for the service. |
Create a new service definition:
use ClanCats\Container\ServiceDefinition;
$logger = new ServiceDefinition(MyLogger::class);
You can also directly pass the constructor arguments as an array:
$logger = new ServiceDefinition(MyLogger::class, ['@some_dependency', ':some_parameter', 42]);
Keep in mind when passing arguments as an array prefixing a string with @
will be interpreted as a dependency and :
as parameter. This applies everywhere arguments are defined as array. Service Arguments
There is also a static method to construct new service definition instance allowing a more expressive syntax.
Static instance constructor to allow eye candy like:
ServiceDefinition::for('\Acme\SessionService')
->addDependencyArgument('storage')
->addParameterArgument('session_token')
->addRawArgument(600)
Or the shorter way
ServiceDefinition::for('\Acme\SessionService', ['@storage', ':session_token', 600])
public static function for(string $serviceClassName, array $arguments = [])
Data type | Variable name | Comment |
---|---|---|
string | $serviceClassName | The full class name of the desired service. |
array | $arguments | An array of constructor arguments for the service. |
Construct a single service definition object from an array
ServiceDefinition::fromArray([
'class' => '\Acme\Demo',
'arguments' => ['@foo', ':bar'],
'calls' => [
['method' => 'setName', [':demo.name']]
]
])
public static function fromArray(array $serviceConfiguration)
Data type | Variable name | Comment |
---|---|---|
array | $serviceConfiguration |
static
You can pass additional constructor arguments any time:
$QA = new ServiceDefinition(QA::class);
$QA
->addRawArgument('The Answer to the Ultimate Question of Life, The Universe, and Everything.');
->addRawArgument(42)
->addDependencyArgument('database')
->addParameterArgument('priority.default');
Using the arguments
method you can also pass them as an array.
$auth = new ServiceDefinition(MyAuth::class)
->arguments([
'@repository.users',
':auth.secret'
]);
Add a simple raw constructor argument.
public function addRawArgument($argumentValue) : ServiceDefinition
Data type | Variable name | Comment |
---|---|---|
mixed | $argumentValue |
self
$def = ServiceDefinition::for('\Acme\SqlConnection')
->addRawArgument('localhost')
->addRawArgument('root')
->addRawArgument('pleaseDontUseRoot');
Add a dependency constructor argument.
public function addDependencyArgument($argumentValue) : ServiceDefinition
Data type | Variable name | Comment |
---|---|---|
mixed | $argumentValue |
self
$def = ServiceDefinition::for('\Acme\Blog\PostRepository')
->addDependencyArgument('db.connection');
Add a simply parameter constructor argument.
public function addParameterArgument($argumentValue) : ServiceDefinition
Data type | Variable name | Comment |
---|---|---|
mixed | $argumentValue |
self
$def = ServiceDefinition::for('\Acme\Session')
->addParameterArgument('session.secret');
Returns the constructor arguments object
public function getArguments() : ServiceArguments
ServiceArguments
$definition->getArguments(); // ServiceArguments instance
Returns the service class name
public function getClassName() : string
string
A service definition can hold method calls that have to be called on construction, these can set dependencies, parameters or raw values just like the constructor arguments.
Adds a method call to the service definition, the arguments should be set as an array.
public function calls(string $method, array $arguments = []) : ServiceDefinition
Data type | Variable name | Comment |
---|---|---|
string | $method | The name of the method to be called. |
array | $arguments | The method arguments as array. |
self
$def = ServiceDefinition::for('\Acme\Session')
->calls('setEventDispatcher', ['@event_dispatcher']);
Adds a method call to the service definition, the arguments must be set with a ServiceArguments instance.
public function addMethodCall(string $methodName, ServiceArguments $arguments) : ServiceDefinition
Data type | Variable name | Comment |
---|---|---|
string | $methodName | The name of the method to be called. |
ServiceArguments | $arguments | An `ServiceArguments` instance for the method call. |
self
$eventDispatchetArgs = (new ServiceArguments())
->addDependencyArgument('event_dispatcher');
$sessionDefinition = ServiceDefinition::for('\Acme\Session')
->addMethodCall('setEventDispatcher', $eventDispatchetArgs);
Any class that will be used as a service definition must implement the ServiceDefinitionInterface
which requires the following methods:
public function getClassName() : string;
public function getArguments() : ServiceArguments;
public function getMethodCalls() : array;
The format of the returned array should look like [string => ServiceArguments]
.