Change record status: 
Project: 
Introduced in branch: 
8.0.x
Description: 

Drupal has now its own Symfony compatible service container and a PHP array dumper.

in practice this only affects modules / sites that relied on internal behavior of the Symfony container, e.g. the constructor.

Constructing a container

Before:

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

$parameter_bag = new ParameterBag($parameters);
$container = new SymfonyContainer($parameter_bag);
$container->set('foo', $foo_service);

After:

use Drupal\Core\DependencyInjection\Container;

$container_definition = [
  'parameters' => $parameters,
];

$container = new Container($container_definition);
$container->set('foo', $foo_service);

The following case has not changed:

$container = new Container();
$container->set('foo', $foo_service);

Unsupported functions and getDefinedServices()

Also the following functions that are defined on the container, but not part of the interface are not supported:

  • - getParameterBag()
  • - isFrozen()
  • - compile()
  • - getAServiceWithAnIdByCamelCase().

getDefinedServices() - while not on the interface - is explicitly supported in order to not break support for core using this functionality in the future or the contributed Drupal console project (that uses it for container introspection).

Dumping a Container Builder to a PHP array definition (optimized for speed)

To dump a container builder to a service definition the following commands can be used:

use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\Dumper\OptimizedPhpArrayDumper;

$container_builder = new ContainerBuilder();
$container_builder->register('foo', 'stdClass');
$container_builder->compile();

$dumper = new OptimizedPhpArrayDumper($container_builder);
$container_definition = $dumper->getArray();

$container = new Container($container_definition);

Dumping a Container Builder to a PHP array definition (optimized for human-readable)

In addition to this machine readable format, there is also a human-readable / human-writable format, which is useful for debugging the container definition:

use Drupal\Core\DependencyInjection\PhpArrayContainer;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\Dumper\PhpArrayDumper;

$container_builder = new ContainerBuilder();
$container_builder->register('foo', 'stdClass');
$container_builder->compile();

$dumper = new PhpArrayDumper($container_builder);
$container_definition = $dumper->getArray();

$container = new PhpArrayContainer($container_definition);

Note: Drupal\Core\DependencyInjection\Container is only compatible with the format of the OptimizedPhpArrayDumper, while the PhpArrayContainer is only compatible with PhpArrayDumper.

Writing a human-readable container definition yourself

use Drupal\Core\DependencyInjection\PhpArrayContainer;

$container_definition = [
  'aliases' => [
    'foo' => 'bar',
  ],
  'parameters' => [
    'some_parameter' => 'I am a container parameter',
  ],
  'services' => [
    'other_service' => [
      'class' => 'stdClass',
    ],
    'bar' => [
      'class' => 'stdClass',
      'arguments' => ['@other_service', '%some_parameter%', '@?optional_service'],
      'factory' => 'SomeService::create',
      'calls' => [
        [ 'setContainer', ['@service_container']],
      ],
      'properties' => [
        '_serviceId' => 'bar',
      ],
    ],
  ],
];

$container = new PhpArrayContainer($container_definition);

So very very similar to the YAML container definition format, but directly stored in PHP.

Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done