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 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) |
|
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) |
|
198 | |||
199 | /** |
||
200 | * Loads the cached file into the current class |
||
201 | */ |
||
202 | 3 | private function loadFromCache() |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
315 | |||
316 | /** |
||
317 | * Returns the imported resources |
||
318 | * |
||
319 | * @return array The Vars imported resources |
||
320 | */ |
||
321 | 69 | public function getResources() |
|
325 | |||
326 | /** |
||
327 | * Returns the Vars replacement variables |
||
328 | * |
||
329 | * @return array The Vars replacement variables |
||
330 | */ |
||
331 | 52 | public function getVariables() |
|
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() |
|
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: