class Container implements ArrayAccess, IteratorAggregate, Countable (View source)

The DI Container.

Constants

FORCE_NEW

Methods

__construct(Container $parent = null, array $children = array())

Constructor for the DI Container

alias(string $alias, string $key)

Create an alias for a given key for easy access.

bind(string $name, mixed $value, bool $shared = false, bool $protected = false)

bind

bindShared(string $name, mixed $value, bool $protected = false)

bindShared

prepareObject(string $class, callable $extend = null, bool $shared = false, bool $protected = false)

prepareObject

prepareSharedObject(string $class, callable $extend = null, bool $protected = false)

prepareSharedObject

object
createObject(string $class, array $args = array(), bool $shared = false, bool $protected = false)

createObject

object
createSharedObject(string $class, array $args = array(), bool $protected = false)

createSharedObject

mixed
newInstance(string $class, array $args = array())

Create an object of class $key;

mixed
execute(callable $callable, array $args = array())

Execute a callable with dependencies.

whenCreating(string $class)

whenCreating

createChild(string $name = null)

Create a child Container with a new property scope that that has the ability to access the parent scope when resolving.

extend(string $key, Closure $callable)

Extend a defined service Closure by wrapping the existing one with a new Closure. This works very similar to a decorator pattern. Note that this only works on service Closures that have been defined in the current Provider, not parent providers.

set(string $key, mixed $value, boolean $shared = false, boolean $protected = false)

Method to set the key and callback to the dataStore array.

protect(string $key, callable $callback, bool $shared = false)

Convenience method for creating protected keys.

share(string $key, callable|mixed $callback, bool $protected = false)

Convenience method for creating shared keys.

mixed
get(string $key, boolean $forceNew = false)

Method to retrieve the results of running the $callback for the specified $key;

remove(string $key)

Remove an item from container.

boolean
exists(string $key)

Method to check if specified dataStore key exists.

mixed
getNewInstance(string $key)

Method to force the container to return a new instance of the results of the callback for requested $key.

mixed
fork(string $key, string $newKey, bool $forceNew = false)

Fork an instance to a new key.

registerServiceProvider(ServiceProviderInterface $provider)

Register a service provider to the container.

getParent()

Method to get property Parent

setParent(Container $parent)

Method to set property parent

addChild(string $name, Container $container)

addChild

getChild(string $name, boolean $forceNew = false)

getChild

boolean
hasChild(string $name)

hasChild

removeChild(string $name)

removeChild

getChildren()

Method to get property Children

setChildren(array $children)

Method to set property children

getIterator()

Retrieve an external iterator

boolean
offsetExists(mixed $offset)

Is a property exists or not.

mixed
offsetGet(mixed $offset)

Get a property.

void
offsetSet(mixed $offset, mixed $value)

Set a value to property.

void
offsetUnset(mixed $offset)

Unset a property.

integer
count()

Count this object.

Details

at line line 71
__construct(Container $parent = null, array $children = array())

Constructor for the DI Container

Parameters

Container $parent Parent for hierarchical containers.
array $children Children Containers.

at line line 87
Container alias(string $alias, string $key)

Create an alias for a given key for easy access.

Parameters

string $alias The alias name
string $key The key to alias

Return Value

Container This object for chaining.

at line line 125
Container bind(string $name, mixed $value, bool $shared = false, bool $protected = false)

bind

Parameters

string $name
mixed $value
bool $shared
bool $protected

Return Value

Container

at line line 149
Container bindShared(string $name, mixed $value, bool $protected = false)

bindShared

Parameters

string $name
mixed $value
bool $protected

Return Value

Container

at line line 166
Container prepareObject(string $class, callable $extend = null, bool $shared = false, bool $protected = false)

prepareObject

Parameters

string $class
callable $extend
bool $shared
bool $protected

Return Value

Container

at line line 194
Container prepareSharedObject(string $class, callable $extend = null, bool $protected = false)

prepareSharedObject

Parameters

string $class
callable $extend
bool $protected

Return Value

Container

at line line 210
object createObject(string $class, array $args = array(), bool $shared = false, bool $protected = false)

createObject

Parameters

string $class
array $args
bool $shared
bool $protected

Return Value

object

at line line 231
object createSharedObject(string $class, array $args = array(), bool $protected = false)

createSharedObject

Parameters

string $class
array $args
bool $protected

Return Value

object

at line line 248
mixed newInstance(string $class, array $args = array())

Create an object of class $key;

Parameters

string $class The class name to build.
array $args The default args if no class hint provided.

Return Value

mixed Instance of class specified by $key with all dependencies injected. Returns an object if the class exists and false otherwise

Exceptions

DependencyResolutionException

at line line 388
mixed execute(callable $callable, array $args = array())

Execute a callable with dependencies.

Parameters

callable $callable
array $args

Return Value

mixed

Exceptions

DependencyResolutionException

