summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Test/TestKernel.php
blob: c1f64d1b0383d33197fc91b804147d254272c8d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

namespace Drupal\Core\Test;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Component\DependencyInjection\ReverseContainer;
use Drupal\Core\DrupalKernel;

/**
 * Kernel that is only used by mock front controllers.
 */
class TestKernel extends DrupalKernel {

  /**
   * {@inheritdoc}
   */
  public function __construct($environment, $class_loader, $allow_dumping = TRUE) {
    // Exit if we should be in a test environment but aren't.
    if (!drupal_valid_test_ua()) {
      header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
      exit;
    }

    parent::__construct($environment, $class_loader, $allow_dumping);
  }

  /**
   * Sets a container with a kernel service on the Drupal class.
   *
   * @return \Drupal\Component\DependencyInjection\ContainerInterface
   *   A container with the kernel service set.
   */
  public static function setContainerWithKernel() {
    $container = new ContainerBuilder();
    $kernel = new DrupalKernel('test', NULL);
    // Objects of the same type will have access to each others private and
    // protected members even though they are not the same instances. This is
    // because the implementation specific details are already known when
    // inside those objects.
    $kernel->container = $container;
    $container->set('kernel', $kernel);
    $container->set(ReverseContainer::class, new ReverseContainer($container));
    \Drupal::setContainer($container);
    return $container;
  }

}