diff -u b/core/lib/Drupal/Core/DependencyInjection/UpdateDependenciesTrait.php b/core/lib/Drupal/Core/DependencyInjection/UpdateDependenciesTrait.php --- b/core/lib/Drupal/Core/DependencyInjection/UpdateDependenciesTrait.php +++ b/core/lib/Drupal/Core/DependencyInjection/UpdateDependenciesTrait.php @@ -2,10 +2,11 @@ /** * @file - * Contains \Drupal\Core\DependencyInjection\DependencyReinjectionTrait. + * Contains \Drupal\Core\DependencyInjection\UpdateDependenciesTrait. */ namespace Drupal\Core\DependencyInjection; + use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -18,10 +19,15 @@ trait UpdateDependenciesTrait { /** - * Updates all external dependencies and refetch them from the container. + * Updates an object's external dependencies from the container. + * + * This method depends on \Drupal\Core\DependencyInjection\Container::get() + * adding the _serviceId property to all services. * * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * The container. + * + * @see \Drupal\Core\DependencyInjection\Container */ protected function updateDependencies(ContainerInterface $container) { $vars = get_object_vars($this); @@ -29,6 +35,10 @@ if (is_object($value) && isset($value->_serviceId)) { $this->$key = $container->get($value->_serviceId); } + // Special case the container, which might not have a service ID. + elseif ($value instanceof ContainerInterface) { + $this->$key = $container; + } } } only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/UpdateDependenciesTraitTest.php @@ -0,0 +1,46 @@ +_serviceId = 'test_service'; + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->any()) + ->method('get') + ->with('test_service') + ->willReturn($service); + + $trait = $this->getMockForTrait('Drupal\Core\DependencyInjection\UpdateDependenciesTrait'); + $trait->dependency = clone $service; + $trait->container = clone $container; + $object = new \StdClass(); + $trait->object = $object; + + $update_dependencies_method = new \ReflectionMethod($trait, 'updateDependencies'); + $update_dependencies_method->setAccessible(TRUE); + + // Ensure that the service and container are updated but other objects are + // untouched. + $update_dependencies_method->invoke($trait, $container); + $this->assertSame($service, $trait->dependency); + $this->assertSame($container, $trait->container); + $this->assertSame($object, $trait->object); + } +}