at line line 434
ClassMeta whenCreating(string $class)

whenCreating

Parameters

string $class

Return Value

ClassMeta

at line line 454
Container createChild(string $name = null)

Create a child Container with a new property scope that that has the ability to access the parent scope when resolving.

Parameters

string $name The child name.

Return Value

Container The new container object.

at line line 476
Container extend(string $key, Closure $callable)

Extend a defined service Closure by wrapping the existing one with a new Closure. This works very similar to a decorator pattern. Note that this only works on service Closures that have been defined in the current Provider, not parent providers.

Parameters

string $key The unique identifier for the Closure or property.
Closure $callable A Closure to wrap the original service Closure.

Return Value

Container

Exceptions

InvalidArgumentException

at line line 514
Container set(string $key, mixed $value, boolean $shared = false, boolean $protected = false)

Method to set the key and callback to the dataStore array.

Parameters

string $key Name of dataStore key to set.
mixed $value Callable function to run or string to retrieve when requesting the specified $key.
boolean $shared True to create and store a shared instance.
boolean $protected True to protect this item from being overwritten. Useful for services.

Return Value

Container This object for chaining.

Exceptions

OutOfBoundsException Thrown if the provided key is already set and is protected.

at line line 537
Container protect(string $key, callable $callback, bool $shared = false)

Convenience method for creating protected keys.

Parameters

string $key Name of dataStore key to set.
callable $callback Callable function to run when requesting the specified $key.
bool $shared True to create and store a shared instance.

Return Value

Container This object for chaining.

at line line 553
Container share(string $key, callable|mixed $callback, bool $protected = false)

Convenience method for creating shared keys.

Parameters

string $key Name of dataStore key to set.
callable|mixed $callback Callable function to run when requesting the specified $key.
bool $protected True to create and store a shared instance.

Return Value

Container This object for chaining.

at line line 569
mixed get(string $key, boolean $forceNew = false)

Method to retrieve the results of running the $callback for the specified $key;

Parameters

string $key Name of the dataStore key to get.
boolean $forceNew True to force creation and return of a new instance.

Return Value

mixed Results of running the $callback for the specified $key.

Exceptions

UnexpectedValueException

at line line 590
Container remove(string $key)

Remove an item from container.

Parameters

string $key Name of the dataStore key to get.

Return Value

Container This object for chaining.

at line line 611
boolean exists(string $key)

Method to check if specified dataStore key exists.

Parameters

string $key Name of the dataStore key to check.

Return Value

boolean True for success

at line line 651
mixed getNewInstance(string $key)

Method to force the container to return a new instance of the results of the callback for requested $key.

Parameters

string $key Name of the dataStore key to get.

Return Value

mixed Results of running the $callback for the specified $key.

at line line 667
mixed fork(string $key, string $newKey, bool $forceNew = false)

Fork an instance to a new key.

Parameters

string $key Origin key.
string $newKey New key.
bool $forceNew Force new.

Return Value

mixed Forked instance.

at line line 685
Container registerServiceProvider(ServiceProviderInterface $provider)

Register a service provider to the container.

Parameters

ServiceProviderInterface $provider The service provider to register.w

Return Value

Container This object for chaining.

at line line 699
Container getParent()

Method to get property Parent

Return Value

Container Parent container.

at line line 713
Container setParent(Container $parent)

Method to set property parent

Parameters

Container $parent Parent container.

Return Value

Container Return self to support chaining.

at line line 730
Container addChild(string $name, Container $container)

addChild

Parameters

string $name
Container $container

Return Value

Container

at line line 749
Container getChild(string $name, boolean $forceNew = false)

getChild

Parameters

string $name
boolean $forceNew

Return Value

Container

at line line 766
boolean hasChild(string $name)

hasChild

Parameters

string $name

Return Value

boolean

at line line 780
Container removeChild(string $name)

removeChild

Parameters

string $name

Return Value

Container

at line line 797
Container[] getChildren()

Method to get property Children

Return Value

Container[]

at line line 811
Container setChildren(array $children)

Method to set property children

Parameters

array $children

Return Value

Container Return self to support chaining.

at line line 825
Traversable getIterator()

Retrieve an external iterator

Return Value

Traversable An instance of an object implementing Iterator or Traversable

at line line 839
boolean offsetExists(mixed $offset)

Is a property exists or not.

Parameters

mixed $offset Offset key.

Return Value

boolean

at line line 853
mixed offsetGet(mixed $offset)

Get a property.

Parameters

mixed $offset Offset key.

Return Value

mixed The value to return.

at line line 868
void offsetSet(mixed $offset, mixed $value)

Set a value to property.

Parameters

mixed $offset Offset key.
mixed $value The value to set.

Return Value

void

at line line 882
void offsetUnset(mixed $offset)

Unset a property.

Parameters

mixed $offset Offset key to unset.

Return Value

void

at line line 894
integer count()

Count this object.

Return Value

integer