There are currently 3 types of service arguments available.
Raw arguments
Simple scalar data. (strings
, numbers
, booleans
)
Dependencies
In other words a reference to another service.
These types are also defined as constants in the ServiceArguments
class:
ServiceArguments::RAW
ServiceArguments::DEPENDENCY
ServiceArguments::PARAMETER
Construct new arguments object with array
public function __construct(array $arguments = [])
Data type | Variable name | Comment |
---|---|---|
array | $arguments |
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',
...
]);
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('@this is still a string')
->addRaw(':nope not a parameter');
Static instance constructor from array for eye candy
ServiceArguments::from([
'@session.storage.redis',
':session_token',
600, // session lifetime
])
public static function from(array $arguments) : ServiceArguments
Data type | Variable name | Comment |
---|---|---|
array | $arguments | The arguments as array. |
$args = (new ServiceArguments())
->addRaw('a string')
->addRaw(42);
Add a simply raw argument,
public function addRaw($argumentValue) : ServiceArguments
Data type | Variable name | Comment |
---|---|---|
mixed | $argumentValue |
self
$args = (new ServiceArguments())
->addDependency('logger');
Add a simply raw argument,
public function addDependency($argumentValue) : ServiceArguments
Data type | Variable name | Comment |
---|---|---|
mixed | $argumentValue |
self
$args = (new ServiceArguments())
->addParameter('auth_token');
Add a simply raw argument,
public function addParameter($argumentValue) : ServiceArguments
Data type | Variable name | Comment |
---|---|---|
mixed | $argumentValue |
self
Add arguments with a simple array
public function addArgumentsFromArray(array $argumentsArray)
Data type | Variable name | Comment |
---|---|---|
array | $argumentsArray |
void
Return all arguments
public function getAll() : array
array[[string => int]]
Resolve the current arguments from the given container instance and return them as array
public function resolve(Container $container) : array
Data type | Variable name | Comment |
---|---|---|
Container | $container |
array