1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Simplex\DefinitionLoader; |
4
|
|
|
|
5
|
|
|
use DI\ContainerBuilder; |
6
|
|
|
use Simplex\ContainerKeys; |
7
|
|
|
use Simplex\Module; |
8
|
|
|
use Symfony\Component\Finder\Finder; |
9
|
|
|
|
10
|
|
|
final class ModuleDefinitionLoader implements DefinitionLoader |
11
|
|
|
{ |
12
|
|
|
private const MODULES_FILENAME = 'modules.php'; |
13
|
|
|
|
14
|
|
|
/** @var \SplFileInfo */ |
15
|
|
|
private $configDirectory; |
16
|
|
|
|
17
|
6 |
|
public function __construct(\SplFileInfo $configDirectory) |
18
|
|
|
{ |
19
|
6 |
|
$this->configDirectory = $configDirectory; |
20
|
6 |
|
} |
21
|
|
|
|
22
|
6 |
|
public function load(ContainerBuilder $containerBuilder): void |
23
|
|
|
{ |
24
|
6 |
|
$modules = $this->getModules(); |
25
|
|
|
|
26
|
4 |
|
foreach ($modules as $module) { |
27
|
4 |
|
$this->addModuleToContainer($module, $containerBuilder); |
28
|
4 |
|
$this->loadModuleDefinitions($module, $containerBuilder); |
29
|
|
|
} |
30
|
3 |
|
} |
31
|
|
|
|
32
|
|
|
/** @return Module[] */ |
33
|
6 |
|
private function getModules(): array |
34
|
|
|
{ |
35
|
6 |
|
$modulesFile = new \SplFileInfo( |
36
|
6 |
|
$this->configDirectory->getPathname() . DIRECTORY_SEPARATOR . self::MODULES_FILENAME |
37
|
|
|
); |
38
|
|
|
|
39
|
6 |
|
if (!$modulesFile->isFile() || !$modulesFile->isReadable()) { |
40
|
1 |
|
throw new \RuntimeException('Could not read modules file ' . $modulesFile->getRealPath()); |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
$modules = include $modulesFile; |
44
|
|
|
|
45
|
5 |
|
if (!is_array($modules)) { |
46
|
1 |
|
throw new \LogicException('Expected ' . $modulesFile->getPathname() . ' to return an array'); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
return $modules; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
private function addModuleToContainer(Module $module, ContainerBuilder $containerBuilder): void |
53
|
|
|
{ |
54
|
4 |
|
$containerBuilder->addDefinitions([ |
55
|
4 |
|
get_class($module) => \DI\create(get_class($module)), |
56
|
4 |
|
ContainerKeys::MODULES => \DI\add([ |
57
|
4 |
|
\DI\get(get_class($module)), |
58
|
|
|
]), |
59
|
|
|
]); |
60
|
4 |
|
} |
61
|
|
|
|
62
|
4 |
|
private function loadModuleDefinitions(Module $module, ContainerBuilder $containerBuilder): void |
63
|
|
|
{ |
64
|
4 |
|
$definitionsDirectoryPath = $module->getServiceDefinitionsDirectory(); |
65
|
|
|
|
66
|
4 |
|
if (null === $definitionsDirectoryPath) { |
67
|
2 |
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$definitionsDirectory = $this->getModuleDefinitionsDirectory( |
71
|
2 |
|
rtrim($definitionsDirectoryPath, DIRECTORY_SEPARATOR) |
72
|
|
|
); |
73
|
|
|
|
74
|
1 |
|
$finder = new Finder(); |
75
|
1 |
|
$finder->files()->depth(0)->in($definitionsDirectory->getPathname()); |
76
|
|
|
|
77
|
1 |
|
foreach ($finder as $file) { |
78
|
1 |
|
$containerBuilder->addDefinitions($file->getPathname()); |
79
|
|
|
} |
80
|
1 |
|
} |
81
|
|
|
|
82
|
2 |
|
private function getModuleDefinitionsDirectory(string $definitionsDirectoryPath): \SplFileInfo |
83
|
|
|
{ |
84
|
2 |
|
$definitionsDirectory = new \SplFileInfo(rtrim($definitionsDirectoryPath, DIRECTORY_SEPARATOR)); |
85
|
|
|
|
86
|
2 |
|
if (!$definitionsDirectory->isDir() || !$definitionsDirectory->isReadable()) { |
87
|
1 |
|
throw new \RuntimeException( |
88
|
1 |
|
'Invalid module definitions directory path ' . $definitionsDirectory->getRealPath() |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $definitionsDirectory; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|