Completed
Push — master ( 52b242...112fdc )
by Thomas
17s queued 11s
created

DebugKernel::attachSynthetic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 1
rs 9.9666
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\ActionRegistrar;
18
use Ekino\Drupal\Debug\ActionMetadata\ActionMetadataManager;
19
use Ekino\Drupal\Debug\Configuration\ConfigurationManager;
20
use Ekino\Drupal\Debug\Kernel\Event\AfterAttachSyntheticEvent;
21
use Ekino\Drupal\Debug\Kernel\Event\AfterContainerInitializationEvent;
22
use Ekino\Drupal\Debug\Kernel\Event\AfterRequestPreHandleEvent;
23
use Ekino\Drupal\Debug\Kernel\Event\AfterSettingsInitializationEvent;
24
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents;
25
use Ekino\Drupal\Debug\Option\OptionsStack;
26
use Symfony\Component\DependencyInjection\ContainerInterface;
27
use Symfony\Component\EventDispatcher\EventDispatcher;
28
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
29
use Symfony\Component\HttpFoundation\Request;
30
31
class DebugKernel extends OriginalDrupalKernel
32
{
33
    /**
34
     * @var EventDispatcher
35
     */
36
    private $eventDispatcher;
37
38
    private $configurationManager;
39
40
    /**
41
     * @var ActionRegistrar
42
     */
43
    private $actionRegistrar;
44
45
    /**
46
     * @var array
47
     */
48
    private $enabledModules;
49
50
    /**
51
     * @var array
52
     */
53
    private $enabledThemes;
54
55
    /**
56
     * @var bool
57
     */
58
    private $settingsWereInitializedWithTheDedicatedDrupalKernelMethod;
59
60
    /**
61
     * @param string            $environment
62
     * @param object            $classLoader
63
     * @param bool              $allowDumping
64
     * @param string|null       $appRoot
65
     * @param OptionsStack|null $optionsStack
66
     */
67 12
    public function __construct($environment, $classLoader, $allowDumping = true, $appRoot = null, OptionsStack $optionsStack = null)
68
    {
69 12
        $this->eventDispatcher = $this->getEventDispatcher();
70
71 12
        if (!\is_string($appRoot)) {
72 12
            $appRoot = static::guessApplicationRoot();
73
        }
74
75 12
        $this->actionRegistrar = $this->getActionRegistrar(
76 12
            $appRoot,
77 12
            ActionMetadataManager::getInstance(),
78 12
            $this->configurationManager = ConfigurationManager::getInstance(),
79 12
            $optionsStack instanceof OptionsStack ? $optionsStack : OptionsStack::create()
80
        );
81
82 12
        $this->actionRegistrar->addEventSubscriberActionsToEventDispatcher($this->eventDispatcher);
83
84 12
        $this->eventDispatcher->dispatch(DebugKernelEvents::ON_KERNEL_INSTANTIATION);
85
86 12
        static::bootEnvironment();
87
88 12
        $this->eventDispatcher->dispatch(DebugKernelEvents::AFTER_ENVIRONMENT_BOOT);
89
90 12
        $this->enabledModules = array();
91 12
        $this->enabledThemes = array();
92 12
        $this->settingsWereInitializedWithTheDedicatedDrupalKernelMethod = false;
93
94 12
        parent::__construct($environment, $classLoader, $allowDumping, $appRoot);
95 12
    }
96
97
    /**
98
     * @return DebugKernel
99
     */
100 2
    public function boot()
101
    {
102
        // The kernel cannot be booted without settings.
103
        //
104
        // If the kernel is going to be booted, but that the
105
        // initializeSettings() method was never called, it means that the
106
        // settings were initialized in another way. If it is not the case, the
107
        // booting is going to fail anyway.
108
        //
109
        // Whatever... Since the settings were not initialized in the traditional
110
        // way, the things we want to do after the settings initialization
111
        // were not done. So we do them now.
112 2
        if (!$this->settingsWereInitializedWithTheDedicatedDrupalKernelMethod) {
113 1
            $this->afterSettingsInitialization();
114
        }
115
116 2
        return parent::boot();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 1
    public function preHandle(Request $request)
123
    {
124 1
        parent::preHandle($request);
125
126 1
        $this->eventDispatcher->dispatch(
127 1
            DebugKernelEvents::AFTER_REQUEST_PRE_HANDLE,
128 1
            new AfterRequestPreHandleEvent(
129 1
                $this->configurationManager->doesConfigurationChanged(),
130 1
                $this->container,
131 1
                $this->enabledModules,
132 1
                $this->enabledThemes
133
            )
134
        );
135 1
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 1
    protected function getKernelParameters()
141
    {
142 1
        return \array_merge(parent::getKernelParameters(), array(
143 1
            'kernel.debug' => true,
144
        ));
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 1
    protected function initializeContainer()
151
    {
152 1
        $container = parent::initializeContainer();
153
154 1
        $this->eventDispatcher->dispatch(
155 1
            DebugKernelEvents::AFTER_CONTAINER_INITIALIZATION,
156 1
            new AfterContainerInitializationEvent(
157 1
                $this->configurationManager->doesConfigurationChanged(),
158
                $container,
159 1
                $this->enabledModules,
160 1
                $this->enabledThemes
161
            )
162
        );
163
164 1
        return $container;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 2
    protected function initializeSettings(Request $request)
171
    {
172 2
        parent::initializeSettings($request);
173
174 2
        $this->settingsWereInitializedWithTheDedicatedDrupalKernelMethod = true;
175
176 2
        $this->afterSettingsInitialization();
177 2
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 1
    protected function attachSynthetic(ContainerInterface $container)
183
    {
184 1
        $container = parent::attachSynthetic($container);
185
186 1
        $this->eventDispatcher->dispatch(
187 1
            DebugKernelEvents::AFTER_ATTACH_SYNTHETIC,
188 1
            new AfterAttachSyntheticEvent(
189 1
                $this->configurationManager->doesConfigurationChanged(),
190
                $container,
191 1
                $this->enabledModules,
192 1
                $this->enabledThemes
193
            )
194
        );
195
196 1
        return $container;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 1
    protected function getContainerBuilder()
203
    {
204 1
        $containerBuilder = parent::getContainerBuilder();
205
206 1
        $this->actionRegistrar->addCompilerPassActionsToContainerBuilder($containerBuilder);
207
208 1
        return $containerBuilder;
209
    }
210
211
    /**
212
     * @return EventDispatcher
213
     */
214 12
    protected function getEventDispatcher(): EventDispatcherInterface
215
    {
216 12
        return new EventDispatcher();
217
    }
218
219
    protected function getActionRegistrar(
220
        string $appRoot,
221
        ActionMetadataManager $actionMetadataManager,
222
        ConfigurationManager $configurationManager,
223
        OptionsStack $optionsStack
224
    ): ActionRegistrar {
225
        return new ActionRegistrar($appRoot, $actionMetadataManager, $configurationManager, $optionsStack);
226
    }
227
228 3
    private function afterSettingsInitialization(): void
229
    {
230
        /** @var array $coreExtensionConfig */
231 3
        $coreExtensionConfig = $this->getConfigStorage()->read('core.extension');
232 3
        if (isset($coreExtensionConfig['module'])) {
233 3
            $this->enabledModules = \array_keys($coreExtensionConfig['module']);
234
        }
235
236 3
        if (isset($coreExtensionConfig['theme'])) {
237 3
            $this->enabledThemes = \array_keys($coreExtensionConfig['theme']);
238
        }
239
240 3
        $this->eventDispatcher->dispatch(
241 3
            DebugKernelEvents::AFTER_SETTINGS_INITIALIZATION,
242 3
            new AfterSettingsInitializationEvent(
243 3
                $this->configurationManager->doesConfigurationChanged(),
244 3
                $this->enabledModules,
245 3
                $this->enabledThemes
246
            )
247
        );
248 3
    }
249
}
250