Completed
Push — master ( 76c18a...59b186 )
by Thomas
24s
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\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\EventDispatcherInterface;
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 13
    public function __construct(WatchModulesHooksImplementationsOptions $options)
60
    {
61 13
        $this->options = $options;
62 13
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 9
    public function process(ContainerBuilder $container): void
68
    {
69 9
        $moduleHandlerDefinition = $this->validateContainerDefinitionClassIs($container, 'module_handler', ModuleHandler::class);
70 7
        $eventDispatcherDefinition = $this->validateContainerDefinitionClassImplements($container, 'event_dispatcher', EventDispatcherInterface::class);
71 4
        $this->validateContainerDefinitionClassImplements($container, 'kernel', HttpKernelInterface::class);
72
73 1
        $resourcesDefinition = new Definition();
74 1
        $resourcesDefinition->setSynthetic(true);
75 1
        $container->setDefinition(self::RESOURCES_SERVICE_ID, $resourcesDefinition);
76
77 1
        $fileBackendDefinition = new Definition(FileBackend::class, array(
78 1
            new Definition(FileCache::class, array(
79 1
                $this->options->getCacheFilePath(),
80 1
                new Reference(self::RESOURCES_SERVICE_ID),
81
            )),
82
        ));
83 1
        $fileBackendDefinition->addMethodCall('setEventDispatcher', array(
84 1
            new Reference('event_dispatcher'),
85
        ));
86
87 1
        $moduleHandlerDefinition->replaceArgument(2, $fileBackendDefinition);
88
89
        $eventDispatcherDefinition
90 1
            ->addMethodCall('addListener', array(
91 1
                FileBackendEvents::ON_CACHE_NOT_FRESH,
92 1
                new Definition(LoadNewModuleFile::class, array(
93 1
                    new Reference('module_handler'),
94 1
                    new Reference('kernel'),
95
                )),
96
          ));
97 1
    }
98
99
    /**
100
     * @param AfterAttachSyntheticEvent $event
101
     */
102
    public function setResources(AfterAttachSyntheticEvent $event): void
103
    {
104
        $event->getContainer()->set(self::RESOURCES_SERVICE_ID, $this->options->getFilteredResourcesCollection($event->getEnabledModules(), $event->getEnabledThemes()));
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 3
    public static function getOptionsClass(): string
111
    {
112 3
        return WatchModulesHooksImplementationsOptions::class;
113
    }
114
}
115