Container

Implements \Psr\Container\ContainerInterface

The Container class.

since

1.0

Methods

__construct

Constructor for the DI Container

__construct(\Psr\Container\ContainerInterface $parent = null) 
since

1.0

Arguments

$parent

\Psr\Container\ContainerInterfaceParent for hierarchical containers.

alias

Create an alias for a given key for easy access.

alias(string $alias, string $key) : \Joomla\DI\Container
since

1.0

Arguments

$alias

stringThe alias name

$key

stringThe key to alias

Response

\Joomla\DI\ContainerThis object for chaining.

buildObject

Build an object of class $key;

buildObject(string $key, boolean $shared = false) : object|false
since

1.0

Arguments

$key

stringThe class name to build.

$shared

booleanTrue to create a shared resource.

Response

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

buildSharedObject

Convenience method for building a shared object.

buildSharedObject(string $key) : object|false
since

1.0

Arguments

$key

stringThe class name to build.

Response

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

createChild

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

createChild() : \Joomla\DI\Container
since

1.0

Response

\Joomla\DI\ContainerThis object for chaining.

exists

Method to check if specified dataStore key exists.

exists(string $key) : boolean
deprecated
since

1.0

deprecated

3.0 Use ContainerInterface::has() instead

Arguments

$key

stringName of the dataStore key to check.

Response

booleanTrue for success

extend

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.

extend(string $key, \Closure $callable) : void
since

1.0

throws

\Joomla\DI\Exception\KeyNotFoundException

Arguments

$key

stringThe unique identifier for the Closure or property.

$callable

\ClosureA Closure to wrap the original service Closure.

get

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

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

1.0

throws

\Joomla\DI\Exception\KeyNotFoundException

Arguments

$key

stringName of the dataStore key to get.

$forceNew

booleanTrue to force creation and return of a new instance.

Response

mixedResults of running the $callback for the specified $key.

getKeys

Retrieve the keys for services assigned to this container.

getKeys() : array
since

1.5.0

Response

array

getMethodArgs

Build an array of constructor parameters.

getMethodArgs(\ReflectionMethod $method) : array
since

1.0

throws

\Joomla\DI\Exception\DependencyResolutionException

Arguments

$method

\ReflectionMethodMethod for which to build the argument array.

Response

arrayArray of arguments to pass to the method.

getNewInstance

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

getNewInstance(string $key) : mixed
since

1.0

Arguments

$key

stringName of the dataStore key to get.

Response

mixedResults of running the $callback for the specified $key.

getRaw

Get the raw data assigned to a key.

getRaw(string $key) : mixed
since

1.0

Arguments

$key

stringThe key for which to get the stored item.

Response

mixed

getTagged

Fetch all services registered to the given tag.

getTagged(string $tag) : array
since

1.5.0

Arguments

$tag

stringThe tag name

Response

arrayThe resolved services for the given tag

has

Method to check if specified dataStore key exists.

has(string $key) : boolean
since

1.5.0

Arguments

$key

stringName of the dataStore key to check.

Response

booleanTrue for success

protect

Convenience method for creating protected keys.

protect(string $key, mixed $value, boolean $shared = false) : \Joomla\DI\Container
since

1.0

Arguments

$key

stringName of dataStore key to set.

$value

mixedCallable function to run or string to retrive when requesting the specified $key.

$shared

booleanTrue to create and store a shared instance.

Response

\Joomla\DI\ContainerThis object for chaining.

registerServiceProvider

Register a service provider to the container.

registerServiceProvider(\Joomla\DI\ServiceProviderInterface $provider) : \Joomla\DI\Container
since

1.0

Arguments

$provider

\Joomla\DI\ServiceProviderInterfaceThe service provider to register.

Response

\Joomla\DI\ContainerThis object for chaining.

resolveAlias

Search the aliases property for a matching alias key.

resolveAlias(string $key) : string
since

1.0

Arguments

$key

stringThe key to search for.

Response

string

set

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

set(string $key, mixed $value, boolean $shared = false, boolean $protected = false) : \Joomla\DI\Container
since

1.0

throws

\Joomla\DI\Exception\ProtectedKeyExceptionThrown if the provided key is already set and is protected.

Arguments

$key

stringName of dataStore key to set.

$value

mixedCallable function to run or string to retrive when requesting the specified $key.

$shared

booleanTrue to create and store a shared instance.

$protected

booleanTrue to protect this item from being overwritten. Useful for services.

Response

\Joomla\DI\ContainerThis object for chaining.

share

Convenience method for creating shared keys.

share(string $key, mixed $value, boolean $protected = false) : \Joomla\DI\Container
since

1.0

Arguments

$key

stringName of dataStore key to set.

$value

mixedCallable function to run or string to retrive when requesting the specified $key.

$protected

booleanTrue to protect this item from being overwritten. Useful for services.

Response

\Joomla\DI\ContainerThis object for chaining.

tag

Assign a tag to services.

tag(string $tag, array $keys) : \Joomla\DI\Container
since

1.5.0

Arguments

$tag

stringThe tag name

$keys

arrayThe service keys to tag

Response

\Joomla\DI\ContainerThis object for chaining.

Properties

aliases

Holds the key aliases.

since

1.0

Type(s)

array

instances

Holds the shared instances.

since

1.0

Type(s)

array

dataStore

Holds the keys, their callbacks, and whether or not the item is meant to be a shared resource.

since

1.0

Type(s)

array

parent

Parent for hierarchical containers.

since

1.0

Type(s)

\Joomla\DI\Container|\Psr\Container\ContainerInterface

tags

Holds the service tag mapping.

since

1.5.0

Type(s)

array