1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the m1\vars library |
5
|
|
|
* |
6
|
|
|
* (c) m1 <[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
|
|
|
* @package m1/vars |
12
|
|
|
* @version 0.3.0 |
13
|
|
|
* @author Miles Croxford <[email protected]> |
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace M1\Vars; |
20
|
|
|
|
21
|
|
|
use M1\Vars\Cache\CacheProvider; |
22
|
|
|
use M1\Vars\Loader\LoaderProvider; |
23
|
|
|
use M1\Vars\Resource\AbstractResource; |
24
|
|
|
use M1\Vars\Resource\ResourceProvider; |
25
|
|
|
use M1\Vars\Resource\VariableResource; |
26
|
|
|
use M1\Vars\Traits\TransformerTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Vars core class |
30
|
|
|
* |
31
|
|
|
* @since 0.1.0 |
32
|
|
|
*/ |
33
|
|
|
class Vars extends AbstractResource |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Used for to* functions |
37
|
|
|
*/ |
38
|
|
|
use TransformerTrait; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The base path for the Vars config and cache folders |
42
|
|
|
* |
43
|
|
|
* @var string $base_path |
44
|
|
|
*/ |
45
|
|
|
private $base_path; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The cache object if the cache is wanted, else false |
49
|
|
|
* |
50
|
|
|
* @var \M1\Vars\Cache\CacheProvider $cache |
51
|
|
|
*/ |
52
|
|
|
public $cache; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The default options for Vars |
56
|
|
|
* |
57
|
|
|
* @var array $default_options |
58
|
|
|
*/ |
59
|
|
|
private $default_options = array( |
60
|
|
|
'base_path' => null, |
61
|
|
|
'cache' => true, |
62
|
|
|
'cache_path' => null, |
63
|
|
|
'cache_expire' => 300, // 5 minutes |
64
|
|
|
'loaders' => array('env', 'ini', 'json', 'php', 'toml', 'yaml', 'xml',) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The loaderProvider for Vars supplies the file loaders and the extensions that are supported |
69
|
|
|
* |
70
|
|
|
* @var \M1\Vars\Loader\LoaderProvider $loader |
71
|
|
|
*/ |
72
|
|
|
public $loader; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Have the base and cache paths been set |
76
|
|
|
* |
77
|
|
|
* @var bool $paths_loaded |
78
|
|
|
*/ |
79
|
|
|
private $paths_loaded = false; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The imported resources |
83
|
|
|
* |
84
|
|
|
* @var array $resources |
85
|
|
|
*/ |
86
|
|
|
private $resources = array(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* The words to be replaced in the config files |
90
|
|
|
* |
91
|
|
|
* @var array $variables |
92
|
|
|
*/ |
93
|
|
|
private $variables = array(); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Creates a new instance of Vars |
97
|
|
|
* |
98
|
|
|
* @param string|array $resource The main configuration resource |
99
|
|
|
* @param array $options The options being used for Vars |
100
|
|
|
*/ |
101
|
80 |
|
public function __construct($resource, $options = array()) |
102
|
|
|
{ |
103
|
80 |
|
$options = $this->parseOptions($options); |
104
|
80 |
|
$this->makeCache($options, $resource); |
105
|
79 |
|
$this->makePaths($options); |
106
|
|
|
|
107
|
78 |
|
if (!$this->cache->checkCache()) { |
108
|
78 |
|
$this->makeLoader($options); |
109
|
76 |
|
$this->makeVariables($options); |
110
|
|
|
|
111
|
75 |
|
$resource = new ResourceProvider($this, $resource); |
112
|
62 |
|
} |
113
|
|
|
|
114
|
62 |
|
if ($this->cache->isHit()) { |
115
|
3 |
|
$this->loadFromCache(); |
116
|
3 |
|
} else { |
117
|
62 |
|
$resource->mergeParentContent(); |
|
|
|
|
118
|
62 |
|
$this->content = $resource->getContent(); |
119
|
|
|
|
120
|
62 |
|
$this->cache->setTime(time()); |
121
|
62 |
|
$this->cache->makeCache($this); |
122
|
|
|
|
123
|
|
|
} |
124
|
62 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Parses the options so Vars can use them |
128
|
|
|
* |
129
|
|
|
* @param array $options The options being used for Vars |
130
|
|
|
* |
131
|
|
|
* @return array The parsed options |
132
|
|
|
*/ |
133
|
80 |
|
private function parseOptions(array $options) |
134
|
|
|
{ |
135
|
80 |
|
$parsed_options = array_merge($this->default_options, $options); |
136
|
80 |
|
$parsed_options['loaders'] = (isset($options['loaders'])) ? $options['loaders'] : $this->default_options['loaders']; |
137
|
80 |
|
return $parsed_options; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Makes the CacheProvider with the options |
142
|
|
|
* |
143
|
|
|
* @param array $options The options being used for Vars |
144
|
|
|
* @param array|string $resource The main configuration resource |
145
|
|
|
*/ |
146
|
80 |
|
private function makeCache($options, $resource) |
147
|
|
|
{ |
148
|
80 |
|
$cache = new CacheProvider($resource, $options); |
149
|
79 |
|
$this->cache = $cache; |
150
|
79 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
154
|
|
|
* base path has |
155
|
|
|
* |
156
|
|
|
* @param array $options The options being used for Vars |
157
|
|
|
*/ |
158
|
79 |
|
private function makePaths($options) |
159
|
|
|
{ |
160
|
79 |
|
$this->setBasePath($options['base_path']); |
161
|
|
|
|
162
|
78 |
|
if (is_null($options['cache_path']) && !is_null($options['base_path'])) { |
163
|
1 |
|
$this->cache->setPath($options['base_path']); |
164
|
1 |
|
$this->paths_loaded = true; |
165
|
1 |
|
} |
166
|
78 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Makes the LoaderProvider with the options |
170
|
|
|
* |
171
|
|
|
* @param array $options The options being used for Vars |
172
|
|
|
*/ |
173
|
78 |
|
private function makeLoader($options) |
174
|
|
|
{ |
175
|
78 |
|
$loader = new LoaderProvider($options, $this->default_options['loaders']); |
176
|
76 |
|
$this->loader = $loader; |
177
|
76 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets the replacement variables if the option has been set |
181
|
|
|
* |
182
|
|
|
* @param array|null $options The options being used for Vars |
183
|
|
|
*/ |
184
|
76 |
|
private function makeVariables($options) |
185
|
|
|
{ |
186
|
76 |
|
if (isset($options['variables'])) { |
187
|
4 |
|
$variables = new VariableResource($this, $options['variables']); |
188
|
|
|
|
189
|
3 |
|
$v = array(); |
190
|
|
|
|
191
|
3 |
|
foreach ($variables->getVariables() as $variable_key => $variable_value) { |
192
|
3 |
|
$v["%".$variable_key.'%'] = $variable_value; |
193
|
3 |
|
} |
194
|
|
|
|
195
|
3 |
|
$this->variables = $v; |
196
|
3 |
|
} |
197
|
75 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Loads the cached file into the current class |
201
|
|
|
*/ |
202
|
3 |
|
private function loadFromCache() |
203
|
|
|
{ |
204
|
3 |
|
$this->cache->load(); |
205
|
|
|
|
206
|
|
|
$passed_keys = array( |
207
|
3 |
|
'base_path', |
208
|
3 |
|
'content', |
209
|
3 |
|
'extensions', |
210
|
3 |
|
'loaders', |
211
|
3 |
|
'resources', |
212
|
3 |
|
'variables', |
213
|
3 |
|
); |
214
|
|
|
|
215
|
3 |
|
$loaded_vars = get_object_vars($this->cache->getLoadedVars()); |
216
|
|
|
|
217
|
3 |
|
foreach ($loaded_vars as $key => $value) { |
218
|
3 |
|
if (in_array($key, $passed_keys)) { |
219
|
3 |
|
$this->$key = $value; |
220
|
3 |
|
} |
221
|
3 |
|
} |
222
|
|
|
|
223
|
3 |
|
$this->cache->setTime($loaded_vars['cache']->getTime()); |
224
|
3 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Checks if the base and cache paths have been set, if not set then will use the $resource as the base path |
228
|
|
|
* |
229
|
|
|
* @param string $resource The resource to use to set the paths if they haven't been set |
230
|
|
|
*/ |
231
|
69 |
|
public function pathsLoadedCheck($resource) |
232
|
|
|
{ |
233
|
69 |
|
if (!$this->paths_loaded) { |
234
|
68 |
|
$base_path = $this->getBasePath(); |
235
|
|
|
|
236
|
68 |
|
if (!$base_path) { |
237
|
66 |
|
$file = pathinfo(realpath($resource)); |
238
|
66 |
|
$base_path = $file['dirname']; |
239
|
66 |
|
$this->setBasePath($base_path); |
240
|
66 |
|
} |
241
|
|
|
|
242
|
68 |
|
if ($this->cache->getProvide() && !$this->cache->getPath()) { |
243
|
2 |
|
$this->cache->setPath($base_path); |
244
|
2 |
|
} |
245
|
|
|
|
246
|
68 |
|
$this->paths_loaded = true; |
247
|
68 |
|
} |
248
|
69 |
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get the Vars base path |
252
|
|
|
* |
253
|
|
|
* @return string The Vars base path |
254
|
|
|
*/ |
255
|
69 |
|
public function getBasePath() |
256
|
|
|
{ |
257
|
69 |
|
return $this->base_path; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Set the Vars base path |
262
|
|
|
* |
263
|
|
|
* @param string $base_path The base path to set |
264
|
|
|
* |
265
|
|
|
* @throws \InvalidArgumentException If the base path does not exist or is not writable |
266
|
|
|
* |
267
|
|
|
* @return \M1\Vars\Vars |
268
|
|
|
*/ |
269
|
79 |
View Code Duplication |
public function setBasePath($base_path) |
|
|
|
|
270
|
|
|
{ |
271
|
79 |
|
if (is_null($base_path)) { |
272
|
75 |
|
return; |
273
|
|
|
} |
274
|
|
|
|
275
|
70 |
|
if (!is_dir($base_path)) { |
276
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
277
|
1 |
|
"'%s' base path does not exist or is not writable", |
278
|
|
|
$base_path |
279
|
1 |
|
)); |
280
|
|
|
} |
281
|
|
|
|
282
|
69 |
|
$this->base_path = realpath($base_path); |
283
|
69 |
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Adds a resource to $this->resources |
288
|
|
|
* |
289
|
|
|
* @param string $resource Resource to add to the stack |
290
|
|
|
* |
291
|
|
|
* @return int The position of the added resource |
292
|
|
|
*/ |
293
|
69 |
|
public function addResource($resource) |
294
|
|
|
{ |
295
|
69 |
|
$r = realpath($resource); |
296
|
69 |
|
$pos = count($this->resources); |
297
|
69 |
|
$this->resources[$pos] = $r; |
298
|
69 |
|
return $pos; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Updates the string resource with the FileResource |
303
|
|
|
* |
304
|
|
|
* @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
305
|
|
|
* @param int $pos The position of the string resource |
306
|
|
|
* |
307
|
|
|
* @return \M1\Vars\Vars |
308
|
|
|
*/ |
309
|
60 |
|
public function updateResource($resource, $pos) |
310
|
|
|
{ |
311
|
60 |
|
$this->resources[$pos] = $resource; |
312
|
60 |
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
317
|
|
|
* |
318
|
|
|
* @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
319
|
|
|
* |
320
|
|
|
* @return bool Has resource already been imported |
321
|
|
|
*/ |
322
|
69 |
|
public function resourceImported($resource) |
323
|
|
|
{ |
324
|
69 |
|
$resource = realpath($resource); |
325
|
69 |
|
foreach ($this->getResources() as $r) { |
326
|
45 |
|
if ((is_a($r, 'M1\Vars\Resource\FileResource') && $resource === $r->getFile()) || |
327
|
45 |
|
(is_string($r) && $resource === $r)) { |
328
|
1 |
|
return true; |
329
|
|
|
} |
330
|
69 |
|
} |
331
|
69 |
|
return false; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Searches the resource stack for a certain resource |
336
|
|
|
* |
337
|
|
|
* @param string $resource The resource to search for |
338
|
|
|
* |
339
|
|
|
* @return bool Returns the resource if found |
340
|
|
|
*/ |
341
|
3 |
|
public function getResource($resource) |
342
|
|
|
{ |
343
|
3 |
|
foreach ($this->getResources() as $r) { |
344
|
3 |
|
if ($resource === $r->getFilename()) { |
345
|
2 |
|
return $r; |
346
|
|
|
} |
347
|
3 |
|
} |
348
|
|
|
|
349
|
1 |
|
return false; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Returns the imported resources |
354
|
|
|
* |
355
|
|
|
* @return array The Vars imported resources |
356
|
|
|
*/ |
357
|
69 |
|
public function getResources() |
358
|
|
|
{ |
359
|
69 |
|
return $this->resources; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Returns the Vars replacement variables |
364
|
|
|
* |
365
|
|
|
* @return array The Vars replacement variables |
366
|
|
|
*/ |
367
|
52 |
|
public function getVariables() |
368
|
|
|
{ |
369
|
52 |
|
return $this->variables; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Returns the CacheProvider if set, if not return false |
374
|
|
|
* |
375
|
|
|
* @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
376
|
|
|
*/ |
377
|
7 |
|
public function getCache() |
378
|
|
|
{ |
379
|
7 |
|
return $this->cache; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: