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 | ); |
||
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 variable provider |
||
89 | * |
||
90 | * @var \M1\Vars\Variables\VariableProvider $variables |
||
91 | */ |
||
92 | public $variables; |
||
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 | 83 | public function __construct($resource, $options = array()) |
|
123 | |||
124 | /** |
||
125 | * Parses the options so Vars can use them |
||
126 | * |
||
127 | * @param array $options The options being used for Vars |
||
128 | * |
||
129 | * @return array The parsed options |
||
130 | */ |
||
131 | 83 | private function parseOptions(array $options) |
|
137 | |||
138 | /** |
||
139 | * Makes the CacheProvider with the options |
||
140 | * |
||
141 | * @param array $options The options being used for Vars |
||
142 | * @param array|string $resource The main configuration resource |
||
143 | */ |
||
144 | 83 | private function makeCache($options, $resource) |
|
149 | |||
150 | /** |
||
151 | * Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
||
152 | * base path has |
||
153 | * |
||
154 | * @param array $options The options being used for Vars |
||
155 | */ |
||
156 | 82 | private function makePaths($options) |
|
165 | |||
166 | /** |
||
167 | * Makes the LoaderProvider with the options |
||
168 | * |
||
169 | * @param array $options The options being used for Vars |
||
170 | */ |
||
171 | 81 | private function makeLoader($options) |
|
176 | |||
177 | /** |
||
178 | * Sets the replacement variables if the option has been set |
||
179 | * |
||
180 | * @param array|null $options The options being used for Vars |
||
181 | */ |
||
182 | 79 | private function makeVariables($options) |
|
190 | |||
191 | /** |
||
192 | * Loads the cached file into the current class |
||
193 | */ |
||
194 | 3 | private function loadFromCache() |
|
217 | |||
218 | /** |
||
219 | * Checks if the base and cache paths have been set, if not set then will use the $resource as the base path |
||
220 | * |
||
221 | * @param string $resource The resource to use to set the paths if they haven't been set |
||
222 | */ |
||
223 | 72 | public function pathsLoadedCheck($resource) |
|
241 | |||
242 | /** |
||
243 | * Adds a resource to $this->resources |
||
244 | * |
||
245 | * @param string $resource Resource to add to the stack |
||
246 | * |
||
247 | * @return int The position of the added resource |
||
248 | */ |
||
249 | 72 | public function addResource($resource) |
|
256 | |||
257 | /** |
||
258 | * Updates the string resource with the FileResource |
||
259 | * |
||
260 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
261 | * @param int $pos The position of the string resource |
||
262 | * |
||
263 | * @return \M1\Vars\Vars |
||
264 | */ |
||
265 | 61 | public function updateResource($resource, $pos) |
|
270 | |||
271 | /** |
||
272 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
||
273 | * |
||
274 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
275 | * |
||
276 | * @return bool Has resource already been imported |
||
277 | */ |
||
278 | 72 | public function resourceImported($resource) |
|
289 | |||
290 | /** |
||
291 | * Searches the resource stack for a certain resource |
||
292 | * |
||
293 | * @param string $resource The resource to search for |
||
294 | * |
||
295 | * @return bool Returns the resource if found |
||
296 | */ |
||
297 | 3 | public function getResource($resource) |
|
307 | |||
308 | /** |
||
309 | * Returns the imported resources |
||
310 | * |
||
311 | * @return array The Vars imported resources |
||
312 | */ |
||
313 | 72 | public function getResources() |
|
317 | |||
318 | /** |
||
319 | * Returns the CacheProvider if set, if not return false |
||
320 | * |
||
321 | * @return \M1\Vars\Cache\CacheProvider|false The CacheProvider or false |
||
322 | */ |
||
323 | 7 | public function getCache() |
|
327 | } |
||
328 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: