1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Framework\Symfony; |
16
|
|
|
|
17
|
|
|
use Exception; |
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
19
|
|
|
use Symfony\Component\Config\Exception\FileLoaderLoadException; |
20
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
22
|
|
|
use Symfony\Component\HttpKernel\Kernel as BaseKernel; |
23
|
|
|
use Symfony\Component\Routing\RouteCollectionBuilder; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Javier Eguiluz |
27
|
|
|
* @author Herberto Graca <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class Kernel extends BaseKernel |
30
|
|
|
{ |
31
|
|
|
use MicroKernelTrait; |
32
|
|
|
|
33
|
|
|
private const ENV_PROD = 'prod'; |
34
|
|
|
|
35
|
|
|
const CONFIG_EXTS = '.{php,xml,yaml,yml}'; |
36
|
|
|
|
37
|
|
|
public function getCacheDir(): string |
38
|
|
|
{ |
39
|
|
|
return $this->getProjectDir() . '/var/cache/' . $this->environment; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function getConfDir(): string |
43
|
|
|
{ |
44
|
|
|
return $this->getProjectDir() . '/config'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getLogDir(): string |
48
|
|
|
{ |
49
|
|
|
return $this->getProjectDir() . '/var/log'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function registerBundles() |
53
|
|
|
{ |
54
|
|
|
/** @var bool[][] $contents */ |
55
|
|
|
$contents = require $this->getConfDir() . '/bundles.php'; |
56
|
|
|
foreach ($contents as $class => $envs) { |
57
|
|
|
if (isset($envs['all']) || isset($envs[$this->environment])) { |
58
|
|
|
yield new $class(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @throws Exception |
65
|
|
|
*/ |
66
|
|
|
protected function configureContainer(ContainerBuilder $containerBuilder, LoaderInterface $loader): void |
67
|
|
|
{ |
68
|
|
|
$containerBuilder->setParameter('container.dumper.inline_class_loader', true); |
69
|
|
|
$confDir = $this->getConfDir(); |
70
|
|
|
|
71
|
|
|
$this->configureParameters($loader, $confDir); |
72
|
|
|
$this->configurePackages($loader, $confDir); |
73
|
|
|
$this->configureServices($loader, $confDir); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @throws FileLoaderLoadException |
78
|
|
|
*/ |
79
|
|
|
protected function configureRoutes(RouteCollectionBuilder $routes): void |
80
|
|
|
{ |
81
|
|
|
$confDir = $this->getConfDir(); |
82
|
|
|
|
83
|
|
|
$environmentList = array_unique([self::ENV_PROD, $this->environment]); |
84
|
|
|
foreach ($environmentList as $environment) { |
85
|
|
|
// Routes can not be bulk imported because they need to be ordered |
86
|
|
|
$routes->import($confDir . '/routes/{' . $environment . '}/index' . self::CONFIG_EXTS, '/', 'glob'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function build(ContainerBuilder $containerBuilder): void |
91
|
|
|
{ |
92
|
|
|
/** @var bool[][] $contents */ |
93
|
|
|
$contents = require $this->getConfDir() . '/compiler_pass.php'; |
94
|
|
|
foreach ($contents as $compilerPass => $envs) { |
95
|
|
|
if (isset($envs['all']) || isset($envs[$this->environment])) { |
96
|
|
|
$containerBuilder->addCompilerPass(new $compilerPass()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @throws Exception |
103
|
|
|
*/ |
104
|
|
|
private function configureParameters(LoaderInterface $loader, string $confDir): void |
105
|
|
|
{ |
106
|
|
|
$environmentList = array_unique([self::ENV_PROD, $this->environment]); |
107
|
|
|
foreach ($environmentList as $environment) { |
108
|
|
|
$loader->load($confDir . '/parameters/{' . $environment . '}' . self::CONFIG_EXTS, 'glob'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @throws Exception |
114
|
|
|
*/ |
115
|
|
|
private function configurePackages(LoaderInterface $loader, string $confDir): void |
116
|
|
|
{ |
117
|
|
|
$loader->load($confDir . '/packages/*' . self::CONFIG_EXTS, 'glob'); |
118
|
|
|
$loader->load($confDir . '/packages/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @throws Exception |
123
|
|
|
*/ |
124
|
|
|
private function configureServices(LoaderInterface $loader, string $confDir): void |
125
|
|
|
{ |
126
|
|
|
$environmentList = array_unique([self::ENV_PROD, $this->environment]); |
127
|
|
|
foreach ($environmentList as $environment) { |
128
|
|
|
$loader->load($confDir . '/services/{' . $environment . '}' . self::CONFIG_EXTS, 'glob'); |
129
|
|
|
$loader->load($confDir . '/services/{' . $environment . '}/**/*' . self::CONFIG_EXTS, 'glob'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|