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

WatchModulesHooksImplementationsAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 68.57%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 91
ccs 24
cts 35
cp 0.6857
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

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