class Container implements ContainerInterface, ArrayAccess (View source)

Dependency Injection Container that supports different types of values.

Constants

TYPE_VALUE

Type for value entries

TYPE_STANDARD

Type for standard entries

TYPE_BLUEPRINT

Type for blueprint entries

TYPE_FACTORY

Type for factory entries

Methods

__construct(ContainerInterface $delegate = null)

Container constructor.

$this
set(array $values)

Sets new standard entries.

$this
setValues(array $values)

Sets new value entries.

$this
setBlueprints(array $blueprints)

Sets new blueprint entries.

$this
setFactories(array $factories)

Sets new factory entries.

$this
setInjections(array $injections)

Adds additional injections for blueprints.

mixed
getPath(string $path, mixed $default = null)

Returns a container entry based on a given dot path.

mixed
get(string $id)

Returns the entry with the given identifier.

bool
has(string $id)

Tells if the container has an entry with the given identifier.

bool
offsetExists(string $offset)

Tells if the container has an entry with the given identifier.

mixed
offsetGet(string $offset)

Returns the value for entry with the given identifier.

offsetSet(string $offset, mixed $value)

Sets the value of the standard entry with the given identifier.

offsetUnset(string $offset)

Removes the entry from the container with the given identifier.

Details

__construct(ContainerInterface $delegate = null)

Container constructor.

Parameters

ContainerInterface $delegate Optional delegate container

$this set(array $values)

Sets new standard entries.

A standard entry may either be a plain PHP value or an invokable. If an invokable is provided, that invokable is called with the container as a parameter and the return value is used as the value for the entry. Note that value returned by the invokable is cached and the same value is returned for further requests for that entry.

Parameters

array $values Array of entry id-value pairs

Return Value

$this Returns self for call chaining

Exceptions

ContainerExceptionInterface If any of the keys already exist

$this setValues(array $values)

Sets new value entries.

A value entry is any PHP value and is simply returned as is when requested, even if the provided value is an invokable.

Parameters

array $values Array of entry id-value pairs

Return Value

$this Returns self for call chaining

Exceptions

ContainerExceptionInterface If any of the keys already exist

$this setBlueprints(array $blueprints)

Sets new blueprint entries.

A blueprint entry is an instruction on how to create a new instance as the value for the entry. The blueprint entry must be an an array that defines how to instantiate a class.

The name of the class may be provided via a key labeled class. If no such key exists, then the entry identifier itself is used as the name of the class.

The constructor arguments for the instance can be provided via a key labeled __construct. However, if neither class nor __construct keys exist in the array, The values of the array are used as the constructor arguments.

Additional methods for the instance may be called by defining additional values to the array. If the key of the value is a string, then that string is used as the name of the method to call and the value, which must be an array, as the arguments for the method. However, for integer keys, the value must be an array that defines the name of the method as the first value and the arguments in the rest of the values.

Please note that argument values MUST be identifiers for container entries.

Parameters

array $blueprints Array of entry id-value pairs

Return Value

$this Returns self for call chaining

Exceptions

ContainerExceptionInterface If any of the keys already exist

$this setFactories(array $factories)

Sets new factory entries.

A factory entry is simply a callable that is always called to determine the value for the entry (in comparison to a standard entry, which is only called once). Factories may be any kind of callables and the callable is always called with the container as the parameter.

Parameters

array $factories Array of entry id-value pairs

Return Value

$this Returns self for call chaining

Exceptions

ContainerExceptionInterface If any of the keys already exist

$this setInjections(array $injections)

Adds additional injections for blueprints.

Injections are additional method calls for blueprints. Each key in the provided array defines either a class or an interface. If any instance is created via a blueprint and it implements or extends any of these keys, the additional method calls for the key are also called for the instance.

The injection method calls are defined the same way as additional method calls in blueprints.

Parameters

array $injections Additional method calls for blueprints

Return Value

$this Returns self for call chaining

Exceptions

ContainerExceptionInterface If any of the injections are already defined

mixed getPath(string $path, mixed $default = null)

Returns a container entry based on a given dot path.

The doth path is a period separated string, which indicates the entry identifier and the keys of further traversed values. The traversed values may be additional containers, arrays or objects with or without array access. For example, assuming the entry is an array, the following two would be equivalent:

  • $container->get('config')['session']['name']
  • $container->getPath('config.session.name')

Note that if a delegate container has been set, the initial lookup is performed on the delegate container.

This method also allows providing a default value that is returned when the provided path cannot be found within the container. If no default value has been provided, an exception will be thrown instead.

Parameters

string $path The identifier path to load
mixed $default Optional default value if not found

Return Value

mixed The value for the path

Exceptions

NotFoundExceptionInterface If no entry exist with the given path

mixed get(string $id)

Returns the entry with the given identifier.

Parameters

string $id The identifier to find

Return Value

mixed The value for the entry

Exceptions

NotFoundExceptionInterface If no entry exists with the given identifier

bool has(string $id)

Tells if the container has an entry with the given identifier.

Parameters

string $id The identifier to find

Return Value

bool True if the entry exists, false if not

bool offsetExists(string $offset)

Tells if the container has an entry with the given identifier.

Parameters

string $offset The entry identifier

Return Value

bool True if the entry exists, false if not

mixed offsetGet(string $offset)

Returns the value for entry with the given identifier.

Parameters

string $offset The entry identifier

Return Value

mixed The value for the entry

Exceptions

NotFoundExceptionInterface If no entry exists with the given identifier

offsetSet(string $offset, mixed $value)

Sets the value of the standard entry with the given identifier.

Parameters

string $offset The entry identifier
mixed $value The value for the standard entry

offsetUnset(string $offset)

Removes the entry from the container with the given identifier.

Parameters

string $offset The entry identifier