Skip to content

Latest commit

 

History

History
107 lines (73 loc) · 2.93 KB

container-configuration.md

File metadata and controls

107 lines (73 loc) · 2.93 KB
layout current_menu
documentation
container-configuration

Configuring the container

Development environment

PHP-DI's container is preconfigured for "plug and play", i.e. development environment. You can start using it simply like so:

$container = ContainerBuilder::buildDevContainer();
// or even simpler
$container = new Container();

By default, PHP-DI will have Autowiring enabled (annotations are disabled by default).

Production environment

In production environment, you will of course favor speed:

$builder = new \DI\ContainerBuilder();
$builder->setDefinitionCache(/* a cache */);
$builder->writeProxiesToFile(true, 'tmp/proxies');

$container = $builder->build();

To choose a cache, read the performances documentation.

Lightweight container

If you want to use PHP-DI's container as a simple container (no autowiring or annotation support), you will want to disable all extra features.

$builder = new \DI\ContainerBuilder();
$builder->useAutowiring(false);
$builder->useAnnotations(false);

$container = $builder->build();

Note that this doesn't necessarily means that the container will be faster, since everything can be cached anyway. Read more about this in the performances documentation.

Using PHP-DI with other containers

If you want to use several containers at once, for example to use PHP-DI in ZF2 or Symfony 2, you can use a tool like Acclimate.

You will just need to tell PHP-DI to look into the composite container, else PHP-DI will be unaware of Symfony's container entries.

Example with Acclimate:

$container = new CompositeContainer();

// Add Symfony's container
$container->addContainer($acclimate->adaptContainer($symfonyContainer));

// Configure PHP-DI container
$builder = new ContainerBuilder();
$builder->wrapContainer($container);

// Add PHP-DI container
$phpdiContainer = $builder->build();
$container->addContainer($acclimate->adaptContainer($phpdiContainer));

// Good to go!
$foo = $container->get('foo');

Ignoring phpDoc errors

Added in v4.4

If you use annotations and your phpDoc is not always correct, you can set up the container to silently ignore those errors:

$builder->ignorePhpDocErrors(true);

For example:

class Foo
{
    /**
     * @param NonExistentClass $param
     */
    public function useAutowiring($param)
    {
    }
}

Here, PHP-DI will throw an exception because NonExistentClass doesn't exist: this is a phpDoc error.

There has been reports that PHP-FPM might choke on such errors and report it with a message like this:

Handler for fastcgi-script returned invalid result code 1

In case the errors still occur, make sure your annotations are correct or temporarily disable annotations ($builder->useAnnotations(false)) to prevent fatal errors and try to clean up your configuration form there.