Menu

## ContainerBuilder
in package

ContainerBuilder.php : 37

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

#### $sourceCacheNamespace

ContainerBuilder.php : 81
`protected string $sourceCacheNamespace = ''`

#### $compileToDirectory

ContainerBuilder.php : 77
`private string|null $compileToDirectory = null`

#### $containerClass

ContainerBuilder.php : 44

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

`private Container> $containerClass`

##### Tags

psalm-var

  class-string<ContainerClass>

#### $containerParentClass

ContainerBuilder.php : 51

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

`private Container> $containerParentClass`

##### Tags

psalm-var

  class-string<ContainerClass>

#### $definitionSources

ContainerBuilder.php : 70
`private array<string|int, DefinitionSource>|array<string|int, string>|array<string|int, array<string|int, mixed>> $definitionSources = []`

#### $locked

ContainerBuilder.php : 75

Whether the container has already been built.

`private bool $locked = false`

#### $proxyDirectory

ContainerBuilder.php : 60

If set, write the proxies to disk in this directory to improve performances.

`private string|null $proxyDirectory = null`

#### $sourceCache

ContainerBuilder.php : 79
`private bool $sourceCache = false`

#### $useAttributes

ContainerBuilder.php : 55
`private bool $useAttributes = false`

#### $useAutowiring

ContainerBuilder.php : 53
`private bool $useAutowiring = true`

#### $wrapperContainer

ContainerBuilder.php : 65

If PHP-DI is wrapped in another container, this references the wrapper.

`private ContainerInterface|null $wrapperContainer = null`

### Methods

#### __construct()

ContainerBuilder.php : 87
`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()

ContainerBuilder.php : 278

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

#### build()

ContainerBuilder.php : 98

Build and return a container.

`public build() : Container`

##### Tags

psalm-return

  ContainerClass

##### Return values

Container

#### enableCompilation()

ContainerBuilder.php : 182

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()

ContainerBuilder.php : 309

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()

ContainerBuilder.php : 322

Are we building a compiled container?

`public isCompilationEnabled() : bool`

##### Return values

bool

#### useAttributes()

ContainerBuilder.php : 219

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()

ContainerBuilder.php : 203

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()

ContainerBuilder.php : 261

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()

ContainerBuilder.php : 241

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

#### ensureNotLocked()

ContainerBuilder.php : 327
`private ensureNotLocked() : void`

## Search results