The container in Autarky is a combination of a service locator and a
dependency injector. Whenever dealing with classes that have dependencies,
the container should usually be told to resolve an instance of that class
rather than instantiating it yourself.
Methods summary
public
|
#
define( string $class, callable $factory, array $params = array() )
Define a factory for a given class.
Define a factory for a given class.
The factory can be a closure, a string containing the name of a function,
an array of [$object, 'method'] or an array of ['Class', 'method']. If
the latter is used, 'Class' will be resolved out of the container.
Parameters
- $class
- $factory
- $params
- See ContainerInterface::params()
|
public
|
#
instance( string $class, object $instance )
Place an already instantiated object into the container. This will make
it available as a shared instance.
Place an already instantiated object into the container. This will make
it available as a shared instance.
The $class argument should usually be the exact class name of the
instance, except in cases of mocking.
Parameters
|
public
|
#
share( string|array $classOrClasses )
Tell the container that a given class should be a shared instance, i.e.
only constructed once. No matter time how many times that class is
resolved out of the container, it will be the same instance.
Tell the container that a given class should be a shared instance, i.e.
only constructed once. No matter time how many times that class is
resolved out of the container, it will be the same instance.
Parameters
|
public
|
#
internal( string|array $classOrClasses )
Define a class or classes as internal.
Define a class or classes as internal.
Internal classes cannot be resolved directly, but can be resolved as
dependencies to other classes.
Parameters
|
public
|
#
params( string|array $classOrClasses, array $params )
Define a set of constructor arguments for a specific class.
Define a set of constructor arguments for a specific class.
The parameters can be an associative array where the keys are either
class/interface names to map against type-hints of the class' constructor
arguments, or variable names (including the $ prefix).
Parameters
|
public
|
#
alias( string $original, string|array $aliasOrAliases )
Define an alias.
Whenever the container is asked to resolve $alias, in any context,
$original should be used instead. Note that it is not possible to have
multiple levels of aliases (e.g. original is aliased to alias1, alias1
is aliased to alias2).
Parameters
- $original
- $aliasOrAliases
|
public
boolean
|
#
isBound( string $class )
Determine if a class is bound onto the container or not.
Determine if a class is bound onto the container or not.
Returns true if a factory is defined, if the class is defined as shared,
or if an instance is set. Aliases are looked up.
Parameters
Returns
boolean
|
public
mixed
|
#
resolve( string $class, array $params = array() )
Resolve a class from the container. Dependencies of the resolved
object will be resolved recursively.
Resolve a class from the container. Dependencies of the resolved
object will be resolved recursively.
If the object resolved is an instance of ContainerAwareInterface, the
container will call setContainer($this) on it.
Parameters
- $class
- $params
- See ContainerInterface::params()
Returns
mixed
Implementation of
|
public
|
#
resolving( string|array $classOrClasses, callable $callback )
Register a callback for whenever the given class is resolved.
Register a callback for whenever the given class is resolved.
This works for both aliases and original classes.
Parameters
- $classOrClasses
- $callback
|
public
|
#
resolvingAny( callable $callback )
Register a callback for whenever anything is resolved.
Register a callback for whenever anything is resolved.
Parameters
|