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\DrupalKernelInterface; |
17
|
|
|
use Drupal\Core\Extension\ModuleHandler; |
18
|
|
|
use Ekino\Drupal\Debug\Cache\Event\CacheNotFreshEvent; |
19
|
|
|
use Ekino\Drupal\Debug\Extension\Model\CustomModule; |
20
|
|
|
use Ekino\Drupal\Debug\Resource\Model\CustomExtensionFileResource; |
21
|
|
|
|
22
|
|
|
class LoadNewModuleFile |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ModuleHandler |
26
|
|
|
*/ |
27
|
|
|
private $moduleHandler; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DrupalKernelInterface |
31
|
|
|
*/ |
32
|
|
|
private $drupalKernel; |
33
|
|
|
|
34
|
7 |
|
public function __construct(ModuleHandler $moduleHandler, DrupalKernelInterface $drupalKernel) |
35
|
|
|
{ |
36
|
7 |
|
$this->moduleHandler = $moduleHandler; |
37
|
7 |
|
$this->drupalKernel = $drupalKernel; |
38
|
7 |
|
} |
39
|
|
|
|
40
|
6 |
|
public function __invoke(CacheNotFreshEvent $event): void |
41
|
|
|
{ |
42
|
6 |
|
foreach ($event->getFileCache()->getCurrentResourcesCollection()->all() as $resource) { |
43
|
6 |
|
if (!$resource instanceof CustomExtensionFileResource) { |
44
|
1 |
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
5 |
|
if (!$resource->isNew()) { |
48
|
1 |
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
$customExtension = $resource->getCustomExtension(); |
52
|
4 |
|
if (!$customExtension instanceof CustomModule) { |
53
|
1 |
|
continue; |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
$machineName = $customExtension->getMachineName(); |
57
|
3 |
|
$extensionFilename = \sprintf('%s.module', $machineName); |
58
|
3 |
|
if ($extensionFilename !== \substr($resource->getFilePath(), -\strlen($extensionFilename))) { |
59
|
1 |
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
$module = $this->moduleHandler->getModule($machineName); |
63
|
2 |
|
if (\is_string($module->getExtensionFilename())) { |
64
|
1 |
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$name = $module->getName(); |
68
|
|
|
|
69
|
1 |
|
$this->moduleHandler->addModule($name, $module->getPath()); |
70
|
1 |
|
$this->moduleHandler->getModule($name)->load(); |
71
|
|
|
|
72
|
1 |
|
$this->drupalKernel->invalidateContainer(); |
73
|
|
|
} |
74
|
6 |
|
} |
75
|
|
|
} |
76
|
|
|
|