Configuration
Setting up the container can be a complicated task. In order to make the it easier, we provide a builder.
The builder is initialised via a named constructors, which represent the format of files to be parsed (php or yaml).
You may use ContainerBuilder::delegating() to allow all supported formats to be used.
Once everything is configured, the builder gives you a fully functional container:
<?php
declare(strict_types=1);
namespace Me\MyApplication\Config;
use Lcobucci\DependencyInjection\ContainerBuilder;
// The path to the current file is passed so we can track changes
// to it and refresh the cache (for development mode)
$builder = ContainerBuilder::yaml(__FILE__, __NAMESPACE__);
$container = $builder->getContainer();
// Alternatively, you may use the method `ContainerBuilder#getTestContainer()`,
// so it can be use for tests that need a real container:
$testContainer = $builder->getTestContainer();
Available methods
ContainerBuilder#addPath(): Add a base path to find filesContainerBuilder#addFile(): Adds a container source fileContainerBuilder#addPass(): Adds an instance of a Compiler Pass to be processedContainerBuilder#addDelayedPass(): Adds a reference (class name and constructor arguments) of a Compiler Pass to be processedContainerBuilder#addPackage(): Adds a reference (class name and constructor arguments) of a Package to be processedContainerBuilder#enableDebugging(): Configures the compiler to track file changes and update the cache, optimised for local development purposesContainerBuilder#setDumpDir(): Configures the directory to be used to dump the cache filesContainerBuilder#setParameter(): Configures a dynamic parameterContainerBuilder#setBaseClass(): Modifies which class should be used as base class for the containerContainerBuilder#setProfileName(): Enables the loading of application profile-specific services via thewhen@{profile-name}syntax
Configuration file example
You may use the following configuration file as inspiration for your projects (usually placed in the /config folder):
<?php
declare(strict_types=1);
namespace Me\MyApplication\Config;
use Lcobucci\DependencyInjection\ContainerBuilder;
use function dirname;
use function getenv;
require __DIR__ . '/../vendor/autoload.php';
$builder = ContainerBuilder::yaml(__FILE__, __NAMESPACE__);
$projectRoot = dirname(__DIR__);
if (getenv('APP_PROFILE') !== 'prod') {
$builder->enableDebugging();
}
return $builder->setProfileName(getenv('APP_PROFILE'))
->setDumpDir($projectRoot . '/var/tmp')
->setParameter('app.basedir', $projectRoot)
->addFile(__DIR__ . '/container.yaml')
->getContainer();