ClanCats Logo

Container

ClanCats Container Usage

This document focuses more on the public API of the default container instance.

For more basic usage check:


Parameters

Think of parameters as mostly scalar configuration values that can be injected inside your dependencies.


Construct with parameters

Construct a new container instance with inital parameters.

Method definition:

public function __construct(array $initalParameters = [])

Arguments

Data type Variable name Comment
array $initalParameters Array of inital parameters.

Returns

void


Getting a parameter

Get the parameter with the the given name, or return the default value if the parameter is not set.

Method definition:

public function getParameter(string $name, $default = null)

Arguments

Data type Variable name Comment
string $name The parameter name.
mixed $default The returned default value if the parameter is not set.

Returns

mixed


Setting a parameter

Set the given parameter with value

Method definition:

public function setParameter(string $name, $value)

Arguments

Data type Variable name Comment
string $name The parameter name.
mixed $value The parameter value.

Returns

void


Check if parameter exists

Does the container contain the parameter with the given name?

Method definition:

public function hasParameter(string $name) : bool

Arguments

Data type Variable name Comment
string $name The parameter name.

Returns

bool


Services


Getting a service

Retrieve a service from the container.

Method definition:

public function get(string $serviceName)

Arguments

Data type Variable name Comment
string $serviceName The name / Identifier of the service to look for.

Returns

mixed The requested service.


Available services

Returns an array of all available servic keys.

Method definition:

public function available() : array

Returns

array[string]


Has service

Does the container have the given service?

Method definition:

public function has(string $serviceName) : bool

Arguments

Data type Variable name Comment
string $serviceName The name / Identifier of the service to look for.

Returns

bool


Bind service

Binds a service factory to the container.

$container->bind('session', new SessionFactory);
$container->bind('config', function($c) {
     return new Config($c->get('config.loader'));
}, false);
$container->bind('router', '\\Routing\\Router')
    ->addDependencyArgument('config');

Method definition:

public function bind(string $name, $factory, bool $shared = true)

Arguments

Data type Variable name Comment
string $name The service name.
mixed $factory The service factory instance, the closure or the classname as string
bool $shared Should the service be shared inside the container.

Returns

Closure, ServiceFactoryInterface The given or generated service factory.

Bind service by class

Creates and binds a service factory by class name and arguments.

Method definition:

public function bindClass(string $name, string $factory, array $arguments = [], bool $shared = true) : ServiceFactory

Arguments

Data type Variable name Comment
string $name The service name.
string $className The service class name.
array $arguments An array of arguments.
bool $shared Should the service be shared inside the container.

Returns

ServiceFactory The created service factory

Bind service factory

Binds an unshared factory instance to the service container.

Method definition:

public function bindFactory(string $name, $factory)

Arguments

Data type Variable name Comment
string $name The service name.
ServiceFactoryInterface|Closure $factory The service factory instance or closure.

Returns

void

Bind shared service factory

Binds a shared factory instance to the service container.

Method definition:

public function bindFactoryShared(string $name, $factory)

Arguments

Data type Variable name Comment
string $name The service name.
ServiceFactoryInterface|Closure $factory The service factory instance or closure.

Returns

void


Set service

Sets a value on the container instance. This will overwrite any service stored / shared under the same name.

Method definition:

public function set(string $serviceName, $serviceValue)

Arguments

Data type Variable name Comment
string $serviceName The name / Identifier of the service to look for.
mixed $serviceValue

Returns

void


Remove service

Removes a service from the container and releases the shared instance if it has been loaded.

Method definition:

public function remove(string $serviceName) : bool

Arguments

Data type Variable name Comment
string $serviceName The name / Identifier of the service to look for.

Returns

bool Returns true if the service has been removed.


Release service

Release a shared resolved service from the container. This will force the service to reload when accessed again.

Method definition:

public function release(string $serviceName) : bool

Arguments

Data type Variable name Comment
string $serviceName

Returns

bool Return false on failure.


Is service resolved

Check if the given service has already been resolved / shared / initiated. A factory service will always return false.

Method definition:

public function isResolved(string $serviceName) : bool

Arguments

Data type Variable name Comment
string $serviceName The name / Identifier of the service to look for.

Returns

bool


Register a service provider

Register a service provider. This will call the provides method on the given service provider instance.

Method definition:

public function register(ServiceProviderInterface $provider)

Arguments

Data type Variable name Comment
ServiceProviderInterface $provider The service provider instance.

Returns

void


Get service resolver type

Get the resolver type of the given service name.

Method definition:

public function getServiceResolverType(string $serviceName) : int

Arguments

Data type Variable name Comment
string $serviceName

Returns

int