Modules::buildModules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
The doc comment array<array-key,ModuleInterface> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key,ModuleInterface>.
Loading history...
27
     */
28
    private static array $modules = [];
29
30
    /**
31
     * @return array<array-key,ModuleInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key,ModuleInterface> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key,ModuleInterface>.
Loading history...
32
     */
33
    public function getModules(): array
34
    {
35
        return static::$modules;
0 ignored issues
show
Bug introduced by
Since $modules is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $modules to at least protected.
Loading history...
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
Documentation Bug introduced by
The doc comment class-string<ModuleInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ModuleInterface>.
Loading history...
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
Bug introduced by
Since $modules is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $modules to at least protected.
Loading history...
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