|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Simplex package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Freddie Frantzen <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Simplex; |
|
12
|
|
|
|
|
13
|
|
|
use DI\Container; |
|
14
|
|
|
use DI\ContainerBuilder as PHPDIContainerBuilder; |
|
15
|
|
|
use Simplex\DefinitionLoader\DefinitionLoader; |
|
16
|
|
|
|
|
17
|
|
|
class ContainerBuilder |
|
18
|
|
|
{ |
|
19
|
|
|
const COMPILED_CONTAINER_DIRECTORY_NAME = 'container'; |
|
20
|
|
|
const CONTAINER_CLASS_SUFFIX = 'Container'; |
|
21
|
|
|
const CONTAINER_CLASS_FILE_EXTENSION = '.php'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var \SplFileInfo */ |
|
24
|
|
|
private $configDirectory; |
|
25
|
|
|
|
|
26
|
|
|
/** @var PHPDIContainerBuilder */ |
|
27
|
|
|
private $phpDiContainerBuilder; |
|
28
|
|
|
|
|
29
|
|
|
/** @var DefinitionLoader */ |
|
30
|
|
|
private $definitionLoader; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $environment; |
|
34
|
|
|
|
|
35
|
|
|
/** @var bool */ |
|
36
|
|
|
private $compileContainer = false; |
|
37
|
|
|
|
|
38
|
|
|
/** @var \SplFileInfo */ |
|
39
|
|
|
private $compiledContainerDirectory; |
|
40
|
|
|
|
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
private $compiledContainerClassName; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct( |
|
45
|
|
|
\SplFileInfo $configDirectory, |
|
46
|
|
|
PHPDIContainerBuilder $phpDiContainerBuilder, |
|
47
|
|
|
DefinitionLoader $definitionLoader, |
|
48
|
|
|
string $environment |
|
49
|
|
|
) { |
|
50
|
|
|
if (!$configDirectory->isDir() || !$configDirectory->isReadable()) { |
|
51
|
|
|
throw new \RuntimeException('Invalid config directory path ' . $configDirectory->getRealPath()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->configDirectory = $configDirectory; |
|
55
|
|
|
$this->phpDiContainerBuilder = $phpDiContainerBuilder; |
|
56
|
|
|
$this->definitionLoader = $definitionLoader; |
|
57
|
|
|
$this->environment = $environment; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function enableCompilation(\SplFileInfo $compiledContainerDirectory): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->compileContainer = true; |
|
63
|
|
|
$this->compiledContainerDirectory = $compiledContainerDirectory; |
|
64
|
|
|
$this->compiledContainerClassName = ucfirst($this->environment) . self::CONTAINER_CLASS_SUFFIX; |
|
65
|
|
|
|
|
66
|
|
|
$this->phpDiContainerBuilder->enableCompilation( |
|
67
|
|
|
$this->compiledContainerDirectory->getPathname(), |
|
68
|
|
|
$this->compiledContainerClassName |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function build(): Container |
|
73
|
|
|
{ |
|
74
|
|
|
if (!$this->compileContainer) { |
|
75
|
|
|
$this->definitionLoader->load($this->phpDiContainerBuilder); |
|
76
|
|
|
return $this->phpDiContainerBuilder->build(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->buildWithCompilation(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function buildWithCompilation(): Container |
|
83
|
|
|
{ |
|
84
|
|
|
$compiledContainerClassFile = $this->compiledContainerDirectory->getPathname() |
|
85
|
|
|
. DIRECTORY_SEPARATOR |
|
86
|
|
|
. $this->compiledContainerClassName |
|
87
|
|
|
. self::CONTAINER_CLASS_FILE_EXTENSION; |
|
88
|
|
|
|
|
89
|
|
|
if (!file_exists($compiledContainerClassFile)) { |
|
90
|
|
|
$this->definitionLoader->load($this->phpDiContainerBuilder); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->phpDiContainerBuilder->build(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|