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.1.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\Resource\AbstractResource; |
23
|
|
|
use M1\Vars\Resource\ResourceProvider; |
24
|
|
|
use M1\Vars\Resource\VariableResource; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Vars core class |
28
|
|
|
* |
29
|
|
|
* @since 0.1.0 |
30
|
|
|
*/ |
31
|
|
|
class Vars extends AbstractResource |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The base path for the Vars config and cache folders |
35
|
|
|
* |
36
|
|
|
* @var string $base_path |
37
|
|
|
*/ |
38
|
|
|
private $base_path; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The cache object if the cache is wanted, else false |
42
|
|
|
* |
43
|
|
|
* @var \M1\Vars\Cache\CacheProvider $cache |
44
|
|
|
*/ |
45
|
|
|
public $cache; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The available extensions |
49
|
|
|
* |
50
|
|
|
* @var array $extensions |
51
|
|
|
*/ |
52
|
|
|
private $extensions = array(); |
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('ini', 'json', 'php', 'toml', 'yaml', 'xml',) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The available loaders |
69
|
|
|
* |
70
|
|
|
* @var array $loaders |
71
|
|
|
*/ |
72
|
|
|
private $loaders = array(); |
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
|
63 |
|
public function __construct($resource, $options = array()) |
102
|
|
|
{ |
103
|
63 |
|
$options = $this->parseOptions($options); |
104
|
63 |
|
$this->makeCache($options, $resource); |
105
|
62 |
|
$this->makePaths($options); |
106
|
|
|
|
107
|
61 |
|
if (!$this->cache->checkCache()) { |
108
|
61 |
|
$this->makeLoaders($options); |
109
|
59 |
|
$this->makeVariables($options); |
110
|
|
|
|
111
|
58 |
|
$resource = new ResourceProvider($this, $resource); |
112
|
47 |
|
} |
113
|
|
|
|
114
|
47 |
|
if ($this->cache->isHit()) { |
115
|
2 |
|
$this->loadFromCache(); |
116
|
2 |
|
} else { |
117
|
47 |
|
$resource->mergeParentContent(); |
|
|
|
|
118
|
47 |
|
$this->content = $resource->getContent(); |
119
|
|
|
|
120
|
47 |
|
$this->cache->setTime(time()); |
121
|
47 |
|
$this->cache->makeCache($this); |
122
|
|
|
|
123
|
|
|
} |
124
|
47 |
|
} |
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
|
63 |
|
private function parseOptions(array $options) |
134
|
|
|
{ |
135
|
63 |
|
if (!$options) { |
|
|
|
|
136
|
1 |
|
$parsed_options = $this->default_options; |
137
|
1 |
|
} else { |
138
|
62 |
|
$parsed_options = array_merge($this->default_options, $options); |
139
|
|
|
|
140
|
62 |
|
if (isset($options['loaders'])) { |
141
|
8 |
|
if (is_array($options['loaders']) && !empty($options['loaders'])) { |
142
|
3 |
|
$loaders = $options['loaders']; |
143
|
8 |
|
} elseif (is_string($options['loaders'])) { |
144
|
4 |
|
$loaders[] = $options['loaders']; |
|
|
|
|
145
|
4 |
|
} else { |
146
|
1 |
|
$loaders = $this->default_options['loaders']; |
147
|
|
|
} |
148
|
|
|
|
149
|
8 |
|
} else { |
150
|
54 |
|
$loaders = $this->default_options['loaders']; |
151
|
|
|
} |
152
|
|
|
|
153
|
62 |
|
$parsed_options['loaders'] = $loaders; |
154
|
|
|
} |
155
|
|
|
|
156
|
63 |
|
return $parsed_options; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Makes the CacheProvider with the options |
161
|
|
|
* |
162
|
|
|
* @param array $options The options being used for Vars |
163
|
|
|
* @param array|string $resource The main configuration resource |
164
|
|
|
*/ |
165
|
63 |
|
private function makeCache($options, $resource) |
166
|
|
|
{ |
167
|
63 |
|
$cache = new CacheProvider($resource, array_merge($this->default_options, $options)); |
168
|
62 |
|
$this->cache = $cache; |
169
|
62 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
173
|
|
|
* base path has |
174
|
|
|
* |
175
|
|
|
* @param array $options The options being used for Vars |
176
|
|
|
*/ |
177
|
62 |
|
private function makePaths($options) |
178
|
|
|
{ |
179
|
62 |
|
$this->setBasePath($options['base_path']); |
180
|
|
|
|
181
|
61 |
|
if (is_null($options['cache_path']) && !is_null($options['base_path'])) { |
182
|
1 |
|
$this->cache->setPath($options['base_path']); |
183
|
1 |
|
$this->paths_loaded = true; |
184
|
1 |
|
} |
185
|
61 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get loaders and make extensions for the loaders |
189
|
|
|
* |
190
|
|
|
* @param array|null $options The options being used for Vars |
191
|
|
|
* |
192
|
|
|
*/ |
193
|
61 |
|
private function makeLoaders($options) |
194
|
|
|
{ |
195
|
61 |
|
$parsed_loaders = array(); |
196
|
|
|
|
197
|
61 |
|
foreach ($options['loaders'] as $loader) { |
198
|
61 |
|
if ($loader === 'default') { |
199
|
2 |
|
$parsed_loaders = array_merge($parsed_loaders, $this->default_options['loaders']); |
200
|
2 |
|
} else { |
201
|
60 |
|
$parsed_loaders[] = $loader; |
202
|
|
|
} |
203
|
61 |
|
} |
204
|
|
|
|
205
|
61 |
|
$parsed_loaders = array_unique($parsed_loaders); |
206
|
|
|
|
207
|
61 |
|
$this->loaders = $this->makeNameSpaceLoaders($parsed_loaders); |
208
|
60 |
|
$this->extensions = $this->makeExtensions($this->loaders); |
209
|
59 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Makes namespace loaders from loader strings |
213
|
|
|
* |
214
|
|
|
* @param array $loaders The options being used for Vars |
215
|
|
|
* |
216
|
|
|
* @throws \InvalidArgumentException If a loader from options isn't found |
217
|
|
|
* @throws \InvalidArgumentException If no loaders were loaded |
218
|
|
|
* |
219
|
|
|
* @return array The namespace loaders |
220
|
|
|
*/ |
221
|
61 |
|
private function makeNameSpaceLoaders($loaders) |
222
|
|
|
{ |
223
|
61 |
|
$parsed_loaders = array(); |
224
|
|
|
|
225
|
61 |
|
foreach ($loaders as $loader) { |
226
|
61 |
|
if (in_array($loader, $this->default_options['loaders'])) { |
227
|
58 |
|
$loader = sprintf('%s\Loader\%sLoader', __NAMESPACE__, ucfirst(strtolower($loader))); |
228
|
58 |
|
} |
229
|
|
|
|
230
|
61 |
|
if (!class_exists($loader)) { |
231
|
1 |
|
throw new \InvalidArgumentException(sprintf("'%s' loader class does not exist", $loader)); |
232
|
|
|
} |
233
|
|
|
|
234
|
60 |
|
$parsed_loaders[] = $loader; |
235
|
60 |
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
60 |
|
if (empty($parsed_loaders)) { |
239
|
|
|
throw new \InvalidArgumentException('No loaders were loaded'); |
240
|
|
|
} |
241
|
|
|
|
242
|
60 |
|
return $parsed_loaders; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Sets the replacement variables if the option has been set |
247
|
|
|
* |
248
|
|
|
* @param array|null $options The options being used for Vars |
249
|
|
|
*/ |
250
|
59 |
|
private function makeVariables($options) |
251
|
|
|
{ |
252
|
59 |
|
if (isset($options['variables'])) { |
253
|
4 |
|
$variables = new VariableResource($this, $options['variables']); |
254
|
|
|
|
255
|
3 |
|
$v = array(); |
256
|
|
|
|
257
|
3 |
|
foreach ($variables->getVariables() as $variable_key => $variable_value) { |
258
|
3 |
|
$v["%".$variable_key.'%'] = $variable_value; |
259
|
3 |
|
} |
260
|
|
|
|
261
|
3 |
|
$this->variables = $v; |
262
|
3 |
|
} |
263
|
58 |
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Loads the cached file into the current class |
267
|
|
|
*/ |
268
|
2 |
|
private function loadFromCache() |
269
|
|
|
{ |
270
|
2 |
|
$this->cache->load(); |
271
|
|
|
|
272
|
|
|
$passed_keys = array( |
273
|
2 |
|
'base_path', |
274
|
2 |
|
'content', |
275
|
2 |
|
'extensions', |
276
|
2 |
|
'loaders', |
277
|
2 |
|
'resources', |
278
|
2 |
|
'variables', |
279
|
2 |
|
); |
280
|
|
|
|
281
|
2 |
|
$loaded_vars = get_object_vars($this->cache->getLoadedVars()); |
282
|
|
|
|
283
|
2 |
|
foreach ($loaded_vars as $key => $value) { |
284
|
2 |
|
if (in_array($key, $passed_keys)) { |
285
|
2 |
|
$this->$key = $value; |
286
|
2 |
|
} |
287
|
2 |
|
} |
288
|
|
|
|
289
|
2 |
|
$this->cache->setTime($loaded_vars['cache']->getTime()); |
290
|
2 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Get and make extensions for loaders made from makeLoaders() |
294
|
|
|
* |
295
|
|
|
* @see \M1\Vars\Vars::makeLoaders() \M1\Vars\Vars::makeLoaders() |
296
|
|
|
* |
297
|
|
|
* @param array $loaders File loaders |
298
|
|
|
* |
299
|
|
|
* @throws \RuntimeException If no loader extensions were found |
300
|
|
|
* |
301
|
|
|
* @return array File loader supported extensions |
302
|
|
|
*/ |
303
|
60 |
|
private function makeExtensions(array $loaders) |
304
|
|
|
{ |
305
|
60 |
|
$extensions = array(); |
306
|
|
|
|
307
|
60 |
|
foreach ($loaders as $loader) { |
308
|
60 |
|
$extensions = array_merge($extensions, $loader::$supported); |
309
|
60 |
|
} |
310
|
|
|
|
311
|
60 |
|
if (empty($extensions)) { |
312
|
1 |
|
throw new \RuntimeException('No loader extensions were found'); |
313
|
|
|
} |
314
|
|
|
|
315
|
59 |
|
return $extensions; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Checks if the base and cache paths have been set, if not set then will use the $resource as the base path |
320
|
|
|
* |
321
|
|
|
* @param string $resource The resource to use to set the paths if they haven't been set |
322
|
|
|
*/ |
323
|
53 |
|
public function pathsLoadedCheck($resource) |
324
|
|
|
{ |
325
|
53 |
|
if (!$this->paths_loaded) { |
326
|
52 |
|
$base_path = $this->getBasePath(); |
327
|
|
|
|
328
|
52 |
|
if (!$base_path) { |
329
|
50 |
|
$file = pathinfo(realpath($resource)); |
330
|
50 |
|
$base_path = $file['dirname']; |
331
|
50 |
|
$this->setBasePath($base_path); |
332
|
50 |
|
} |
333
|
|
|
|
334
|
52 |
|
if ($this->cache->getProvide() && !$this->cache->getPath()) { |
335
|
|
|
$this->cache->setPath($base_path); |
336
|
|
|
} |
337
|
|
|
|
338
|
52 |
|
$this->paths_loaded = true; |
339
|
52 |
|
} |
340
|
53 |
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get the Vars file loaders |
344
|
|
|
* |
345
|
|
|
* @return array The Vars file loaders |
346
|
|
|
*/ |
347
|
53 |
|
public function getLoaders() |
348
|
|
|
{ |
349
|
53 |
|
return $this->loaders; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Get the Vars file loaders extensions |
354
|
|
|
* |
355
|
|
|
* @return array The Vars file loader extensions |
356
|
|
|
*/ |
357
|
4 |
|
public function getExtensions() |
358
|
|
|
{ |
359
|
4 |
|
return $this->extensions; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Get the Vars base path |
364
|
|
|
* |
365
|
|
|
* @return string The Vars base path |
366
|
|
|
*/ |
367
|
53 |
|
public function getBasePath() |
368
|
|
|
{ |
369
|
53 |
|
return $this->base_path; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Set the Vars base path |
374
|
|
|
* |
375
|
|
|
* @param string $base_path The base path to set |
376
|
|
|
* |
377
|
|
|
* @throws \InvalidArgumentException If the base path does not exist or is not writable |
378
|
|
|
* |
379
|
|
|
* @return \M1\Vars\Vars |
380
|
|
|
*/ |
381
|
62 |
View Code Duplication |
public function setBasePath($base_path) |
|
|
|
|
382
|
|
|
{ |
383
|
62 |
|
if (is_null($base_path)) { |
384
|
58 |
|
return; |
385
|
|
|
} |
386
|
|
|
|
387
|
54 |
|
if (!is_dir($base_path)) { |
388
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
389
|
1 |
|
"'%s' base path does not exist or is not writable", |
390
|
|
|
$base_path |
391
|
1 |
|
)); |
392
|
|
|
} |
393
|
|
|
|
394
|
53 |
|
$this->base_path = realpath($base_path); |
395
|
53 |
|
return $this; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Adds a resource to $this->resources |
400
|
|
|
* |
401
|
|
|
* @param string $resource Resource to add to the stack |
402
|
|
|
* |
403
|
|
|
* @return int The position of the added resource |
404
|
|
|
*/ |
405
|
53 |
|
public function addResource($resource) |
406
|
|
|
{ |
407
|
53 |
|
$r = realpath($resource); |
408
|
53 |
|
$pos = count($this->resources); |
409
|
53 |
|
$this->resources[$pos] = $r; |
410
|
53 |
|
return $pos; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Updates the string resource with the FileResource |
415
|
|
|
* |
416
|
|
|
* @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
417
|
|
|
* @param int $pos The position of the string resource |
418
|
|
|
* |
419
|
|
|
* @return \M1\Vars\Vars |
420
|
|
|
*/ |
421
|
46 |
|
public function updateResource($resource, $pos) |
422
|
|
|
{ |
423
|
46 |
|
$this->resources[$pos] = $resource; |
424
|
46 |
|
return $this; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
429
|
|
|
* |
430
|
|
|
* @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
431
|
|
|
* |
432
|
|
|
* @return bool Has resource already been imported |
433
|
|
|
*/ |
434
|
53 |
|
public function resourceImported($resource) |
435
|
|
|
{ |
436
|
53 |
|
$resource = realpath($resource); |
437
|
53 |
|
foreach ($this->getResources() as $r) { |
438
|
36 |
|
if ((is_a($r, 'M1\Vars\Resource\FileResource') && $resource === $r->getFile()) || |
439
|
36 |
|
(is_string($r) && $resource === $r)) { |
440
|
1 |
|
return true; |
441
|
|
|
} |
442
|
53 |
|
} |
443
|
53 |
|
return false; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Searches the resource stack for a certain resource |
448
|
|
|
* |
449
|
|
|
* @param string $resource The resource to search for |
450
|
|
|
* |
451
|
|
|
* @return bool Returns the resource if found |
452
|
|
|
*/ |
453
|
3 |
|
public function getResource($resource) |
454
|
|
|
{ |
455
|
3 |
|
foreach ($this->getResources() as $r) { |
456
|
3 |
|
if ($resource === $r->getFilename()) { |
457
|
2 |
|
return $r; |
458
|
|
|
} |
459
|
3 |
|
} |
460
|
|
|
|
461
|
1 |
|
return false; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Returns the imported resources |
466
|
|
|
* |
467
|
|
|
* @return array The Vars imported resources |
468
|
|
|
*/ |
469
|
53 |
|
public function getResources() |
470
|
|
|
{ |
471
|
53 |
|
return $this->resources; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Returns the Vars replacement variables |
476
|
|
|
* |
477
|
|
|
* @return array The Vars replacement variables |
478
|
|
|
*/ |
479
|
42 |
|
public function getVariables() |
480
|
|
|
{ |
481
|
42 |
|
return $this->variables; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Returns the CacheProvider if set, if not return false |
486
|
|
|
* |
487
|
|
|
* @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
488
|
|
|
*/ |
489
|
5 |
|
public function getCache() |
490
|
|
|
{ |
491
|
5 |
|
return $this->cache; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: