Test Failed
Branch master (fdda4e)
by ANTHONIUS
03:39
created

Kernel   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
c 0
b 0
f 0
dl 0
loc 120
rs 10
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configureContainer() 0 17 3
A initializeModules() 0 11 3
A configureRoutes() 0 17 4
A configureModule() 0 26 4
A getModules() 0 3 1
A initializeBundles() 0 11 3
1
<?php
2
3
/*
4
 * This file is part of the EOffice 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 EOffice\Core\Application;
15
16
use EOffice\Contracts\Support\ModuleInterface;
17
use EOffice\Core\Exception\CoreException;
18
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
19
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
20
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
21
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
22
23
class Kernel extends BaseKernel
24
{
25
    use MicroKernelTrait;
0 ignored issues
show
Bug introduced by
The trait Symfony\Bundle\Framework...Kernel\MicroKernelTrait requires the property $instanceof which is not provided by EOffice\Core\Application\Kernel.
Loading history...
26
27
    /**
28
     * @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...
29
     */
30
    private array $modules;
31
32
    /**
33
     * @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...
34
     */
35
    public function getModules(): array
36
    {
37
        return $this->modules;
38
    }
39
40
    /**
41
     * @throws CoreException
42
     */
43
    protected function initializeBundles(): void
44
    {
45
        parent::initializeBundles();
46
        $this->modules = [];
47
48
        foreach ($this->initializeModules() as $module) {
49
            if ( ! $module instanceof ModuleInterface) {
50
                $moduleClass = \get_class($module);
51
                throw CoreException::moduleShouldImplementModuleInterface($moduleClass);
52
            }
53
            $this->modules[$module->getName()] = $module;
54
        }
55
    }
56
57
    protected function configureContainer(ContainerConfigurator $container): void
58
    {
59
        $env        = $this->getEnvironment();
60
        $projectDir = $this->getProjectDir();
61
62
        $container->import($projectDir.'/config/{packages}/*.yaml');
63
        $container->import($projectDir.'/config/{packages}/'.$env.'/*.yaml');
64
65
        if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
66
            $container->import($projectDir.'/config/services.yaml');
67
            $container->import($projectDir.'/config/{services}_'.$env.'.yaml');
68
        } else {
69
            $container->import($projectDir.'/config/{services}.php');
70
        }
71
72
        foreach ($this->modules as $module) {
73
            $this->configureModule($container, $module);
74
        }
75
    }
76
77
    protected function configureRoutes(RoutingConfigurator $routes): void
78
    {
79
        $env        = $this->getEnvironment();
80
        $projectDir = $this->getProjectDir();
81
        $routes->import($projectDir.'/config/{routes}/'.$env.'/*.yaml');
82
        $routes->import($projectDir.'/config/{routes}/*.yaml');
83
84
        if (is_file(\dirname(__DIR__).'/config/routes.yaml')) {
85
            $routes->import($projectDir.'/config/routes.yaml');
86
        } else {
87
            $routes->import($projectDir.'/config/{routes}.php');
88
        }
89
90
        foreach ($this->getModules() as $module) {
91
            $baseDir = $module->getBaseDir();
92
            if (is_file($file = $baseDir.'/Resources/config/routes.yaml')) {
93
                $routes->import($file);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @throws CoreException
100
     *
101
     * @return iterable
102
     */
103
    protected function initializeModules(): iterable
104
    {
105
        $path = $this->getProjectDir().'/config/modules.php';
106
        if ( ! is_file($path)) {
107
            throw CoreException::modulesFileNotFound($path);
108
        }
109
110
        /** @var array $modules */
111
        $modules = require $path;
112
        foreach ($modules as $class) {
113
            yield new $class();
114
        }
115
    }
116
117
    protected function configureModule(ContainerConfigurator $container, ModuleInterface $module): void
118
    {
119
        $env       = $this->getEnvironment();
120
        $moduleDir = $module->getBaseDir();
121
        $name      = $module->getName();
122
123
        $container->parameters()->set('eoffice.'.$name.'.module_dir', $moduleDir);
124
        if (is_file($serviceConfig = $moduleDir.'/Resources/config/services.xml')) {
125
            $container->import($serviceConfig);
126
        }
127
        if (is_file($envConfig = $moduleDir.'/Resources/config/{services}_'.$env.'.yaml')) {
128
            $container->import($envConfig);
129
        }
130
        $resourcesDir = $moduleDir.'/Resources';
131
        if (is_dir($resourcesDir)) {
132
            // load override package
133
            $container->import($resourcesDir.'/{packages}/*.yaml');
134
            $container->import($resourcesDir.'/{packages}/*.xml');
135
            $container->import($resourcesDir.'/{packages}/'.$env.'/*.yaml');
136
            $container->import($resourcesDir.'/{packages}/'.$env.'/*.xml');
137
138
            // load services
139
            $container->import($resourcesDir.'/{services}/*.yaml');
140
            $container->import($resourcesDir.'/{services}/*.xml');
141
            $container->import($resourcesDir.'/{services}/'.$env.'/*.yaml');
142
            $container->import($resourcesDir.'/{services}/'.$env.'/*.xml');
143
        }
144
    }
145
}
146