Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
DI\ContainerBuilder Class Reference

Public Member Functions

 __construct (string $containerClass=Container::class)
 
 addDefinitions (string|array|DefinitionSource ... $definitions)
 Add definitions to the container.
 
 build ()
 Build and return a container.
 
 enableCompilation (string $directory, string $containerClass='CompiledContainer', string $containerParentClass=CompiledContainer::class)
 Compile the container for optimum performances.
 
 enableDefinitionCache (string $cacheNamespace='')
 Enables the use of APCu to cache definitions.
 
 isCompilationEnabled ()
 Are we building a compiled container?
 
 useAttributes (bool $bool)
 Enable or disable the use of PHP 8 attributes to configure injections.
 
 useAutowiring (bool $bool)
 Enable or disable the use of autowiring to guess injections.
 
 wrapContainer (ContainerInterface $otherContainer)
 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 (bool $writeToFile, string $proxyDirectory=null)
 Configure the proxy generation.
 

Protected Attributes

string $sourceCacheNamespace = ''
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

DI\ContainerBuilder::__construct ( string $containerClass = Container::class)
Parameters
class-string<Container>$containerClass Name of the container class, used to create the container. @psalm-param class-string<ContainerClass> $containerClass

Member Function Documentation

◆ addDefinitions()

DI\ContainerBuilder::addDefinitions ( string|array|DefinitionSource ... $definitions)

Add definitions to the container.

Parameters
string|array|DefinitionSource...$definitions Can be an array of definitions, the name of a file containing definitions or a DefinitionSource object.
Return values
$this

◆ build()

DI\ContainerBuilder::build ( )

Build and return a container.

Return values
Container@psalm-return ContainerClass

◆ enableCompilation()

DI\ContainerBuilder::enableCompilation ( string $directory,
string $containerClass = 'CompiledContainer',
string $containerParentClass = CompiledContainer::class )

Compile the container for optimum performances.

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
See also
https://php-di.org/doc/performances.html

@psalm-template T of CompiledContainer

Parameters
string$directoryDirectory in which to put the compiled container.
string$containerClassName of the compiled class. Customize only if necessary.
class-string<Container>$containerParentClass Name of the compiled container parent class. Customize only if necessary. @psalm-param class-string<T> $containerParentClass

@psalm-return self<T>

◆ enableDefinitionCache()

DI\ContainerBuilder::enableDefinitionCache ( string $cacheNamespace = '')

Enables the use of APCu to cache definitions.

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.

See also
https://php-di.org/doc/performances.html
Parameters
string$cacheNamespaceuse unique namespace per container when sharing a single APC memory pool to prevent cache collisions
Return values
$this

◆ useAttributes()

DI\ContainerBuilder::useAttributes ( bool $bool)

Enable or disable the use of PHP 8 attributes to configure injections.

Disabled by default.

Return values
$this

◆ useAutowiring()

DI\ContainerBuilder::useAutowiring ( bool $bool)

Enable or disable the use of autowiring to guess injections.

Enabled by default.

Return values
$this

◆ wrapContainer()

DI\ContainerBuilder::wrapContainer ( ContainerInterface $otherContainer)

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.

Return values
$this

◆ writeProxiesToFile()

DI\ContainerBuilder::writeProxiesToFile ( bool $writeToFile,
string $proxyDirectory = null )

Configure the proxy generation.

For dev environment, use writeProxiesToFile(false) (default configuration) For production environment, use ‘writeProxiesToFile(true, 'tmp/proxies’)`

See also
https://php-di.org/doc/lazy-injection.html
Parameters
bool$writeToFileIf true, write the proxies to disk to improve performances
string | null$proxyDirectoryDirectory where to write the proxies
Return values
$this
Exceptions
InvalidArgumentExceptionwhen writeToFile is set to true and the proxy directory is null

The documentation for this class was generated from the following file: