Container

Implements ContainerInterface

The Container class.

since

1.0

package

Joomla Framework

Methods

__construct

Constructor for the DI Container

__construct(\Psr\Container\ContainerInterface|null parent = null) : mixed
since

1.0

Arguments

parent

\Psr\Container\ContainerInterface|nullParent for hierarchical containers.

Response

mixed

alias

Create an alias for a given key for easy access.

alias(string alias, string key) : $this
since

1.0

Arguments

alias

stringThe alias name

key

stringThe key to alias

Response

$this

buildObject

Build an object of the requested class

buildObject(string resourceName, bool shared = false) : object|false

Creates an instance of the class specified by $resourceName with all dependencies injected. If the dependencies cannot be completely resolved, a DependencyResolutionException is thrown.

since

1.0

throws

\Joomla\DI\Exception\DependencyResolutionExceptionif the object could not be built (due to missing information)

Arguments

resourceName

stringThe class name to build.

shared

boolTrue to create a shared resource.

Response

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

buildSharedObject

Convenience method for building a shared object.

buildSharedObject(string resourceName) : object|false
since

1.0

Arguments

resourceName

stringThe class name to build.

Response

object|falseInstance of class specified by $resourceName 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 has the ability to access the parent scope when resolving.

createChild() : \Joomla\DI\Container
since

1.0

Response

\Joomla\DI\ContainerA new container with the current as a parent

exists

Method to check if specified dataStore key exists.

exists(string key) : bool
deprecated
since

1.0

deprecated

3.0 Use ContainerInterface::has() instead

Arguments

key

stringName of the dataStore key to check.

Response

boolTrue for success

extend

Extend a defined service Closure by wrapping the existing one with a new callable function.

extend(string resourceName, callable callable) : void

This works very similar to a decorator pattern. Note that this only works on service Closures that have been defined in the current container, not parent containers.

since

1.0

throws

\Joomla\DI\Exception\KeyNotFoundException

Arguments

resourceName

stringThe unique identifier for the Closure or property.

callable

callableA callable to wrap the original service Closure.

get

Retrieve a resource

get(string resourceName) : mixed
since

1.0

throws

\Joomla\DI\Exception\KeyNotFoundException

Arguments

resourceName

stringName of the resource to get.

Response

mixedThe requested resource

getKeys

Retrieve the keys for services assigned to this container.

getKeys() : array
since

1.5.0

Response

array

getMethodArgs

Build an array of method arguments.

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 resources key to get.

Response

mixedResults of running the callback for the specified key.

getResource

Get the raw data assigned to a key.

getResource(string key, bool bail = false) : \Joomla\DI\ContainerResource|null
since

2.0.0

throws

\Joomla\DI\Exception\KeyNotFoundException

Arguments

key

stringThe key for which to get the stored item.

bail

boolThrow an exception, if the key is not found

Response

\Joomla\DI\ContainerResource|nullThe resource if present, or null if instructed to not bail

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

Check if specified resource exists.

has(string resourceName) : bool
since

1.5.0

Arguments

resourceName

stringName of the resource to check.

Response

booltrue if key is defined, false otherwise

hasFlag

Check whether a flag (i.e., one of 'shared' or 'protected') is set

hasFlag(string resourceName, string method, bool default = true) : bool
since

2.0.0

throws

\Joomla\DI\Exception\KeyNotFoundException

Arguments

resourceName

stringName of the resource to check.

method

stringMethod to delegate to

default

boolDefault return value

Response

bool

isProtected

Check whether a resource is protected

isProtected(string resourceName) : bool
since

2.0.0

Arguments

resourceName

stringName of the resource to check.

Response

bool

isShared

Check whether a resource is shared

isShared(string resourceName) : bool
since

2.0.0

Arguments

resourceName

stringName of the resource to check.

Response

bool

protect

Shortcut method for creating protected keys.

protect(string key, mixed value, bool shared = false) : $this
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

boolTrue to create and store a shared instance.

Response

$this

registerServiceProvider

Register a service provider to the container.

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

1.0

Arguments

provider

\Joomla\DI\ServiceProviderInterfaceThe service provider to register.

Response

$this

resolveAlias

Resolve a resource name.

resolveAlias(string resourceName) : string

If the resource name is an alias, the corresponding key is returned. If the resource name is not an alias, the resource name is returned unchanged.

since

1.0

Arguments

resourceName

stringThe key to search for.

Response

string

set

Set a resource to the container. If the value is null the resource is removed.

set(string key, mixed value, bool shared = false, bool protected = false) : $this
since

1.0

throws

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

Arguments

key

stringName of resources key to set.

value

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

shared

boolTrue to create and store a shared instance.

protected

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

Response

$this

share

Shortcut method for creating shared keys.

share(string key, mixed value, bool protected = false) : $this
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

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

Response

$this

tag

Assign a tag to services.

tag(string tag, array keys) : $this
since

1.5.0

Arguments

tag

stringThe tag name

keys

arrayThe service keys to tag

Response

$this

Properties

aliases

Holds the key aliases.

Format: 'alias' => 'key'

since

1.0

Type(s)

array

resources

Holds the resources.

since

2.0.0

Type(s)

\Joomla\DI\ContainerResource[]

parent

Parent for hierarchical containers.

In fact, this can be any PSR-11 compatible container, which gets decorated by this

since

1.0

Type(s)

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

tags

Holds the service tag mapping.

since

1.5.0

Type(s)

array