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\PathTrait; |
27
|
|
|
use M1\Vars\Traits\TransformerTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Vars core class |
31
|
|
|
* |
32
|
|
|
* @since 0.1.0 |
33
|
|
|
*/ |
34
|
|
|
class Vars extends AbstractResource |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Used for path functions and variables |
38
|
|
|
*/ |
39
|
|
|
use PathTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Used for to* functions |
43
|
|
|
*/ |
44
|
|
|
use TransformerTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The cache object if the cache is wanted, else false |
48
|
|
|
* |
49
|
|
|
* @var \M1\Vars\Cache\CacheProvider $cache |
50
|
|
|
*/ |
51
|
|
|
public $cache; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The default options for Vars |
55
|
|
|
* |
56
|
|
|
* @var array $default_options |
57
|
|
|
*/ |
58
|
|
|
private $default_options = array( |
59
|
|
|
'path' => null, |
60
|
|
|
'cache' => true, |
61
|
|
|
'cache_path' => null, |
62
|
|
|
'cache_expire' => 300, // 5 minutes |
63
|
|
|
'loaders' => array('env', 'ini', 'json', 'php', 'toml', 'yaml', 'xml',) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The loaderProvider for Vars supplies the file loaders and the extensions that are supported |
68
|
|
|
* |
69
|
|
|
* @var \M1\Vars\Loader\LoaderProvider $loader |
70
|
|
|
*/ |
71
|
|
|
public $loader; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Have the base and cache paths been set |
75
|
|
|
* |
76
|
|
|
* @var bool $paths_loaded |
77
|
|
|
*/ |
78
|
|
|
private $paths_loaded = false; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The imported resources |
82
|
|
|
* |
83
|
|
|
* @var array $resources |
84
|
|
|
*/ |
85
|
|
|
private $resources = array(); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The words to be replaced in the config files |
89
|
|
|
* |
90
|
|
|
* @var array $variables |
91
|
|
|
*/ |
92
|
|
|
private $variables = array(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Creates a new instance of Vars |
96
|
|
|
* |
97
|
|
|
* @param string|array $resource The main configuration resource |
98
|
|
|
* @param array $options The options being used for Vars |
99
|
|
|
*/ |
100
|
80 |
|
public function __construct($resource, $options = array()) |
101
|
|
|
{ |
102
|
80 |
|
$options = $this->parseOptions($options); |
103
|
80 |
|
$this->makeCache($options, $resource); |
104
|
79 |
|
$this->makePaths($options); |
105
|
|
|
|
106
|
78 |
|
if (!$this->cache->checkCache()) { |
107
|
78 |
|
$this->makeLoader($options); |
108
|
76 |
|
$this->makeVariables($options); |
109
|
|
|
|
110
|
75 |
|
$resource = new ResourceProvider($this, $resource); |
111
|
62 |
|
} |
112
|
|
|
|
113
|
62 |
|
if ($this->cache->isHit()) { |
114
|
3 |
|
$this->loadFromCache(); |
115
|
3 |
|
} else { |
116
|
62 |
|
$resource->mergeParentContent(); |
|
|
|
|
117
|
62 |
|
$this->content = $resource->getContent(); |
118
|
|
|
|
119
|
62 |
|
$this->cache->setTime(time()); |
120
|
62 |
|
$this->cache->makeCache($this); |
121
|
|
|
|
122
|
|
|
} |
123
|
62 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Parses the options so Vars can use them |
127
|
|
|
* |
128
|
|
|
* @param array $options The options being used for Vars |
129
|
|
|
* |
130
|
|
|
* @return array The parsed options |
131
|
|
|
*/ |
132
|
80 |
|
private function parseOptions(array $options) |
133
|
|
|
{ |
134
|
80 |
|
$parsed_options = array_merge($this->default_options, $options); |
135
|
80 |
|
$parsed_options['loaders'] = (isset($options['loaders'])) ? $options['loaders'] : $this->default_options['loaders']; |
136
|
80 |
|
return $parsed_options; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Makes the CacheProvider with the options |
141
|
|
|
* |
142
|
|
|
* @param array $options The options being used for Vars |
143
|
|
|
* @param array|string $resource The main configuration resource |
144
|
|
|
*/ |
145
|
80 |
|
private function makeCache($options, $resource) |
146
|
|
|
{ |
147
|
80 |
|
$cache = new CacheProvider($resource, $options); |
148
|
79 |
|
$this->cache = $cache; |
149
|
79 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
153
|
|
|
* base path has |
154
|
|
|
* |
155
|
|
|
* @param array $options The options being used for Vars |
156
|
|
|
*/ |
157
|
79 |
|
private function makePaths($options) |
158
|
|
|
{ |
159
|
79 |
|
$this->setPath($options['path']); |
160
|
|
|
|
161
|
78 |
|
if (is_null($options['cache_path']) && !is_null($options['path'])) { |
162
|
|
|
|
163
|
1 |
|
$this->cache->setPath($options['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 |
|
'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 |
|
$path = $this->getPath(); |
235
|
|
|
|
236
|
68 |
|
if (!$path) { |
237
|
66 |
|
$file = pathinfo(realpath($resource)); |
238
|
66 |
|
$path = $file['dirname']; |
239
|
66 |
|
$this->setPath($path); |
240
|
66 |
|
} |
241
|
|
|
|
242
|
68 |
|
if ($this->cache->getProvide() && !$this->cache->getPath()) { |
243
|
2 |
|
$this->cache->setPath($path); |
244
|
2 |
|
} |
245
|
|
|
|
246
|
68 |
|
$this->paths_loaded = true; |
247
|
68 |
|
} |
248
|
69 |
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Adds a resource to $this->resources |
252
|
|
|
* |
253
|
|
|
* @param string $resource Resource to add to the stack |
254
|
|
|
* |
255
|
|
|
* @return int The position of the added resource |
256
|
|
|
*/ |
257
|
69 |
|
public function addResource($resource) |
258
|
|
|
{ |
259
|
69 |
|
$r = realpath($resource); |
260
|
69 |
|
$pos = count($this->resources); |
261
|
69 |
|
$this->resources[$pos] = $r; |
262
|
69 |
|
return $pos; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Updates the string resource with the FileResource |
267
|
|
|
* |
268
|
|
|
* @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
269
|
|
|
* @param int $pos The position of the string resource |
270
|
|
|
* |
271
|
|
|
* @return \M1\Vars\Vars |
272
|
|
|
*/ |
273
|
60 |
|
public function updateResource($resource, $pos) |
274
|
|
|
{ |
275
|
60 |
|
$this->resources[$pos] = $resource; |
276
|
60 |
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
281
|
|
|
* |
282
|
|
|
* @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
283
|
|
|
* |
284
|
|
|
* @return bool Has resource already been imported |
285
|
|
|
*/ |
286
|
69 |
|
public function resourceImported($resource) |
287
|
|
|
{ |
288
|
69 |
|
$resource = realpath($resource); |
289
|
69 |
|
foreach ($this->getResources() as $r) { |
290
|
45 |
|
if ((is_a($r, 'M1\Vars\Resource\FileResource') && $resource === $r->getFile()) || |
291
|
45 |
|
(is_string($r) && $resource === $r)) { |
292
|
1 |
|
return true; |
293
|
|
|
} |
294
|
69 |
|
} |
295
|
69 |
|
return false; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Searches the resource stack for a certain resource |
300
|
|
|
* |
301
|
|
|
* @param string $resource The resource to search for |
302
|
|
|
* |
303
|
|
|
* @return bool Returns the resource if found |
304
|
|
|
*/ |
305
|
3 |
|
public function getResource($resource) |
306
|
|
|
{ |
307
|
3 |
|
foreach ($this->getResources() as $r) { |
308
|
3 |
|
if ($resource === $r->getFilename()) { |
309
|
2 |
|
return $r; |
310
|
|
|
} |
311
|
3 |
|
} |
312
|
|
|
|
313
|
1 |
|
return false; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Returns the imported resources |
318
|
|
|
* |
319
|
|
|
* @return array The Vars imported resources |
320
|
|
|
*/ |
321
|
69 |
|
public function getResources() |
322
|
|
|
{ |
323
|
69 |
|
return $this->resources; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Returns the Vars replacement variables |
328
|
|
|
* |
329
|
|
|
* @return array The Vars replacement variables |
330
|
|
|
*/ |
331
|
52 |
|
public function getVariables() |
332
|
|
|
{ |
333
|
52 |
|
return $this->variables; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Returns the CacheProvider if set, if not return false |
338
|
|
|
* |
339
|
|
|
* @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
340
|
|
|
*/ |
341
|
7 |
|
public function getCache() |
342
|
|
|
{ |
343
|
7 |
|
return $this->cache; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: