1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the ModularBundle project. |
||
5 | * |
||
6 | * (c) Anthonius Munthi <https://itstoni.com> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Doyo\Bundle\Modular; |
||
15 | |||
16 | use Doctrine\Inflector\InflectorFactory; |
||
17 | use Doyo\Bundle\Modular\Application\ModuleInterface; |
||
18 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
19 | use Symfony\Component\Finder\Finder; |
||
20 | use Symfony\Component\Finder\SplFileInfo; |
||
21 | use Symfony\Component\HttpKernel\KernelInterface; |
||
22 | |||
23 | class Modules |
||
24 | { |
||
25 | /** |
||
26 | * @var array<array-key,ModuleInterface> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
27 | */ |
||
28 | private static array $modules = []; |
||
29 | |||
30 | /** |
||
31 | * @return array<array-key,ModuleInterface> |
||
0 ignored issues
–
show
|
|||
32 | */ |
||
33 | public function getModules(): array |
||
34 | { |
||
35 | return static::$modules; |
||
0 ignored issues
–
show
|
|||
36 | } |
||
37 | |||
38 | public function buildModules(ContainerBuilder $container): void |
||
39 | { |
||
40 | $this->initModules($container); |
||
41 | } |
||
42 | |||
43 | public function initModules(ContainerBuilder $container): void |
||
44 | { |
||
45 | $definition = $container->getDefinition('kernel'); |
||
46 | /** @var class-string<KernelInterface> $kernelClass */ |
||
47 | $kernelClass = $definition->getClass(); |
||
48 | $r = new \ReflectionClass($kernelClass); |
||
49 | $dir = \dirname((string) $r->getFileName()); |
||
50 | |||
51 | $finder = Finder::create() |
||
52 | ->in($dir) |
||
53 | ->depth(1) |
||
54 | ->name('*Module.php'); |
||
55 | |||
56 | /** @var SplFileInfo $file */ |
||
57 | foreach ($finder->files() as $file) { |
||
58 | /** @var class-string<ModuleInterface> $class */ |
||
59 | $class = $r->getNamespaceName().'\\'.$file->getRelativePath().'\\'.$file->getBasename('.php'); |
||
60 | if (class_exists($class, true)) { |
||
61 | $this->registerModule($container, $class); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param class-string<ModuleInterface> $class |
||
0 ignored issues
–
show
|
|||
68 | */ |
||
69 | private function registerModule(ContainerBuilder $container, string $class): void |
||
70 | { |
||
71 | $module = new $class(); |
||
72 | $module->boot(); |
||
73 | $inflector = InflectorFactory::create()->build(); |
||
74 | $exp = explode('\\', $module->getNamespace()); |
||
75 | $paramNS = $inflector->tableize($exp[0].'.'.$module->getName()); |
||
76 | $container->setParameter($paramNS.'.base_path', $module->getBasePath()); |
||
77 | static::$modules[$module->getName()] = $module; |
||
0 ignored issues
–
show
|
|||
78 | |||
79 | $definition = $container->register('doyo.modules.'.$module->getName(), $class); |
||
80 | $definition->setPublic(true); |
||
81 | $definition->addTag('doyo.modules'); |
||
82 | $definition->addMethodCall('boot'); |
||
83 | } |
||
84 | } |
||
85 |