Documentation

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
since
3.2
author

Matthieu Napoli matthieu@mnapoli.fr

psalm-template

ContainerClass of Container

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

$containerClass

Name of the container class, used to create the container.

private Container> $containerClass
Tags
psalm-var

class-string<ContainerClass>

$containerParentClass

Name of the container parent class, used on compiled container.

private Container> $containerParentClass
Tags
psalm-var

class-string<ContainerClass>

$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

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
psalm-param

class-string<ContainerClass> $containerClass

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
$this

enableCompilation()

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
see
https://php-di.org/doc/performances.html
psalm-template

T of CompiledContainer

psalm-param

class-string<T> $containerParentClass

psalm-return

self<T>

Return values
self

enableDefinitionCache()

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 of get() (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
see
https://php-di.org/doc/performances.html
Return values
$this

isCompilationEnabled()

Are we building a compiled container?

public isCompilationEnabled() : bool
Return values
bool

useAttributes()

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
$this

useAutowiring()

Enable or disable the use of autowiring to guess injections.

public useAutowiring(bool $bool) : $this

Enabled by default.

Parameters
$bool : bool
Return values
$this

wrapContainer()

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
$this

writeProxiesToFile()

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
see
https://php-di.org/doc/lazy-injection.html
throws
InvalidArgumentException

when writeToFile is set to true and the proxy directory is null

Return values
$this

        
On this page

Search results