Skip to content

Service Arguments

Whenever you define a service programmatically, you need to describe what to feed into its constructor and method calls. Some of those values are plain data, some are other services, and some are configuration parameters — and the container has to tell them apart so it can resolve references before handing them over. ServiceArguments is the small wrapper that carries that information.

You will rarely construct it by hand; the service definition and factory helpers build it for you. But understanding the three argument types explains how the container knows a @ means "another service" and a : means "a parameter."

There are three types of service arguments:

  1. Raw arguments
    Simple scalar data. (strings, numbers, booleans)

  2. Dependencies
    In other words a reference to another service.

  3. Parameters
    A reference to a value from a container parameter.

These types are also defined as constants in the ServiceArguments class:

php
ServiceArguments::RAW
ServiceArguments::DEPENDENCY
ServiceArguments::PARAMETER

Constructor

Construct new arguments object with array

Method definition:

php
final public function __construct(array $arguments = [])

Arguments

Data typeVariable nameComment
array<mixed>$arguments

Returns

void

The constructor takes in an array, to allow an easier / lazier way to define the arguments:

php
use ClanCats\Container\ServiceArguments;

$arguments = new ServiceArguments(['Hello', 'World']);

When you want do define a dependency in the array manner you can simply prefix the dependencies name with an @ char.

php
$mailerArguments = new ServiceArguments(['@mailer.smtp', '@queue']);

Same goes for parameters just with a : character.

php
$smtpArguments = new ServiceArguments([
	':smtp.host',
	':smtp.port',
	...
]);

So what happens if you genuinely need a raw string that starts with an @ or a : — a value that only looks like a reference? The array shorthand would misread it as a dependency or parameter. When that happens, skip the shorthand and add the arguments explicitly instead. The constructor is only a convenience over addArgumentsFromArray; you are always free to define each argument directly and say exactly what you mean:

php
$onlyRawArgs = (new ServiceArguments())
	->addRaw('@this is still a string')
	->addRaw(':nope not a parameter');

Static constructor

Static instance constructor from array for eye candy

php
ServiceArguments::from([
    '@session.storage.redis',
    ':session_token',
    600, // session lifetime
])

Method definition:

php
public static function from(array $arguments) : ServiceArguments

Arguments

Data typeVariable nameComment
array<mixed>$argumentsThe arguments as array.

Adding Arguments

Raw

php
$args = (new ServiceArguments())
	->addRaw('a string')
	->addRaw(42);

Add a simply raw argument,

Method definition:

php
public function addRaw($argumentValue) : ServiceArguments

Arguments

Data typeVariable nameComment
mixed$argumentValue

Returns

self

Dependency

php
$args = (new ServiceArguments())
	->addDependency('logger');

Add a simply raw argument,

Method definition:

php
public function addDependency($argumentValue) : ServiceArguments

Arguments

Data typeVariable nameComment
mixed$argumentValue

Returns

self

Parameter

php
$args = (new ServiceArguments())
	->addParameter('auth_token');

Add a simply raw argument,

Method definition:

php
public function addParameter($argumentValue) : ServiceArguments

Arguments

Data typeVariable nameComment
mixed$argumentValue

Returns

self

From array

Add arguments with a simple array

  • @ prefix indicates dependency
  • : prefix indicates parameter

Method definition:

php
public function addArgumentsFromArray(array $argumentsArray)

Arguments

Data typeVariable nameComment
array<mixed>$argumentsArray

Returns

void

get all

Return all arguments

Method definition:

php
public function getAll() : array

Arguments

This method takes no arguments.

Returns

array<mixed>

Resolving

Resolve the current arguments from the given container instance and return them as array

Method definition:

php
public function resolve(Container $container) : array

Arguments

Data typeVariable nameComment
Container$container

Returns

array<int, mixed>