Completed
Pull Request — master (#62)
by
unknown
06:12
created

DebugKernel   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 182
ccs 52
cts 54
cp 0.963
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A afterSettingsInitialization() 0 12 3
A getEventDispatcher() 0 3 1
A getContainerBuilder() 0 7 1
A getActionManager() 0 3 1
A initializeContainer() 0 7 1
A preHandle() 0 5 1
A initializeSettings() 0 7 1
A boot() 0 17 2
A __construct() 0 23 3
A getKernelParameters() 0 4 1
A attachSynthetic() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Kernel;
15
16
use Drupal\Core\OriginalDrupalKernel;
17
use Ekino\Drupal\Debug\Action\ActionManager;
18
use Ekino\Drupal\Debug\Kernel\Event\AfterAttachSyntheticEvent;
19
use Ekino\Drupal\Debug\Kernel\Event\AfterContainerInitializationEvent;
20
use Ekino\Drupal\Debug\Kernel\Event\AfterRequestPreHandleEvent;
21
use Ekino\Drupal\Debug\Kernel\Event\AfterSettingsInitializationEvent;
22
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents;
23
use Ekino\Drupal\Debug\Option\OptionsStack;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use Symfony\Component\EventDispatcher\EventDispatcher;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
use Symfony\Component\HttpFoundation\Request;
28
29
class DebugKernel extends OriginalDrupalKernel
30
{
31
    /**
32
     * @var EventDispatcher
33
     */
34
    private $eventDispatcher;
35
36
    /**
37
     * @var ActionManager
38
     */
39
    private $actionManager;
40
41
    /**
42
     * @var array
43
     */
44
    private $enabledModules;
45
46
    /**
47
     * @var array
48
     */
49
    private $enabledThemes;
50
51
    /**
52
     * @var bool
53
     */
54
    private $settingsWereInitializedWithTheDedicatedDrupalKernelMethod;
55
56
    /**
57
     * @param string            $environment
58
     * @param object            $classLoader
59
     * @param bool              $allowDumping
60
     * @param string|null       $appRoot
61
     * @param OptionsStack|null $optionsStack
62
     */
63 12
    public function __construct($environment, $classLoader, $allowDumping = true, $appRoot = null, OptionsStack $optionsStack = null)
64
    {
65 12
        $this->eventDispatcher = $this->getEventDispatcher();
66
67 12
        if (!\is_string($appRoot)) {
68 12
            $appRoot = static::guessApplicationRoot();
69
        }
70
71 12
        $this->actionManager = $this->getActionManager($appRoot, $optionsStack instanceof OptionsStack ? $optionsStack : OptionsStack::create());
72
73 12
        $this->actionManager->addEventSubscriberActionsToEventDispatcher($this->eventDispatcher);
74
75 12
        $this->eventDispatcher->dispatch(DebugKernelEvents::ON_KERNEL_INSTANTIATION);
76
77 12
        static::bootEnvironment();
78
79 12
        $this->eventDispatcher->dispatch(DebugKernelEvents::AFTER_ENVIRONMENT_BOOT);
80
81 12
        $this->enabledModules = array();
82 12
        $this->enabledThemes = array();
83 12
        $this->settingsWereInitializedWithTheDedicatedDrupalKernelMethod = false;
84
85 12
        parent::__construct($environment, $classLoader, $allowDumping, $appRoot);
86 12
    }
87
88
    /**
89
     * @return DebugKernel
90
     */
91 2
    public function boot()
92
    {
93
        // The kernel cannot be booted without settings.
94
        //
95
        // If the kernel is going to be booted, but that the
96
        // initializeSettings() method was never called, it means that the
97
        // settings were initialized in another way. If it is not the case, the
98
        // booting is going to fail anyway.
99
        //
100
        // Whatever... Since the settings were not initialized in the traditional
101
        // way, the things we want to do after the settings initialization
102
        // were not done. So we do them now.
103 2
        if (!$this->settingsWereInitializedWithTheDedicatedDrupalKernelMethod) {
104 1
            $this->afterSettingsInitialization();
105
        }
106
107 2
        return parent::boot();
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    public function preHandle(Request $request)
114
    {
115 1
        parent::preHandle($request);
116
117 1
        $this->eventDispatcher->dispatch(DebugKernelEvents::AFTER_REQUEST_PRE_HANDLE, new AfterRequestPreHandleEvent($this->container, $this->enabledModules, $this->enabledThemes));
118 1
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 1
    protected function getKernelParameters()
124
    {
125 1
        return \array_merge(parent::getKernelParameters(), array(
126 1
            'kernel.debug' => true,
127
        ));
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 1
    protected function initializeContainer()
134
    {
135 1
        $container = parent::initializeContainer();
136
137 1
        $this->eventDispatcher->dispatch(DebugKernelEvents::AFTER_CONTAINER_INITIALIZATION, new AfterContainerInitializationEvent($container, $this->enabledModules, $this->enabledThemes));
138
139 1
        return $container;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 2
    protected function initializeSettings(Request $request)
146
    {
147 2
        parent::initializeSettings($request);
148
149 2
        $this->settingsWereInitializedWithTheDedicatedDrupalKernelMethod = true;
150
151 2
        $this->afterSettingsInitialization();
152 2
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 1
    protected function attachSynthetic(ContainerInterface $container)
158
    {
159 1
        $container = parent::attachSynthetic($container);
160
161 1
        $container->set('ekino.drupal.debug.event_dispatcher', $this->eventDispatcher);
162
163 1
        $this->eventDispatcher->dispatch(DebugKernelEvents::AFTER_ATTACH_SYNTHETIC, new AfterAttachSyntheticEvent($container, $this->enabledModules, $this->enabledThemes));
164
165 1
        return $container;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 1
    protected function getContainerBuilder()
172
    {
173 1
        $containerBuilder = parent::getContainerBuilder();
174
175 1
        $this->actionManager->addCompilerPassActionsToContainerBuilder($containerBuilder);
176
177 1
        return $containerBuilder;
178
    }
179
180
    /**
181
     * @return EventDispatcher
182
     */
183 12
    protected function getEventDispatcher(): EventDispatcherInterface
184
    {
185 12
        return new EventDispatcher();
186
    }
187
188
    /**
189
     * @param string       $appRoot
190
     * @param OptionsStack $optionsStack
191
     *
192
     * @return ActionManager
193
     */
194
    protected function getActionManager(string $appRoot, OptionsStack $optionsStack): ActionManager
195
    {
196
        return new ActionManager($appRoot, $optionsStack);
197
    }
198
199 3
    private function afterSettingsInitialization(): void
200
    {
201 3
        $coreExtensionConfig = $this->getConfigStorage()->read('core.extension');
202 3
        if (isset($coreExtensionConfig['module'])) {
203 3
            $this->enabledModules = \array_keys($coreExtensionConfig['module']);
204
        }
205
206 3
        if (isset($coreExtensionConfig['theme'])) {
207 3
            $this->enabledThemes = \array_keys($coreExtensionConfig['theme']);
208
        }
209
210 3
        $this->eventDispatcher->dispatch(DebugKernelEvents::AFTER_SETTINGS_INITIALIZATION, new AfterSettingsInitializationEvent($this->enabledModules, $this->enabledThemes));
211 3
    }
212
}
213