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 Symfony\Component\Config\ConfigCache; |
20
|
|
|
use Symfony\Component\Config\FileLocator; |
21
|
|
|
use Symfony\Component\DependencyInjection\Container; |
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
23
|
|
|
use Symfony\Component\DependencyInjection\Dumper\DumperInterface; |
24
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
25
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
26
|
|
|
|
27
|
|
|
class Builder |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $cacheFileName; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Config |
36
|
|
|
*/ |
37
|
|
|
private $config; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ContainerInterface |
41
|
|
|
*/ |
42
|
|
|
private $container; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ContainerBuilder |
46
|
|
|
*/ |
47
|
|
|
private $containerBuilder; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var DumperInterface |
51
|
|
|
*/ |
52
|
|
|
private $dumper; |
53
|
|
|
|
54
|
|
|
public function __construct(Config $config) |
55
|
|
|
{ |
56
|
|
|
$this->config = $config; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
|
|
public function compile(): void |
63
|
|
|
{ |
64
|
|
|
$cachePath = $this->getCacheFileName(); |
65
|
|
|
$cache = new ConfigCache($cachePath, true); |
66
|
|
|
$env = getenv('DOTFILES_ENV'); |
67
|
|
|
|
68
|
|
|
if (!$cache->isFresh() || 'dev' === $env) { |
69
|
|
|
$builder = $this->getContainerBuilder(); |
70
|
|
|
$this->configureCoreServices($builder); |
71
|
|
|
$builder->addCompilerPass(new CommandPass()); |
72
|
|
|
$builder->addCompilerPass(new ListenerPass()); |
73
|
|
|
$builder->compile(); |
74
|
|
|
|
75
|
|
|
$dumper = $this->getDumper(); |
76
|
|
|
//file_put_contents($target,$dumper->dump(['class'=>'CachedContainer']), LOCK_EX); |
77
|
|
|
$cache->write($dumper->dump(array('class' => 'CachedContainer')), $builder->getResources()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getCacheFileName(): string |
85
|
|
|
{ |
86
|
|
|
return $this->config->get('dotfiles.cache_dir').'/container.php'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Container |
91
|
|
|
*/ |
92
|
|
|
public function getContainer() |
93
|
|
|
{ |
94
|
|
|
if (null === $this->container) { |
95
|
|
|
if (!class_exists('CachedContainer')) { |
96
|
|
|
include_once $this->getCacheFileName(); |
97
|
|
|
} |
98
|
|
|
$this->container = new \CachedContainer(); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->container; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return ContainerBuilder |
106
|
|
|
*/ |
107
|
|
|
public function getContainerBuilder(): ContainerBuilder |
108
|
|
|
{ |
109
|
|
|
if (null === $this->containerBuilder) { |
110
|
|
|
$this->containerBuilder = new ContainerBuilder(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->containerBuilder; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return DumperInterface |
118
|
|
|
*/ |
119
|
|
|
public function getDumper(): DumperInterface |
120
|
|
|
{ |
121
|
|
|
if (null === $this->dumper) { |
122
|
|
|
$this->dumper = new PhpDumper($this->containerBuilder); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->dumper; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $cacheFileName |
130
|
|
|
* |
131
|
|
|
* @return self |
132
|
|
|
*/ |
133
|
|
|
public function setCacheFileName(string $cacheFileName): self |
134
|
|
|
{ |
135
|
|
|
if (!is_dir($dir = dirname($cacheFileName))) { |
136
|
|
|
mkdir($dir, 0755, true); |
137
|
|
|
} |
138
|
|
|
$this->cacheFileName = $cacheFileName; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Config $config |
145
|
|
|
* |
146
|
|
|
* @return Builder |
147
|
|
|
*/ |
148
|
|
|
public function setConfig(Config $config): self |
149
|
|
|
{ |
150
|
|
|
$this->config = $config; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param ContainerBuilder $builder |
157
|
|
|
* |
158
|
|
|
* @return self |
159
|
|
|
*/ |
160
|
|
|
public function setContainerBuilder(ContainerBuilder $builder): self |
161
|
|
|
{ |
162
|
|
|
$this->containerBuilder = $builder; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param DumperInterface $dumper |
169
|
|
|
* |
170
|
|
|
* @return Builder |
171
|
|
|
*/ |
172
|
|
|
public function setDumper(DumperInterface $dumper): self |
173
|
|
|
{ |
174
|
|
|
$this->dumper = $dumper; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param ContainerBuilder $builder |
181
|
|
|
* |
182
|
|
|
* @throws \Exception |
183
|
|
|
*/ |
184
|
|
|
private function configureCoreServices(ContainerBuilder $builder): void |
185
|
|
|
{ |
186
|
|
|
$locator = new FileLocator(__DIR__.'/../Resources/config'); |
187
|
|
|
$loader = new YamlFileLoader($builder, $locator); |
188
|
|
|
$loader->load('services.yaml'); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|