1 | <?php |
||
58 | class WrappedConfig |
||
59 | { |
||
60 | /** |
||
61 | * used when we do not know the name of the config we are wrapping |
||
62 | */ |
||
63 | const NO_NAME = "UNKNOWN"; |
||
64 | |||
65 | const ROOT_IS_OBJECT = false; |
||
66 | const ROOT_IS_ARRAY = true; |
||
67 | |||
68 | /** |
||
69 | * the config settings that this object wraps |
||
70 | * @var BaseObject|array |
||
71 | */ |
||
72 | private $config; |
||
73 | |||
74 | /** |
||
75 | * remember where we were loaded from |
||
76 | * @var string|null |
||
77 | */ |
||
78 | private $filename = null; |
||
79 | |||
80 | /** |
||
81 | * the name assigned to this config container |
||
82 | * @var string|null |
||
83 | */ |
||
84 | private $name = null; |
||
85 | |||
86 | /** |
||
87 | * constructor |
||
88 | */ |
||
89 | public function __construct($isArray = false) |
||
90 | { |
||
91 | $this->setName("UNKNOWN"); |
||
92 | if (!$isArray) { |
||
93 | $this->setConfig(new BaseObject); |
||
94 | } |
||
95 | else { |
||
96 | $this->setConfig([]); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * get the config that we wrap |
||
102 | * |
||
103 | * @return array|object |
||
104 | */ |
||
105 | public function &getConfig() |
||
109 | |||
110 | /** |
||
111 | * store the config that we wrap |
||
112 | * |
||
113 | * @param array|object $config |
||
114 | * the config to store |
||
115 | */ |
||
116 | public function setConfig($config) |
||
120 | |||
121 | /** |
||
122 | * do we have any config? |
||
123 | * |
||
124 | * @return boolean |
||
125 | */ |
||
126 | public function hasConfig() |
||
127 | { |
||
128 | if ($this->config instanceof BaseObject) { |
||
129 | return $this->config->hasProperties(); |
||
130 | } |
||
131 | |||
132 | if (is_array($this->config)) { |
||
133 | return empty($this->config); |
||
134 | } |
||
135 | |||
136 | return false; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * load a config file from disk, and store the config in $this |
||
141 | * |
||
142 | * @param string $pathToFile |
||
143 | * the filename to load from |
||
144 | * @return void |
||
145 | */ |
||
146 | public function loadConfigFromFile($pathToFile) |
||
147 | { |
||
148 | if (!file_exists($pathToFile)) { |
||
149 | throw new E4xx_ConfigFileNotFound($pathToFile); |
||
150 | } |
||
151 | |||
152 | $raw = @file_get_contents($pathToFile); |
||
153 | if (false === $raw) { |
||
154 | throw new E4xx_ConfigFileNotFound($pathToFile); |
||
155 | } |
||
156 | |||
157 | // special case - file is empty |
||
158 | if (strlen(rtrim($raw)) === 0) { |
||
159 | // nothing to see, move along :) |
||
160 | $this->setConfig(new BaseObject); |
||
161 | $this->setNameFromFilename($pathToFile); |
||
162 | return; |
||
163 | } |
||
164 | |||
165 | // if we get here, we expect the file to contain valid JSON |
||
166 | $json = @json_decode($raw); |
||
167 | if (null === $json) { |
||
168 | throw new E4xx_ConfigFileContainsInvalidJson($pathToFile); |
||
169 | } |
||
170 | |||
171 | // convert from stdClass to our useful BaseObject |
||
172 | $config = new BaseObject; |
||
173 | $config->mergeFrom($json); |
||
174 | |||
175 | // store the config |
||
176 | $this->setConfig($config); |
||
177 | $this->setNameFromFilename($pathToFile); |
||
178 | $this->setFilename($pathToFile); |
||
179 | |||
180 | // all done |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * set the name of this config by looking at the filename |
||
185 | * |
||
186 | * the 'name' is used as an array key elsewhere. if we get this wrong, |
||
187 | * then Storyplayer isn't going to be able to find our config later on |
||
188 | * |
||
189 | * override this in more specialist config types |
||
190 | * |
||
191 | * @param string $filename |
||
192 | * the path/to/file/name.ext where we found this config |
||
193 | * @return void |
||
194 | */ |
||
195 | protected function setNameFromFilename($filename) |
||
196 | { |
||
197 | $this->setName(basename($filename, '.json')); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * save the config to disk, as a JSON file |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | public function saveConfig() |
||
206 | { |
||
207 | // do we have a filename? |
||
208 | $filename = $this->getFilename(); |
||
209 | if ($filename === null) { |
||
210 | throw new E4xx_ConfigNeedsAFilename($this->getName()); |
||
211 | } |
||
212 | |||
213 | // make sure that the parent folder exists |
||
214 | $this->makeConfigDir(dirname($filename)); |
||
215 | |||
216 | // let's get this saved |
||
217 | $data = json_encode($this->getConfig(), JSON_PRETTY_PRINT); |
||
218 | if (!file_put_contents($filename, $data)) { |
||
219 | throw new E4xx_ConfigCannotBeSaved($name, $filename); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return void |
||
225 | */ |
||
226 | protected function makeConfigDir($configDir) |
||
227 | { |
||
228 | if (file_exists($configDir)) { |
||
229 | // nothing to do |
||
230 | return; |
||
231 | } |
||
232 | |||
233 | // can we make the folder? |
||
234 | // |
||
235 | // if this fails, we do not know why |
||
236 | $success = mkdir($configDir, 0700, true); |
||
237 | if (!$success) |
||
238 | { |
||
239 | // cannot create it - bail out now |
||
240 | throw new E4xx_ConfigPathCannotBeCreated($configDir); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * forget all of the config we (may) currently have |
||
246 | * |
||
247 | * @return void |
||
248 | */ |
||
249 | public function emptyConfig() |
||
253 | |||
254 | /** |
||
255 | * @return void |
||
256 | */ |
||
257 | public function removeConfig() |
||
258 | { |
||
259 | // do we have a filename? |
||
260 | $filename = $this->getFilename(); |
||
261 | if ($filename === null) { |
||
262 | throw new E4xx_ConfigNeedsAFilename($this->getName()); |
||
263 | } |
||
264 | |||
265 | // does the file exist? |
||
266 | if (!file_exists($filename)) { |
||
267 | // nothing to do |
||
268 | return; |
||
269 | } |
||
270 | |||
271 | // remove the file |
||
272 | $deleted = @unlink($filename); |
||
273 | if ($deleted) { |
||
274 | // all done |
||
275 | return; |
||
276 | } |
||
277 | |||
278 | // if we get here, we could not remove the file |
||
279 | throw new E4xx_ConfigCannotBeRemoved($this->getName(), $filename); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * returns the name assigned to this config |
||
284 | * @return string|null |
||
285 | */ |
||
286 | public function getName() |
||
290 | |||
291 | /** |
||
292 | * assigns a name to this config |
||
293 | * |
||
294 | * @param string $name |
||
295 | * the name to assign |
||
296 | */ |
||
297 | public function setName($name) |
||
301 | |||
302 | /** |
||
303 | * returns the filename we loaded this config from |
||
304 | * |
||
305 | * @return string|null |
||
306 | */ |
||
307 | public function getFilename() |
||
311 | |||
312 | /** |
||
313 | * sets the filename that we loaded this config from |
||
314 | * |
||
315 | * @param string $filename |
||
316 | * the filename to store |
||
317 | */ |
||
318 | public function setFilename($filename) |
||
322 | |||
323 | // ================================================================== |
||
324 | // |
||
325 | // dot.notation.support |
||
326 | // |
||
327 | // ------------------------------------------------------------------ |
||
328 | |||
329 | /** |
||
330 | * expand any variables in $this |
||
331 | * |
||
332 | * @param array|object|null $baseConfig |
||
333 | * the config to use for expanding variables (optional) |
||
334 | * @return array|object |
||
335 | * a copy of the config stored in $this, with any Twig |
||
336 | * variables expanded |
||
337 | */ |
||
338 | public function getExpandedConfig($baseConfig = null) |
||
342 | |||
343 | /** |
||
344 | * retrieve data using a dot.notation.path |
||
345 | * |
||
346 | * NOTE that you should treat any data returned from here as READ-ONLY |
||
347 | * |
||
348 | * @param string $path |
||
349 | * the dot.notation.path to use to navigate |
||
350 | * |
||
351 | * @return mixed |
||
352 | */ |
||
353 | public function getData($path) |
||
357 | |||
358 | /** |
||
359 | * retrieve data from a dot.notation.path |
||
360 | * |
||
361 | * throws an exception if the path does not point to an array |
||
362 | * |
||
363 | * @param string $path |
||
364 | * the dot.notation.path to the data to return |
||
365 | * @return array |
||
366 | */ |
||
367 | public function getArray($path) |
||
371 | |||
372 | /** |
||
373 | * retrieve data from a dot.notation.path |
||
374 | * |
||
375 | * throws an exception if the path does not point to an object |
||
376 | * |
||
377 | * @param string $path |
||
378 | * the dot.notation.path to the data to return |
||
379 | * @return object |
||
380 | */ |
||
381 | public function getObject($path) |
||
385 | |||
386 | /** |
||
387 | * check for existence of data using a dot.notation.path |
||
388 | * |
||
389 | * @param string $path |
||
390 | * the dot.notation.path to use to navigate |
||
391 | * |
||
392 | * @return boolean |
||
393 | */ |
||
394 | public function hasData($path) |
||
398 | |||
399 | /** |
||
400 | * merge data into this config |
||
401 | * |
||
402 | * @param string $path |
||
403 | * path.to.merge.to |
||
404 | * @param mixed $dataToMerge |
||
405 | * the data to merge at $path |
||
406 | * @return void |
||
407 | */ |
||
408 | public function mergeData($path, $dataToMerge) |
||
412 | |||
413 | /** |
||
414 | * assigns data to a specific path |
||
415 | * |
||
416 | * @param string $path |
||
417 | * the path to assign to |
||
418 | * @param mixed $data |
||
419 | * the data to assign |
||
420 | * |
||
421 | * @return void |
||
422 | */ |
||
423 | public function setData($path, $data) |
||
424 | { |
||
425 | // special case |
||
426 | if ($path == "") { |
||
427 | $this->config = $data; |
||
428 | return; |
||
429 | } |
||
430 | |||
431 | // general case |
||
432 | $this->config->setData($path, $data); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * support for arrow->notation support on wrapped configs |
||
437 | * |
||
438 | * NOTE: objects returned by arrow->notation are READ-WRITE |
||
439 | * |
||
440 | * @param string $path |
||
441 | * the variable to be retrieved |
||
442 | * @return mixed |
||
443 | * the fake attribute |
||
444 | */ |
||
445 | public function __get($path) |
||
446 | { |
||
447 | return $this->config->$path; |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * support for arrow->notation on wrapped configs |
||
452 | * |
||
453 | * @param string $path |
||
454 | * the name of the attribute to save |
||
455 | * @param mixed $data |
||
456 | * the data to save |
||
457 | * @return void |
||
458 | */ |
||
459 | public function __set($path, $data) |
||
460 | { |
||
461 | return $this->setData($path, $data); |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * remove data using a dot.notation.path |
||
466 | * |
||
467 | * @param string $path |
||
468 | * the dot.notation.path to use to navigate |
||
469 | * |
||
470 | * @return void |
||
471 | */ |
||
472 | public function unsetData($path) |
||
476 | } |
||
477 |