Passed
Pull Request — master (#62)
by
unknown
06:24
created

WatchModulesHooksImplementationsAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 84
ccs 24
cts 32
cp 0.75
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 25 1
A getSubscribedEvents() 0 4 1
A setResources() 0 9 1
A __construct() 0 3 1
A getOptionsClass() 0 3 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\Action\WatchModulesHooksImplementations;
15
16
use Drupal\Core\Extension\ModuleHandler;
17
use Ekino\Drupal\Debug\Action\ActionWithOptionsInterface;
18
use Ekino\Drupal\Debug\Action\CompilerPassActionInterface;
19
use Ekino\Drupal\Debug\Action\EventSubscriberActionInterface;
20
use Ekino\Drupal\Debug\Action\ValidateContainerDefinitionTrait;
21
use Ekino\Drupal\Debug\Cache\Event\FileBackendEvents;
22
use Ekino\Drupal\Debug\Cache\FileBackend;
23
use Ekino\Drupal\Debug\Cache\FileCache;
24
use Ekino\Drupal\Debug\Kernel\Event\AfterAttachSyntheticEvent;
25
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents;
26
use Symfony\Component\DependencyInjection\ContainerBuilder;
27
use Symfony\Component\DependencyInjection\Definition;
28
use Symfony\Component\DependencyInjection\Reference;
29
use Symfony\Component\HttpKernel\HttpKernelInterface;
30
31
class WatchModulesHooksImplementationsAction implements CompilerPassActionInterface, EventSubscriberActionInterface, ActionWithOptionsInterface
32
{
33
    use ValidateContainerDefinitionTrait;
34
35
    /**
36
     * @var string
37
     */
38
    private const RESOURCES_SERVICE_ID = 'ekino.drupal.debug.action.watch_modules_hooks_implementations.resources';
39
40
    /**
41
     * @var WatchModulesHooksImplementationsOptions
42
     */
43
    private $options;
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public static function getSubscribedEvents(): array
49
    {
50
        return array(
51 1
            DebugKernelEvents::AFTER_ATTACH_SYNTHETIC => 'setResources',
52
        );
53
    }
54
55
    /**
56
     * @param WatchModulesHooksImplementationsOptions $options
57
     */
58 10
    public function __construct(WatchModulesHooksImplementationsOptions $options)
59
    {
60 10
        $this->options = $options;
61 10
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 6
    public function process(ContainerBuilder $container): void
67
    {
68 6
        $moduleHandlerDefinition = $this->validateContainerDefinitionClassIs($container, 'module_handler', ModuleHandler::class);
69
70 4
        $eventDispatcherDefinition = new Definition();
71 4
        $eventDispatcherDefinition->setSynthetic(true);
72 4
        $container->setDefinition('ekino.drupal.debug.event_dispatcher', $eventDispatcherDefinition);
73
74 4
        $this->validateContainerDefinitionClassImplements($container, 'kernel', HttpKernelInterface::class);
75
76 1
        $resourcesDefinition = new Definition();
77 1
        $resourcesDefinition->setSynthetic(true);
78 1
        $container->setDefinition(self::RESOURCES_SERVICE_ID, $resourcesDefinition);
79
80 1
        $fileBackendDefinition = new Definition(FileBackend::class, array(
81 1
            new Definition(FileCache::class, array(
82 1
                $this->options->getCacheFilePath(),
83 1
                new Reference(self::RESOURCES_SERVICE_ID),
84
            )),
85
        ));
86 1
        $fileBackendDefinition->addMethodCall('setEventDispatcher', array(
87 1
            new Reference('ekino.drupal.debug.event_dispatcher'),
88
        ));
89
90 1
        $moduleHandlerDefinition->replaceArgument(2, $fileBackendDefinition);
91 1
    }
92
93
    /**
94
     * @param AfterAttachSyntheticEvent $event
95
     */
96
    public function setResources(AfterAttachSyntheticEvent $event): void
97
    {
98
        $event->getContainer()->set(self::RESOURCES_SERVICE_ID, $this->options->getFilteredResourcesCollection($event->getEnabledModules(), $event->getEnabledThemes()));
99
100
        $event->getContainer()->get('ekino.drupal.debug.event_dispatcher')->addListener(
101
            FileBackendEvents::ON_CACHE_NOT_FRESH,
102
            new LoadNewModuleFile(
103
                $event->getContainer()->get('module_handler'),
104
                $event->getContainer()->get('kernel')
105
            )
106
        );
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 3
    public static function getOptionsClass(): string
113
    {
114 3
        return WatchModulesHooksImplementationsOptions::class;
115
    }
116
}
117