Commit 9cf83e9c authored by alexpott's avatar alexpott

Issue #2307869 by dawehner, Berdir, RavindraSingh, effulgentsia: Remove Drupal's Container::get()

parent 4a961b98
...@@ -442,6 +442,8 @@ services: ...@@ -442,6 +442,8 @@ services:
class: SplString class: SplString
factory_service: 'app.root.factory' factory_service: 'app.root.factory'
factory_method: 'get' factory_method: 'get'
tags:
- { name: parameter_service }
app.root.factory: app.root.factory:
class: Drupal\Core\AppRootFactory class: Drupal\Core\AppRootFactory
arguments: ['@kernel'] arguments: ['@kernel']
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass; use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteEnhancers; use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteEnhancers;
use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteFilters; use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteFilters;
use Drupal\Core\DependencyInjection\Compiler\DependencySerializationTraitPass;
use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass; use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass; use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass;
use Drupal\Core\DependencyInjection\ServiceProviderInterface; use Drupal\Core\DependencyInjection\ServiceProviderInterface;
...@@ -84,6 +85,8 @@ public function register(ContainerBuilder $container) { ...@@ -84,6 +85,8 @@ public function register(ContainerBuilder $container) {
// Register plugin managers. // Register plugin managers.
$container->addCompilerPass(new PluginManagerPass()); $container->addCompilerPass(new PluginManagerPass());
$container->addCompilerPass(new DependencySerializationTraitPass());
} }
/** /**
......
<?php
/**
* @file
* Contains \Drupal\Core\DependencyInjection\Compiler\DependencySerializationTraitPass.
*/
namespace Drupal\Core\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Sets the _serviceId property on all services.
*
* @see \Drupal\Core\DependencyInjection\DependencySerializationTrait
*/
class DependencySerializationTraitPass implements CompilerPassInterface {
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container) {
foreach ($container->getDefinitions() as $service_id => $definition) {
// Only add the property to services that are public (as private services
// can not be reloaded through Container::get()) and are objects.
if (!$definition->hasTag('parameter_service') && $definition->isPublic()) {
$definition->setProperty('_serviceId', $service_id);
}
}
}
}
...@@ -17,14 +17,13 @@ class Container extends SymfonyContainer { ...@@ -17,14 +17,13 @@ class Container extends SymfonyContainer {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) { public function set($id, $service, $scope = SymfonyContainer::SCOPE_CONTAINER) {
$service = parent::get($id, $invalidBehavior); parent::set($id, $service, $scope);
// Some services are called but do not exist, so the parent returns nothing.
if (is_object($service)) {
$service->_serviceId = $id;
}
return $service; // Ensure that the _serviceId property is set on synthetic services as well.
if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
$this->services[$id]->_serviceId = $id;
}
} }
/** /**
......
...@@ -29,19 +29,6 @@ public function __construct(ParameterBagInterface $parameterBag = NULL) { ...@@ -29,19 +29,6 @@ public function __construct(ParameterBagInterface $parameterBag = NULL) {
parent::__construct($parameterBag); parent::__construct($parameterBag);
} }
/**
* {@inheritdoc}
*/
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) {
$service = parent::get($id, $invalidBehavior);
// Some services are called but do not exist, so the parent returns nothing.
if (is_object($service)) {
$service->_serviceId = $id;
}
return $service;
}
/** /**
* Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set(). * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
* *
...@@ -59,6 +46,11 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER) { ...@@ -59,6 +46,11 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER) {
if ($this->hasDefinition($id) && ($definition = $this->getDefinition($id)) && $definition->isSynchronized()) { if ($this->hasDefinition($id) && ($definition = $this->getDefinition($id)) && $definition->isSynchronized()) {
$this->synchronize($id); $this->synchronize($id);
} }
// Ensure that the _serviceId property is set on synthetic services as well.
if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
$this->services[$id]->_serviceId = $id;
}
} }
/** /**
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass;
require_once __DIR__ . '../../../../../../vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php'; require_once __DIR__ . '../../../../../../vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php';
...@@ -50,7 +51,18 @@ public function testGet() { ...@@ -50,7 +51,18 @@ public function testGet() {
$result = $container->get('bar'); $result = $container->get('bar');
$this->assertTrue($result instanceof \BarClass); $this->assertTrue($result instanceof \BarClass);
$this->assertEquals('bar', $result->_serviceId); }
/**
* Tests the set() method.
*
* @covers ::set
*/
public function testSet() {
$container = new ContainerBuilder();
$class = new BarClass();
$container->set('bar', $class);
$this->assertEquals('bar', $class->_serviceId);
} }
} }
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\Core\DependencyInjection\Container; use Drupal\Core\DependencyInjection\Container;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass;
/** /**
* @coversDefaultClass \Drupal\Core\DependencyInjection\Container * @coversDefaultClass \Drupal\Core\DependencyInjection\Container
...@@ -31,18 +32,23 @@ protected function setUp() { ...@@ -31,18 +32,23 @@ protected function setUp() {
} }
/** /**
* Tests the get method. * Tests serialization.
* *
* @see \Drupal\Core\DependencyInjection\Container::get() * @expectedException \PHPUnit_Framework_Error
*/ */
public function testGet() { public function testSerialize() {
$service = new \stdClass(); serialize($this->container);
$service->key = 'value'; }
$this->container->set('test_service', $service); /**
$result = $this->container->get('test_service'); * Tests the set() method.
$this->assertSame($service, $result); *
$this->assertEquals('test_service', $result->_serviceId); * @covers ::set
*/
public function testSet() {
$class = new BarClass();
$this->container->set('bar', $class);
$this->assertEquals('bar', $class->_serviceId);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment