1 | <?php |
||
20 | class YamlReader { |
||
21 | |||
22 | /** |
||
23 | * The path for the cache files. |
||
24 | */ |
||
25 | protected $cachePath; |
||
26 | |||
27 | /** |
||
28 | * Gets an absolute path for a cache file. |
||
29 | * |
||
30 | * @param string $fileName |
||
31 | * the file to cache |
||
32 | * @return string |
||
33 | * the absolute path of the cache file |
||
34 | */ |
||
35 | 82 | protected function getCacheFile($fileName) { |
|
38 | |||
39 | /** |
||
40 | * Reads the content of the cached file if it exists. |
||
41 | * |
||
42 | * @param string $fileName |
||
43 | * the cache file to read from |
||
44 | * @return null|array |
||
45 | * the cached data structure or null if the cache file was not available |
||
46 | */ |
||
47 | 82 | protected function readFromCache($fileName) { |
|
48 | 82 | $cacheFile = $this->getCacheFile($fileName); |
|
49 | 82 | if (file_exists($cacheFile) && is_readable($cacheFile)) { |
|
50 | 1 | include($cacheFile); |
|
51 | 1 | if (isset($crudlexCacheContent)) { |
|
52 | 1 | return $crudlexCacheContent; |
|
53 | } |
||
54 | } |
||
55 | 82 | return null; |
|
56 | 1 | } |
|
57 | |||
58 | /** |
||
59 | * Writes the given content to a cached PHP file. |
||
60 | * |
||
61 | * @param string $fileName |
||
62 | * the original filename |
||
63 | * @param array $content |
||
64 | * the content to cache |
||
65 | */ |
||
66 | 80 | protected function writeToCache($fileName, $content) { |
|
67 | 80 | if ($this->cachePath === null || !is_dir($this->cachePath) || !is_writable($this->cachePath)) { |
|
68 | 79 | return; |
|
69 | } |
||
70 | 1 | $encoder = new \Riimu\Kit\PHPEncoder\PHPEncoder(); |
|
71 | 1 | $contentPHP = $encoder->encode($content, [ |
|
72 | 1 | 'whitespace' => false, |
|
73 | 'recursion.detect' => false |
||
74 | 1 | ]); |
|
75 | 1 | $cache = '<?php $crudlexCacheContent = '.$contentPHP.';'; |
|
76 | 1 | file_put_contents($this->getCacheFile($fileName), $cache); |
|
77 | 1 | } |
|
78 | |||
79 | /** |
||
80 | * YamlReader constructor. |
||
81 | * @param string|null $cachePath |
||
82 | * if given, the path for the cache files which should be a writable directory |
||
83 | */ |
||
84 | 82 | public function __construct($cachePath) { |
|
87 | |||
88 | /** |
||
89 | * Reads and returns the contents of the given Yaml file. If |
||
90 | * it goes wrong, it throws an exception. |
||
91 | * |
||
92 | * @param string $fileName |
||
93 | * the file to read |
||
94 | * |
||
95 | * @return array |
||
96 | * the file contents |
||
97 | * |
||
98 | * @throws \RuntimeException |
||
99 | * thrown if the file could not be read or parsed |
||
100 | */ |
||
101 | 82 | public function read($fileName) { |
|
120 | |||
121 | } |
||
122 |