1e08718969e1d1f2203d68fd073e2edb7138161b
[yaffs-website] / web / core / tests / Drupal / KernelTests / TestServiceProvider.php
1 <?php
2
3 namespace Drupal\KernelTests;
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 /**
11  * Provides special routing services for tests.
12  */
13 class TestServiceProvider implements ServiceProviderInterface, ServiceModifierInterface {
14
15   /**
16    * @var \Drupal\simpletest\TestBase
17    */
18   public static $currentTest;
19
20   /**
21    * {@inheritdoc}
22    */
23   public function register(ContainerBuilder $container) {
24     if (static::$currentTest && method_exists(static::$currentTest, 'containerBuild')) {
25       static::$currentTest->containerBuild($container);
26     }
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function alter(ContainerBuilder $container) {
33     if (static::$currentTest instanceof KernelTestBase) {
34       static::addRouteProvider($container);
35     }
36   }
37
38   /**
39    * Add the on demand rebuild route provider service.
40    *
41    * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
42    */
43   public static function addRouteProvider(ContainerBuilder $container) {
44     $route_provider_service_name = 'router.route_provider';
45     // While $container->get() does a recursive resolve, getDefinition() does
46     // not, so do it ourselves.
47     $id = $route_provider_service_name;
48     while ($container->hasAlias($id)) {
49       $id = (string) $container->getAlias($id);
50     }
51     $definition = $container->getDefinition($id);
52     $definition->clearTag('needs_destruction');
53     $container->setDefinition("simpletest.$route_provider_service_name", $definition);
54     $container->setDefinition($id, new Definition(RouteProvider::class));
55   }
56
57 }