class Container implements ResettableContainerInterface

Container is a dependency injection container.

It gives access to object instances (services). Services and parameters are simple key/pair stores. The container can have four possible behaviors when a service does not exist (or is not initialized for the last case):

  • EXCEPTION_ON_INVALID_REFERENCE: Throws an exception (the default)
  • NULL_ON_INVALID_REFERENCE: Returns null
  • IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference (for instance, ignore a setter if the service does not exist)
  • IGNORE_ON_UNINITIALIZED_REFERENCE: Ignores/returns null for uninitialized services or invalid references

Properties

protected $parameterBag
protected $services
protected $privates
protected $fileMap
protected $methodMap
protected $factories
protected $aliases
protected $loading
protected $resolving
protected $syntheticIds

Methods

__construct(ParameterBagInterface $parameterBag = null)

No description

compile()

Compiles the container.

bool
isCompiled()

Returns true if the container is compiled.

getParameterBag()

Gets the service container parameter bag.

mixed
getParameter(string $name)

Gets a parameter.

bool
hasParameter(string $name)

Checks if a parameter exists.

setParameter(string $name, mixed $value)

Sets a parameter.

set(string $id, object $service)

Sets a service.

bool
has(string $id)

Returns true if the given service is defined.

object
get(string $id, int $invalidBehavior = 1)

Gets a service.

bool
initialized(string $id)

Returns true if the given service has actually been initialized.

reset()

Resets shared services from the container.

array
getServiceIds()

Gets all service ids.

array
getRemovedIds()

Gets service ids that existed at compile time.

static string
camelize(string $id)

Camelizes a string.

static string
underscore(string $id)

A string to underscore.

object
load($file)

Creates a service by requiring its factory file.

mixed
getEnv(string $name)

Fetches a variable from the environment.

getService($registry, $id, $method, $load)

No description

Details

at line 59
__construct(ParameterBagInterface $parameterBag = null)

Parameters

ParameterBagInterface $parameterBag

at line 72
compile()

Compiles the container.

This method does two things:

  • Parameter values are resolved;
  • The parameter bag is frozen.

at line 86
bool isCompiled()

Returns true if the container is compiled.

Return Value

bool

at line 96
ParameterBagInterface getParameterBag()

Gets the service container parameter bag.

Return Value

ParameterBagInterface A ParameterBagInterface instance

at line 110
mixed getParameter(string $name)

Gets a parameter.

Parameters

string $name The parameter name

Return Value

mixed The parameter value

Exceptions

InvalidArgumentException if the parameter is not defined

at line 122
bool hasParameter(string $name)

Checks if a parameter exists.

Parameters

string $name The parameter name

Return Value

bool The presence of parameter in container

at line 133
setParameter(string $name, mixed $value)

Sets a parameter.

Parameters

string $name The parameter name
mixed $value The parameter value

at line 147
set(string $id, object $service)

Sets a service.

Setting a synthetic service to null resets it: has() returns false and get() behaves in the same way as if the service was never created.

Parameters

string $id The service identifier
object $service The service instance

at line 192
bool has(string $id)

Returns true if the given service is defined.

Parameters

string $id The service identifier

Return Value

bool true if the service is defined, false otherwise

at line 221
object get(string $id, int $invalidBehavior = 1)

Gets a service.

Parameters

string $id The service identifier
int $invalidBehavior The behavior when the service does not exist

Return Value

object The associated service

Exceptions

ServiceCircularReferenceException When a circular reference is detected
ServiceNotFoundException When the service is not defined
Exception if an exception has been thrown when the service has been resolved

See also

Reference

at line 288
bool initialized(string $id)

Returns true if the given service has actually been initialized.

Parameters

string $id

Return Value

bool true if the service has been initialized, false otherwise

at line 304
reset()

Resets shared services from the container.

The container is not intended to be used again after being reset in a normal workflow. This method is meant as a way to release references for ref-counting. A subsequent call to ContainerInterface::get will recreate a new instance of the shared service.

at line 325
array getServiceIds()

Gets all service ids.

Return Value

array An array of all defined service ids

at line 335
array getRemovedIds()

Gets service ids that existed at compile time.

Return Value

array

at line 347
static string camelize(string $id)

Camelizes a string.

Parameters

string $id A string to camelize

Return Value

string The camelized string

at line 359
static string underscore(string $id)

A string to underscore.

Parameters

string $id The string to underscore

Return Value

string The underscored string

at line 369
protected object load($file)

Creates a service by requiring its factory file.

Parameters

$file

Return Value

object The service created by the file

at line 383
protected mixed getEnv(string $name)

Fetches a variable from the environment.

Parameters

string $name The name of the environment variable

Return Value

mixed The value to use for the provided environment variable name

Exceptions

EnvNotFoundException When the environment variable is not found and has no default value

at line 421
final protected getService($registry, $id, $method, $load)

Parameters

$registry
$id
$method
$load