ClanCats Logo

Container

Service Definitions

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.


Constructor

Construct a new service definition with the given classname and optional arguments as array.

Method definition:

public function __construct(string $className, array $arguments = [])

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


Static factory

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])

Method definition:

public static function for(string $serviceClassName, array $arguments = [])

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 from array

Construct a single service definition object from an array

ServiceDefinition::fromArray([
    'class' => '\Acme\Demo',
    'arguments' => ['@foo', ':bar'],
    'calls' => [
        ['method' => 'setName', [':demo.name']]
    ]
])

Method definition:

public static function fromArray(array $serviceConfiguration)

Arguments

Data type Variable name Comment
array $serviceConfiguration

Returns

static


 Constructor Arguments

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 raw argument

Add a simple raw constructor argument.

Method definition:

public function addRawArgument($argumentValue) : ServiceDefinition

Arguments

Data type Variable name Comment
mixed $argumentValue

Returns

self

$def = ServiceDefinition::for('\Acme\SqlConnection')
    ->addRawArgument('localhost')
    ->addRawArgument('root')
    ->addRawArgument('pleaseDontUseRoot');

Add dependency argument

Add a dependency constructor argument.

Method definition:

public function addDependencyArgument($argumentValue) : ServiceDefinition

Arguments

Data type Variable name Comment
mixed $argumentValue

Returns

self

$def = ServiceDefinition::for('\Acme\Blog\PostRepository')
    ->addDependencyArgument('db.connection');

Add parameter argument

Add a simply parameter constructor argument.

Method definition:

public function addParameterArgument($argumentValue) : ServiceDefinition

Arguments

Data type Variable name Comment
mixed $argumentValue

Returns

self

$def = ServiceDefinition::for('\Acme\Session')
    ->addParameterArgument('session.secret');

Get all arguments

Returns the constructor arguments object

Method definition:

public function getArguments() : ServiceArguments

Returns

ServiceArguments

$definition->getArguments(); // ServiceArguments instance

Get service class name

Returns the service class name

Method definition:

public function getClassName() : string

Returns

string


Method Calls

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.


Add a method call (with array)

Adds a method call to the service definition, the arguments should be set as an array.

Method definition:

public function calls(string $method, array $arguments = []) : ServiceDefinition

Arguments

Data type Variable name Comment
string $method The name of the method to be called.
array $arguments The method arguments as array.

Returns

self

$def = ServiceDefinition::for('\Acme\Session')
    ->calls('setEventDispatcher', ['@event_dispatcher']);

Add a method call

Adds a method call to the service definition, the arguments must be set with a ServiceArguments instance.

Method definition:

public function addMethodCall(string $methodName, ServiceArguments $arguments) : ServiceDefinition

Arguments

Data type Variable name Comment
string $methodName The name of the method to be called.
ServiceArguments $arguments An `ServiceArguments` instance for the method call.

Returns

self

$eventDispatchetArgs = (new ServiceArguments())
    ->addDependencyArgument('event_dispatcher');

$sessionDefinition = ServiceDefinition::for('\Acme\Session')
    ->addMethodCall('setEventDispatcher', $eventDispatchetArgs);

Interface

Any class that will be used as a service definition must implement the ServiceDefinitionInterface which requires the following methods:

Return the service class name

public function getClassName() : string;

Return the constructor arguments object

public function getArguments() : ServiceArguments;

Return the registered method calls

public function getMethodCalls() : array;

The format of the returned array should look like [string => ServiceArguments].