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:
Raw arguments
Simple scalar data. (strings,numbers,booleans)Dependencies
In other words a reference to another service.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::PARAMETERConstructor
Construct new arguments object with array
Method definition:
final public function __construct(array $arguments = [])Arguments
| Data type | Variable name | Comment |
|---|---|---|
| array<mixed> | $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(['@mailer.smtp', '@queue']);Same goes for parameters just with a : character.
$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:
$onlyRawArgs = (new ServiceArguments())
->addRaw('@this is still a string')
->addRaw(':nope not a parameter');Static constructor
Static instance constructor from array for eye candy
ServiceArguments::from([
'@session.storage.redis',
':session_token',
600, // session lifetime
])Method definition:
public static function from(array $arguments) : ServiceArgumentsArguments
| Data type | Variable name | Comment |
|---|---|---|
| array<mixed> | $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) : ServiceArgumentsArguments
| 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) : ServiceArgumentsArguments
| 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) : ServiceArgumentsArguments
| 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<mixed> | $argumentsArray |
Returns
void
get all
Return all arguments
Method definition:
public function getAll() : arrayArguments
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:
public function resolve(Container $container) : arrayArguments
| Data type | Variable name | Comment |
|---|---|---|
| Container | $container |
Returns
array<int, mixed>
