1 | <?php declare(strict_types = 1); |
||
2 | |||
3 | namespace Simplex; |
||
4 | |||
5 | use DI\Container; |
||
6 | use DI\ContainerBuilder as PHPDIContainerBuilder; |
||
7 | use Doctrine\Common\Annotations\AnnotationRegistry; |
||
8 | use Simplex\DefinitionLoader\ChainDefinitionLoader; |
||
9 | use Simplex\DefinitionLoader\ConfigDefinitionLoader; |
||
10 | use Simplex\DefinitionLoader\CoreDefinitionLoader; |
||
11 | use Simplex\DefinitionLoader\ModuleDefinitionLoader; |
||
12 | |||
13 | class Bootstrap |
||
14 | { |
||
15 | private const CONFIG_DIRECTORY_NAME = 'config'; |
||
16 | private const CACHE_DIRECTORY_NAME = 'cache'; |
||
17 | private const CONTAINER_DIRECTORY_NAME = 'container'; |
||
18 | |||
19 | /** @var Container */ |
||
20 | private $container; |
||
21 | |||
22 | /** @var \SplFileInfo */ |
||
23 | private $projectRootDirectory; |
||
24 | |||
25 | public function __construct(string $pathToProjectRoot) |
||
26 | { |
||
27 | $projectRootDirectory = new \SplFileInfo($pathToProjectRoot); |
||
28 | |||
29 | if (!$projectRootDirectory->isDir() || !$projectRootDirectory->isReadable()) { |
||
30 | throw new \RuntimeException('Invalid project root directory: ' . $projectRootDirectory->getRealPath()); |
||
31 | } |
||
32 | |||
33 | $this->projectRootDirectory = new \SplFileInfo($pathToProjectRoot); |
||
34 | } |
||
35 | |||
36 | public function getContainer(): Container |
||
37 | { |
||
38 | if (null == $this->container) { |
||
39 | $this->init(); |
||
40 | } |
||
41 | |||
42 | return $this->container; |
||
43 | } |
||
44 | |||
45 | public function getProjectRootDirectory(): \SplFileInfo |
||
46 | { |
||
47 | return $this->projectRootDirectory; |
||
48 | } |
||
49 | |||
50 | public function getConfigDirectory(): \SplFileInfo |
||
51 | { |
||
52 | return new \SplFileInfo( |
||
53 | $this->projectRootDirectory->getPathname() . DIRECTORY_SEPARATOR . self::CONFIG_DIRECTORY_NAME |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | public function getCacheDirectory(): \SplFileInfo |
||
58 | { |
||
59 | return new \SplFileInfo( |
||
60 | $this->projectRootDirectory->getPathname() . DIRECTORY_SEPARATOR . self::CACHE_DIRECTORY_NAME |
||
61 | ); |
||
62 | } |
||
63 | |||
64 | public function getCompiledContainerDirectory(): \SplFileInfo |
||
65 | { |
||
66 | return new \SplFileInfo( |
||
67 | $this->getCacheDirectory()->getPathname() . DIRECTORY_SEPARATOR . self::CONTAINER_DIRECTORY_NAME |
||
68 | ); |
||
69 | } |
||
70 | |||
71 | private function init(): void |
||
72 | { |
||
73 | AnnotationRegistry::registerLoader('class_exists'); |
||
0 ignored issues
–
show
|
|||
74 | |||
75 | (new Environment())->load($this->getProjectRootDirectory()); |
||
76 | |||
77 | $definitionLoader = $this->buildDefinitionLoader(); |
||
78 | |||
79 | $containerBuilder = $this->buildContainerBuilder($definitionLoader); |
||
80 | |||
81 | $this->container = $containerBuilder->build(); |
||
82 | } |
||
83 | |||
84 | private function buildDefinitionLoader(): ChainDefinitionLoader |
||
85 | { |
||
86 | $definitionLoader = new ChainDefinitionLoader( |
||
87 | new CoreDefinitionLoader($this->buildCoreConfigDefinitions()), |
||
88 | new ModuleDefinitionLoader($this->getConfigDirectory()), |
||
89 | new ConfigDefinitionLoader($this->getConfigDirectory(), getenv(Environment::SIMPLEX_ENV)) |
||
90 | ); |
||
91 | return $definitionLoader; |
||
92 | } |
||
93 | |||
94 | private function buildCoreConfigDefinitions(): array |
||
95 | { |
||
96 | return [ |
||
97 | ContainerKeys::ENVIRONMENT => getenv(Environment::SIMPLEX_ENV), |
||
98 | ContainerKeys::DEBUG_MODE => getenv(Environment::DEBUG_MODE), |
||
99 | ContainerKeys::ENABLE_CACHE => getenv(Environment::ENABLE_CACHE), |
||
100 | ContainerKeys::COMPILE_CONTAINER => getenv(Environment::COMPILE_CONTAINER), |
||
101 | ContainerKeys::PROJECT_ROOT_DIRECTORY => $this->getProjectRootDirectory(), |
||
102 | ContainerKeys::CONFIG_DIRECTORY => $this->getConfigDirectory(), |
||
103 | ContainerKeys::CACHE_DIRECTORY => $this->getCacheDirectory(), |
||
104 | ContainerKeys::COMPILED_CONTAINER_DIR => $this->getCompiledContainerDirectory(), |
||
105 | ]; |
||
106 | } |
||
107 | |||
108 | private function buildContainerBuilder($definitionLoader): ContainerBuilder |
||
109 | { |
||
110 | $containerBuilder = new ContainerBuilder( |
||
111 | new PHPDIContainerBuilder(), |
||
112 | $definitionLoader, |
||
113 | getenv(Environment::SIMPLEX_ENV) |
||
114 | ); |
||
115 | |||
116 | if (getenv(Environment::COMPILE_CONTAINER)) { |
||
117 | $containerBuilder->enableCompilation($this->getCompiledContainerDirectory()); |
||
118 | } |
||
119 | return $containerBuilder; |
||
120 | } |
||
121 | } |
||
122 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.