38fee503ec49ed84deee8ead252a604a9276c06a
[yaffs-website] / web / core / modules / system / tests / modules / theme_test / src / EventSubscriber / ThemeTestSubscriber.php
1 <?php
2
3 namespace Drupal\theme_test\EventSubscriber;
4
5 use Drupal\Core\Render\RendererInterface;
6 use Drupal\Core\Url;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Symfony\Component\HttpKernel\KernelEvents;
9 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12 /**
13  * Theme test subscriber for controller requests.
14  */
15 class ThemeTestSubscriber implements EventSubscriberInterface {
16
17   /**
18    * The used container.
19    *
20    * @todo This variable is never initialized, so we don't know what it is.
21    *   See https://www.drupal.org/node/2721315
22    */
23   protected $container;
24
25   /**
26    * The current route match.
27    *
28    * @var \Drupal\Core\Routing\RouteMatchInterface
29    */
30   protected $currentRouteMatch;
31
32   /**
33    * The renderer.
34    *
35    * @var \Drupal\Core\Render\RendererInterface
36    */
37   protected $renderer;
38
39   /**
40    * Constructs a new ThemeTestSubscriber.
41    *
42    * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
43    * @param \Drupal\Core\Render\RendererInterface $renderer
44    *   The renderer.
45    */
46   public function __construct(RouteMatchInterface $current_route_match, RendererInterface $renderer) {
47     $this->currentRouteMatch = $current_route_match;
48     $this->renderer = $renderer;
49   }
50
51   /**
52    * Generates themed output early in a page request.
53    *
54    * @see \Drupal\system\Tests\Theme\ThemeEarlyInitializationTest::testRequestListener()
55    */
56   public function onRequest(GetResponseEvent $event) {
57     if ($this->currentRouteMatch->getRouteName() === 'theme_test.request_listener') {
58       // First, force the theme registry to be rebuilt on this page request.
59       // This allows us to test a full initialization of the theme system in
60       // the code below.
61       drupal_theme_rebuild();
62       // Next, initialize the theme system by storing themed text in a global
63       // variable. We will use this later in
64       // theme_test_request_listener_page_callback() to test that even when the
65       // theme system is initialized this early, it is still capable of
66       // returning output and theming the page as a whole.
67       $more_link = [
68         '#type' => 'more_link',
69         '#url' => Url::fromRoute('user.page'),
70         '#attributes' => ['title' => 'Themed output generated in a KernelEvents::REQUEST listener'],
71       ];
72       $GLOBALS['theme_test_output'] = $this->renderer->renderPlain($more_link);
73     }
74   }
75
76   /**
77    * Ensures that the theme registry was not initialized.
78    */
79   public function onView(GetResponseEvent $event) {
80     $current_route = $this->currentRouteMatch->getRouteName();
81     $entity_autcomplete_route = [
82       'system.entity_autocomplete',
83     ];
84
85     if (in_array($current_route, $entity_autcomplete_route)) {
86       if ($this->container->initialized('theme.registry')) {
87         throw new \Exception('registry initialized');
88       }
89     }
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public static function getSubscribedEvents() {
96     $events[KernelEvents::REQUEST][] = ['onRequest'];
97     $events[KernelEvents::VIEW][] = ['onView', -1000];
98     return $events;
99   }
100
101 }