Completed
Push — master ( 139fb8...7c9eb0 )
by Thomas
10s
created

addCompilerPassActionsToContainerBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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\Action;
15
16
use Ekino\Drupal\Debug\Action\DisableCSSAggregation\DisableCSSAggregationAction;
17
use Ekino\Drupal\Debug\Action\DisableDynamicPageCache\DisableDynamicPageCacheAction;
18
use Ekino\Drupal\Debug\Action\DisableInternalPageCache\DisableInternalPageCacheAction;
19
use Ekino\Drupal\Debug\Action\DisableJSAggregation\DisableJSAggregationAction;
20
use Ekino\Drupal\Debug\Action\DisableRenderCache\DisableRenderCacheAction;
21
use Ekino\Drupal\Debug\Action\DisableTwigCache\DisableTwigCacheAction;
22
use Ekino\Drupal\Debug\Action\DisplayDumpLocation\DisplayDumpLocationAction;
23
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptions\DisplayPrettyExceptionsAction;
24
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptionsASAP\DisplayPrettyExceptionsASAPAction;
25
use Ekino\Drupal\Debug\Action\EnableDebugClassLoader\EnableDebugClassLoaderAction;
26
use Ekino\Drupal\Debug\Action\EnableTwigDebug\EnableTwigDebugAction;
27
use Ekino\Drupal\Debug\Action\EnableTwigStrictVariables\EnableTwigStrictVariablesAction;
28
use Ekino\Drupal\Debug\Action\ThrowErrorsAsExceptions\ThrowErrorsAsExceptionsAction;
29
use Ekino\Drupal\Debug\Action\WatchContainerDefinitions\WatchContainerDefinitionsAction;
30
use Ekino\Drupal\Debug\Action\WatchHooksImplementations\WatchHooksImplementationsAction;
31
use Ekino\Drupal\Debug\Action\WatchRoutingDefinitions\WatchRoutingDefinitionsAction;
32
use Ekino\Drupal\Debug\Configuration\ConfigurationManager;
33
use Ekino\Drupal\Debug\Option\OptionsInterface;
34
use Ekino\Drupal\Debug\Option\OptionsStack;
35
use Symfony\Component\DependencyInjection\ContainerBuilder;
36
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
37
38
class ActionManager
39
{
40
    /**
41
     * @var EventSubscriberActionInterface[]
42
     */
43
    private $eventSubscriberActions;
44
45
    /**
46
     * @var CompilerPassActionInterface[]
47
     */
48
    private $compilerPassActions;
49
50
    /**
51
     * @param string       $appRoot
52
     * @param OptionsStack $optionsStack
53
     */
54 2
    public function __construct(string $appRoot, OptionsStack $optionsStack)
55
    {
56 2
        $this->eventSubscriberActions = array();
57 2
        $this->compilerPassActions = array();
58
59 2
        foreach ($this->getActions($appRoot, $optionsStack) as $action) {
60 2
            if ($action instanceof EventSubscriberActionInterface) {
61 2
                $this->eventSubscriberActions[] = $action;
62
            }
63
64 2
            if ($action instanceof CompilerPassActionInterface) {
65 2
                $this->compilerPassActions[] = $action;
66
            }
67
        }
68 2
    }
69
70
    /**
71
     * @param EventDispatcherInterface $eventDispatcher
72
     */
73 1
    public function addEventSubscriberActionsToEventDispatcher(EventDispatcherInterface $eventDispatcher): void
74
    {
75 1
        foreach ($this->eventSubscriberActions as $eventSubscriberAction) {
76 1
            $eventDispatcher->addSubscriber($eventSubscriberAction);
77
        }
78 1
    }
79
80
    /**
81
     * @param ContainerBuilder $containerBuilder
82
     */
83 1
    public function addCompilerPassActionsToContainerBuilder(ContainerBuilder $containerBuilder): void
84
    {
85 1
        foreach ($this->compilerPassActions as $compilerPassAction) {
86 1
            $containerBuilder->addCompilerPass($compilerPassAction);
87
        }
88 1
    }
89
90
    /**
91
     * @param string       $appRoot
92
     * @param OptionsStack $optionsStack
93
     *
94
     * @return ActionInterface[]
95
     */
96 2
    private function getActions(string $appRoot, OptionsStack $optionsStack): array
97
    {
98
        $actionsClasses = array(
99 2
            DisableCSSAggregationAction::class,
100
            DisableDynamicPageCacheAction::class,
101
            DisableInternalPageCacheAction::class,
102
            DisableJSAggregationAction::class,
103
            DisableRenderCacheAction::class,
104
            DisableTwigCacheAction::class,
105
            DisplayDumpLocationAction::class,
106
            DisplayPrettyExceptionsAction::class,
107
            DisplayPrettyExceptionsASAPAction::class,
108
            EnableDebugClassLoaderAction::class,
109
            EnableTwigDebugAction::class,
110
            // Drupal Core does not handle strict variables. temporarily
111
            // @see https://www.drupal.org/project/drupal/issues/2445705
112
            //EnableTwigStrictVariablesAction::class,
113
            ThrowErrorsAsExceptionsAction::class,
114
            WatchContainerDefinitionsAction::class,
115
            WatchHooksImplementationsAction::class,
116
            WatchRoutingDefinitionsAction::class,
117
        );
118
119 2
        $defaultsConfiguration = ConfigurationManager::getDefaultsConfiguration();
120
121 2
        $actions = array();
122 2
        foreach ($actionsClasses as $actionClass) {
123 2
            $refl = new \ReflectionClass($actionClass);
124
125 2
            $args = array();
126 2
            if ($refl->implementsInterface(ActionWithOptionsInterface::class)) {
127 2
                $optionsClass = $refl->getMethod('getOptionsClass')->invoke(null);
128
129 2
                $options = $optionsStack->get($optionsClass);
130 2
                if (!$options instanceof OptionsInterface) {
131 2
                    $options = (new \ReflectionClass($optionsClass))->getMethod('getDefault')->invokeArgs(null, array(
132 2
                        $appRoot,
133 2
                        $defaultsConfiguration,
134
                    ));
135
                }
136
137 2
                $args[] = $options;
138
            }
139
140
            /** @var ActionInterface $action */
141 2
            $action = $refl->newInstanceArgs($args);
142
143 2
            $actions[] = $action;
144
        }
145
146 2
        return $actions;
147
    }
148
}
149