0097803deec4ab85da30c1dc1392d4c582152633
[yaffs-website] / web / core / modules / simpletest / src / TestServiceProvider.php
1 <?php
2
3 namespace Drupal\simpletest;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\DependencyInjection\ServiceModifierInterface;
7 use Drupal\Core\DependencyInjection\ServiceProviderInterface;
8 use Symfony\Component\DependencyInjection\Definition;
9
10 class TestServiceProvider implements ServiceProviderInterface, ServiceModifierInterface {
11
12   /**
13    * @var \Drupal\simpletest\TestBase;
14    */
15   public static $currentTest;
16
17   /**
18    * {@inheritdoc}
19    */
20   public function register(ContainerBuilder $container) {
21     if (static::$currentTest && method_exists(static::$currentTest, 'containerBuild')) {
22       static::$currentTest->containerBuild($container);
23     }
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function alter(ContainerBuilder $container) {
30     if (static::$currentTest instanceof KernelTestBase) {
31       static::addRouteProvider($container);
32     }
33   }
34
35   /**
36    * Add the on demand rebuild route provider service.
37    *
38    * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
39    */
40   public static function addRouteProvider(ContainerBuilder $container) {
41     foreach (['router.route_provider' => 'RouteProvider'] as $original_id => $class) {
42       // While $container->get() does a recursive resolve, getDefinition() does
43       // not, so do it ourselves.
44       // @todo Make the code more readable in
45       //   https://www.drupal.org/node/2911498.
46       for ($id = $original_id; $container->hasAlias($id); $id = (string) $container->getAlias($id)) {
47       }
48       $definition = $container->getDefinition($id);
49       $definition->clearTag('needs_destruction');
50       $container->setDefinition("simpletest.$original_id", $definition);
51       $container->setDefinition($id, new Definition('Drupal\simpletest\\' . $class));
52     }
53   }
54
55 }