ClanCats Logo

Container

Service Arguments

There are currently 3 types of service arguments available.

  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:

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

Constructor

Construct new arguments object with array

Method definition:

public function __construct(array $arguments = [])

Arguments

Data type Variable name Comment
array $arguments

Returns

void

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

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.

$mailerArguments = new ServiceArguments([[email protected]', [email protected]']);

Same goes for parameters just with a : character.

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

But what if I need a string that starts with an @??

The construcotr makes use of the addArgumentsFromArray method. You are still able to define the arguments directly.

$onlyRawArgs = (new ServiceArguments())
    ->addRaw([email protected] is still a string')
    ->addRaw(':nope not a parameter');

Static constructor

Static instance constructor from array for eye candy

ServiceArguments::from([
    [email protected]',
    ':session_token',
    600, // session lifetime
])

Method definition:

public static function from(array $arguments) : ServiceArguments

Arguments

Data type Variable name Comment
array $arguments The arguments as array.


Adding Arguments


Raw

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

Add a simply raw argument,

Method definition:

public function addRaw($argumentValue) : ServiceArguments

Arguments

Data type Variable name Comment
mixed $argumentValue

Returns

self


Dependency

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

Add a simply raw argument,

Method definition:

public function addDependency($argumentValue) : ServiceArguments

Arguments

Data type Variable name Comment
mixed $argumentValue

Returns

self


Parameter

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

Add a simply raw argument,

Method definition:

public function addParameter($argumentValue) : ServiceArguments

Arguments

Data type Variable name Comment
mixed $argumentValue

Returns

self


From array

Add arguments with a simple array

  • @ prefix indicates dependency
  • : prefix indicates parameter

Method definition:

public function addArgumentsFromArray(array $argumentsArray)

Arguments

Data type Variable name Comment
array $argumentsArray

Returns

void


get all

Return all arguments

Method definition:

public function getAll() : array

Returns

array[[string => int]]


Resolving

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

Method definition:

public function resolve(Container $container) : array

Arguments

Data type Variable name Comment
Container $container

Returns

array