1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2015 Matthew Loberg |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mlo\FileLoader; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Config\ConfigCache; |
10
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
11
|
|
|
use Symfony\Component\Config\Definition\Processor; |
12
|
|
|
use Symfony\Component\Config\FileLocator; |
13
|
|
|
use Symfony\Component\Config\Loader\DelegatingLoader; |
14
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
15
|
|
|
use Symfony\Component\Config\Loader\LoaderResolver; |
16
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* FileLoader |
20
|
|
|
* |
21
|
|
|
* @author Matthew Loberg <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class FileLoader |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $cacheDirectory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $directories; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var LoaderInterface[]|array |
37
|
|
|
*/ |
38
|
|
|
private $loaders = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $debug; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param string $cacheDirectory |
49
|
|
|
* @param array|string $directories |
50
|
|
|
* @param LoaderInterface[]|array $loaders |
51
|
|
|
* @param bool $debug |
52
|
|
|
*/ |
53
|
|
|
public function __construct($cacheDirectory, $directories = null, array $loaders = [], $debug = false) |
54
|
|
|
{ |
55
|
|
|
$this->cacheDirectory = $cacheDirectory; |
56
|
|
|
$this->directories = (array) $directories; |
57
|
|
|
$this->debug = $debug; |
58
|
|
|
|
59
|
|
|
foreach ($loaders as $loader) { |
60
|
|
|
$this->addLoader($loader); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get cache directory |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getCacheDirectory() |
70
|
|
|
{ |
71
|
|
|
return $this->cacheDirectory; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set cache directory |
76
|
|
|
* |
77
|
|
|
* @param string $cacheDirectory |
78
|
|
|
* |
79
|
|
|
* @return FileLoader |
80
|
|
|
*/ |
81
|
|
|
public function setCacheDirectory($cacheDirectory) |
82
|
|
|
{ |
83
|
|
|
$this->cacheDirectory = $cacheDirectory; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get Debug |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function isDebug() |
94
|
|
|
{ |
95
|
|
|
return $this->debug; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set Debug |
100
|
|
|
* |
101
|
|
|
* @param bool $debug |
102
|
|
|
* |
103
|
|
|
* @return FileLoader |
104
|
|
|
*/ |
105
|
|
|
public function setDebug($debug) |
106
|
|
|
{ |
107
|
|
|
$this->debug = $debug; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get directories |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getDirectories() |
118
|
|
|
{ |
119
|
|
|
return $this->directories; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set directories |
124
|
|
|
* |
125
|
|
|
* @param array $directories |
126
|
|
|
* |
127
|
|
|
* @return FileLoader |
128
|
|
|
*/ |
129
|
|
|
public function setDirectories(array $directories) |
130
|
|
|
{ |
131
|
|
|
$this->directories = $directories; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Add directory |
138
|
|
|
* |
139
|
|
|
* @param string $directory |
140
|
|
|
* |
141
|
|
|
* @return FileLoader |
142
|
|
|
*/ |
143
|
|
|
public function addDirectory($directory) |
144
|
|
|
{ |
145
|
|
|
if (!in_array($directory, $this->directories)) { |
146
|
|
|
$this->directories[] = $directory; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get Loaders |
154
|
|
|
* |
155
|
|
|
* @return LoaderInterface[]|array |
156
|
|
|
*/ |
157
|
|
|
public function getLoaders() |
158
|
|
|
{ |
159
|
|
|
return $this->loaders; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Add loader |
164
|
|
|
* |
165
|
|
|
* @param LoaderInterface $loader |
166
|
|
|
* |
167
|
|
|
* @return FileLoader |
168
|
|
|
*/ |
169
|
|
|
public function addLoader(LoaderInterface $loader) |
170
|
|
|
{ |
171
|
|
|
$this->loaders[] = $loader; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Load from file |
178
|
|
|
* |
179
|
|
|
* @param string $fileName |
180
|
|
|
* @param bool $refresh |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
* @throws \Symfony\Component\Config\Exception\FileLoaderLoadException |
184
|
|
|
*/ |
185
|
|
|
public function load($fileName, $refresh = false) |
186
|
|
|
{ |
187
|
|
|
$cachePath = $this->getCacheDirectory() . DIRECTORY_SEPARATOR . $fileName . '.php'; |
188
|
|
|
$cache = new ConfigCache($cachePath, $this->debug); |
189
|
|
|
|
190
|
|
|
if ($refresh || !$cache->isFresh()) { |
191
|
|
|
$resolver = new LoaderResolver($this->loaders); |
192
|
|
|
$loader = new DelegatingLoader($resolver); |
193
|
|
|
$locator = new FileLocator($this->getDirectories()); |
194
|
|
|
|
195
|
|
|
$filePath = $locator->locate($fileName); |
196
|
|
|
$values = $loader->load($filePath); |
197
|
|
|
$resource = new FileResource($filePath); |
|
|
|
|
198
|
|
|
|
199
|
|
|
$cacheValue = sprintf("<?php return %s;", var_export($values, true)); |
200
|
|
|
|
201
|
|
|
$cache->write($cacheValue, [$resource]); |
202
|
|
|
} else { |
203
|
|
|
$values = require($cachePath); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $values; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Load from file and apply configuration |
211
|
|
|
* |
212
|
|
|
* @param string $fileName |
213
|
|
|
* @param ConfigurationInterface $configuration |
214
|
|
|
* @param bool $refresh |
215
|
|
|
* |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
|
|
public function loadWithConfiguration($fileName, ConfigurationInterface $configuration, $refresh = false) |
219
|
|
|
{ |
220
|
|
|
$tree = $configuration->getConfigTreeBuilder()->buildTree(); |
221
|
|
|
$name = $tree->getName(); |
222
|
|
|
|
223
|
|
|
$cachePath = $this->getCacheDirectory() . DIRECTORY_SEPARATOR . $fileName . '.config.' . $name . '.php'; |
224
|
|
|
$cache = new ConfigCache($cachePath, $this->debug); |
225
|
|
|
|
226
|
|
|
if ($refresh || !$cache->isFresh()) { |
227
|
|
|
$reflection = new \ReflectionClass(get_class($configuration)); |
228
|
|
|
$resource = new FileResource($reflection->getFileName()); |
229
|
|
|
|
230
|
|
|
$processor = new Processor(); |
231
|
|
|
$config = $this->load($fileName, $refresh); |
232
|
|
|
$values = $processor->process($tree, $config); |
233
|
|
|
|
234
|
|
|
$cacheValue = sprintf("<?php return %s;", var_export($values, true)); |
235
|
|
|
|
236
|
|
|
$cache->write($cacheValue, [$resource]); |
237
|
|
|
} else { |
238
|
|
|
$values = require($cachePath); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $values; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.