1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the dotfiles project. |
7
|
|
|
* |
8
|
|
|
* (c) Anthonius Munthi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Dotfiles\Core\DI; |
15
|
|
|
|
16
|
|
|
use Dotfiles\Core\Config\Config; |
17
|
|
|
use Dotfiles\Core\DI\Compiler\CommandPass; |
18
|
|
|
use Dotfiles\Core\DI\Compiler\ListenerPass; |
19
|
|
|
use Dotfiles\Core\Util\Toolkit; |
20
|
|
|
use Symfony\Component\Config\ConfigCache; |
21
|
|
|
use Symfony\Component\Config\FileLocator; |
22
|
|
|
use Symfony\Component\DependencyInjection\Container; |
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
24
|
|
|
use Symfony\Component\DependencyInjection\Dumper\DumperInterface; |
25
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
26
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
27
|
|
|
|
28
|
|
|
class Builder |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Config |
32
|
|
|
*/ |
33
|
|
|
private $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ContainerInterface |
37
|
|
|
*/ |
38
|
|
|
private $container; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ContainerBuilder |
42
|
|
|
*/ |
43
|
|
|
private $containerBuilder; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var DumperInterface |
47
|
|
|
*/ |
48
|
|
|
private $dumper; |
49
|
|
|
|
50
|
|
|
public function __construct(Config $config) |
51
|
|
|
{ |
52
|
|
|
$this->config = $config; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
|
|
public function compile(): void |
59
|
|
|
{ |
60
|
|
|
$cachePath = $this->getCacheFileName(); |
61
|
|
|
$cache = new ConfigCache($cachePath, true); |
62
|
|
|
$env = getenv('DOTFILES_ENV'); |
63
|
|
|
|
64
|
|
|
if (!$cache->isFresh() || 'dev' === $env) { |
65
|
|
|
$builder = $this->getContainerBuilder(); |
66
|
|
|
$this->configureCoreServices($builder); |
67
|
|
|
$builder->addCompilerPass(new CommandPass()); |
68
|
|
|
$builder->addCompilerPass(new ListenerPass()); |
69
|
|
|
$builder->compile(); |
70
|
|
|
|
71
|
|
|
$dumper = $this->getDumper(); |
72
|
|
|
//file_put_contents($target,$dumper->dump(['class'=>'CachedContainer']), LOCK_EX); |
73
|
|
|
$cache->write($dumper->dump(array('class' => 'CachedContainer')), $builder->getResources()); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getCacheFileName() |
78
|
|
|
{ |
79
|
|
|
return Toolkit::getCachePathPrefix().'/container.php'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return Container |
84
|
|
|
*/ |
85
|
|
|
public function getContainer() |
86
|
|
|
{ |
87
|
|
|
if (null === $this->container) { |
88
|
|
|
if (!class_exists('CachedContainer')) { |
89
|
|
|
include_once $this->getCacheFileName(); |
90
|
|
|
} |
91
|
|
|
$this->container = new \CachedContainer(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->container; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return ContainerBuilder |
99
|
|
|
*/ |
100
|
|
|
public function getContainerBuilder(): ContainerBuilder |
101
|
|
|
{ |
102
|
|
|
if (null === $this->containerBuilder) { |
103
|
|
|
$this->containerBuilder = new ContainerBuilder(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->containerBuilder; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return DumperInterface |
111
|
|
|
*/ |
112
|
|
|
public function getDumper(): DumperInterface |
113
|
|
|
{ |
114
|
|
|
if (null === $this->dumper) { |
115
|
|
|
$this->dumper = new PhpDumper($this->containerBuilder); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->dumper; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Config $config |
123
|
|
|
* |
124
|
|
|
* @return Builder |
125
|
|
|
*/ |
126
|
|
|
public function setConfig(Config $config): self |
127
|
|
|
{ |
128
|
|
|
$this->config = $config; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param ContainerBuilder $builder |
135
|
|
|
* |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
|
|
public function setContainerBuilder(ContainerBuilder $builder): self |
139
|
|
|
{ |
140
|
|
|
$this->containerBuilder = $builder; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param DumperInterface $dumper |
147
|
|
|
* |
148
|
|
|
* @return Builder |
149
|
|
|
*/ |
150
|
|
|
public function setDumper(DumperInterface $dumper): self |
151
|
|
|
{ |
152
|
|
|
$this->dumper = $dumper; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param ContainerBuilder $builder |
159
|
|
|
* |
160
|
|
|
* @throws \Exception |
161
|
|
|
*/ |
162
|
|
|
private function configureCoreServices(ContainerBuilder $builder): void |
163
|
|
|
{ |
164
|
|
|
$locator = new FileLocator(__DIR__.'/../Resources/config'); |
165
|
|
|
$loader = new YamlFileLoader($builder, $locator); |
166
|
|
|
$loader->load('services.yaml'); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|