ContainerBuilder
in package
Helper to create and configure a Container.
With the default options, the container created is appropriate for the development environment.
Example:
$builder = new ContainerBuilder();
$container = $builder->build();
Tags
Table of Contents
Properties
- $sourceCacheNamespace : string
- $compileToDirectory : string|null
- $containerClass : Container>
- Name of the container class, used to create the container.
- $containerParentClass : Container>
- Name of the container parent class, used on compiled container.
- $definitionSources : array<string|int, DefinitionSource>|array<string|int, string>|array<string|int, array<string|int, mixed>>
- $locked : bool
- Whether the container has already been built.
- $proxyDirectory : string|null
- If set, write the proxies to disk in this directory to improve performances.
- $sourceCache : bool
- $useAttributes : bool
- $useAutowiring : bool
- $wrapperContainer : ContainerInterface|null
- If PHP-DI is wrapped in another container, this references the wrapper.
Methods
- __construct() : mixed
- addDefinitions() : $this
- Add definitions to the container.
- build() : Container
- Build and return a container.
- enableCompilation() : self
- Compile the container for optimum performances.
- enableDefinitionCache() : $this
- Enables the use of APCu to cache definitions.
- isCompilationEnabled() : bool
- Are we building a compiled container?
- useAttributes() : $this
- Enable or disable the use of PHP 8 attributes to configure injections.
- useAutowiring() : $this
- Enable or disable the use of autowiring to guess injections.
- wrapContainer() : $this
- If PHP-DI's container is wrapped by another container, we can set this so that PHP-DI will use the wrapper rather than itself for building objects.
- writeProxiesToFile() : $this
- Configure the proxy generation.
- ensureNotLocked() : void
Properties
$sourceCacheNamespace
protected
string
$sourceCacheNamespace
= ''
$compileToDirectory
private
string|null
$compileToDirectory
= null
$containerClass
Name of the container class, used to create the container.
private
Container>
$containerClass
Tags
$containerParentClass
Name of the container parent class, used on compiled container.
private
Container>
$containerParentClass
Tags
$definitionSources
private
array<string|int, DefinitionSource>|array<string|int, string>|array<string|int, array<string|int, mixed>>
$definitionSources
= []
$locked
Whether the container has already been built.
private
bool
$locked
= false
$proxyDirectory
If set, write the proxies to disk in this directory to improve performances.
private
string|null
$proxyDirectory
= null
$sourceCache
private
bool
$sourceCache
= false
$useAttributes
private
bool
$useAttributes
= false
$useAutowiring
private
bool
$useAutowiring
= true
$wrapperContainer
If PHP-DI is wrapped in another container, this references the wrapper.
private
ContainerInterface|null
$wrapperContainer
= null
Methods
__construct()
public
__construct([Container> $containerClass = Container::class ]) : mixed
Parameters
- $containerClass : Container> = Container::class
-
Name of the container class, used to create the container.
Tags
addDefinitions()
Add definitions to the container.
public
addDefinitions(string|array<string|int, mixed>|DefinitionSource ...$definitions) : $this
Parameters
- $definitions : string|array<string|int, mixed>|DefinitionSource
-
Can be an array of definitions, the name of a file containing definitions or a DefinitionSource object.
Return values
$thisbuild()
Build and return a container.
public
build() : Container
Tags
Return values
ContainerenableCompilation()
Compile the container for optimum performances.
public
enableCompilation(string $directory[, string $containerClass = 'CompiledContainer' ][, Container> $containerParentClass = CompiledContainer::class ]) : self
Be aware that the container is compiled once and never updated!
Therefore:
- in production you should clear that directory every time you deploy
- in development you should not compile the container
Parameters
- $directory : string
-
Directory in which to put the compiled container.
- $containerClass : string = 'CompiledContainer'
-
Name of the compiled class. Customize only if necessary.
- $containerParentClass : Container> = CompiledContainer::class
-
Name of the compiled container parent class. Customize only if necessary.
Tags
Return values
selfenableDefinitionCache()
Enables the use of APCu to cache definitions.
public
enableDefinitionCache([string $cacheNamespace = '' ]) : $this
You must have APCu enabled to use it.
Before using this feature, you should try these steps first:
- enable compilation if not already done (see
enableCompilation()) - if you use autowiring or attributes, add all the classes you are using into your configuration so that
PHP-DI knows about them and compiles them
Once this is done, you can try to optimize performances further with APCu. It can also be useful if you use
Container::make()instead ofget()(make()calls cannot be compiled so they are not optimized).
Remember to clear APCu on each deploy else your application will have a stale cache. Do not enable the cache in development environment: any change you will make to the code will be ignored because of the cache.
Parameters
- $cacheNamespace : string = ''
-
use unique namespace per container when sharing a single APC memory pool to prevent cache collisions
Tags
Return values
$thisisCompilationEnabled()
Are we building a compiled container?
public
isCompilationEnabled() : bool
Return values
booluseAttributes()
Enable or disable the use of PHP 8 attributes to configure injections.
public
useAttributes(bool $bool) : $this
Disabled by default.
Parameters
- $bool : bool
Return values
$thisuseAutowiring()
Enable or disable the use of autowiring to guess injections.
public
useAutowiring(bool $bool) : $this
Enabled by default.
Parameters
- $bool : bool
Return values
$thiswrapContainer()
If PHP-DI's container is wrapped by another container, we can set this so that PHP-DI will use the wrapper rather than itself for building objects.
public
wrapContainer(ContainerInterface $otherContainer) : $this
Parameters
- $otherContainer : ContainerInterface
Return values
$thiswriteProxiesToFile()
Configure the proxy generation.
public
writeProxiesToFile(bool $writeToFile[, string|null $proxyDirectory = null ]) : $this
For dev environment, use writeProxiesToFile(false) (default configuration)
For production environment, use writeProxiesToFile(true, 'tmp/proxies')
Parameters
- $writeToFile : bool
-
If true, write the proxies to disk to improve performances
- $proxyDirectory : string|null = null
-
Directory where to write the proxies
Tags
Return values
$thisensureNotLocked()
private
ensureNotLocked() : void