1 | <?php |
||
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 | 'merge_globals' => true, |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * The global file variables |
||
69 | * |
||
70 | * @var array globals |
||
71 | */ |
||
72 | private $globals = array(); |
||
73 | |||
74 | /** |
||
75 | * The loaderProvider for Vars supplies the file loaders and the extensions that are supported |
||
76 | * |
||
77 | * @var \M1\Vars\Loader\LoaderProvider $loader |
||
78 | */ |
||
79 | public $loader; |
||
80 | |||
81 | /** |
||
82 | * Have the base and cache paths been set |
||
83 | * |
||
84 | * @var bool $paths_loaded |
||
85 | */ |
||
86 | private $paths_loaded = false; |
||
87 | |||
88 | /** |
||
89 | * The imported resources |
||
90 | * |
||
91 | * @var array $resources |
||
92 | */ |
||
93 | private $resources = array(); |
||
94 | |||
95 | /** |
||
96 | * The variable provider |
||
97 | * |
||
98 | * @var \M1\Vars\Variables\VariableProvider $variables |
||
99 | */ |
||
100 | public $variables; |
||
101 | |||
102 | /** |
||
103 | * Creates a new instance of Vars |
||
104 | * |
||
105 | * @param string|array $resource The main configuration resource |
||
106 | * @param array $options The options being used for Vars |
||
107 | */ |
||
108 | 86 | public function __construct($resource, $options = array()) |
|
129 | |||
130 | /** |
||
131 | * Parses the options so Vars can use them |
||
132 | * |
||
133 | * @param array $options The options being used for Vars |
||
134 | * |
||
135 | * @return array The parsed options |
||
136 | */ |
||
137 | 86 | private function parseOptions(array $options) |
|
145 | |||
146 | /** |
||
147 | * Makes the CacheProvider with the options |
||
148 | * |
||
149 | * @param array $options The options being used for Vars |
||
150 | * @param array|string $resource The main configuration resource |
||
151 | */ |
||
152 | 86 | private function makeCache($options, $resource) |
|
157 | |||
158 | /** |
||
159 | * Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
||
160 | * base path has |
||
161 | * |
||
162 | * @param array $options The options being used for Vars |
||
163 | */ |
||
164 | 85 | private function makePaths($options) |
|
173 | |||
174 | /** |
||
175 | * Makes the LoaderProvider with the options |
||
176 | * |
||
177 | * @param array $options The options being used for Vars |
||
178 | */ |
||
179 | 84 | private function makeLoader($options) |
|
184 | |||
185 | /** |
||
186 | * Sets the replacement variables if the option has been set |
||
187 | * |
||
188 | * @param array|null $options The options being used for Vars |
||
189 | */ |
||
190 | 82 | private function makeVariables($options) |
|
198 | |||
199 | /** |
||
200 | * Loads the cached file into the current class |
||
201 | */ |
||
202 | 3 | private function loadFromCache() |
|
226 | |||
227 | /** |
||
228 | * Checks if the base and cache paths have been set, if not\ set then will use the $resource as the base path |
||
229 | * |
||
230 | * @param string $resource The resource to use to set the paths if they haven't been set |
||
231 | */ |
||
232 | 75 | public function pathsLoadedCheck($resource) |
|
250 | |||
251 | |||
252 | /** |
||
253 | * Gets the _globals from the file and merges them if merge_globals is true |
||
254 | * |
||
255 | * @param array $content The unparsed content |
||
256 | * @param array $options The options being used for Vars |
||
257 | * |
||
258 | * @return array $content The parsed content |
||
259 | */ |
||
260 | 66 | private function mergeGlobals($content, $options) |
|
274 | |||
275 | /** |
||
276 | * Adds a resource to $this->resources |
||
277 | * |
||
278 | * @param string $resource Resource to add to the stack |
||
279 | * |
||
280 | * @return int The position of the added resource |
||
281 | */ |
||
282 | 75 | public function addResource($resource) |
|
289 | |||
290 | /** |
||
291 | * Updates the string resource with the FileResource |
||
292 | * |
||
293 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
294 | * @param int $pos The position of the string resource |
||
295 | * |
||
296 | * @return \M1\Vars\Vars |
||
297 | */ |
||
298 | 64 | public function updateResource($resource, $pos) |
|
303 | |||
304 | /** |
||
305 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
||
306 | * |
||
307 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
308 | * |
||
309 | * @return bool Has resource already been imported |
||
310 | */ |
||
311 | 75 | public function resourceImported($resource) |
|
322 | |||
323 | /** |
||
324 | * Searches the resource stack for a certain resource |
||
325 | * |
||
326 | * @param string $resource The resource to search for |
||
327 | * |
||
328 | * @return \M1\Vars\Resource\FileResource|bool Returns the resource if found |
||
329 | */ |
||
330 | 3 | public function getResource($resource) |
|
340 | |||
341 | /** |
||
342 | * Returns the imported resources |
||
343 | * |
||
344 | * @return array The Vars imported resources |
||
345 | */ |
||
346 | 75 | public function getResources() |
|
350 | |||
351 | /** |
||
352 | * Returns the imported resources |
||
353 | * |
||
354 | * @return array The Vars imported resources |
||
355 | */ |
||
356 | 2 | public function getGlobals() |
|
360 | |||
361 | /** |
||
362 | * Returns the CacheProvider if set |
||
363 | * |
||
364 | * @return \M1\Vars\Cache\CacheProvider The CacheProvider |
||
365 | */ |
||
366 | 7 | public function getCache() |
|
370 | } |
||
371 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: