|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CRUDlex package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CRUDlex; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Reads Yaml files and caches them if a writable path is given. The cache is used first on the next read. |
|
18
|
|
|
* It is a simple PHP class internally. |
|
19
|
|
|
*/ |
|
20
|
|
|
class YamlReader |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The path for the cache files. |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $cachePath; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Gets an absolute path for a cache file. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $fileName |
|
33
|
|
|
* the file to cache |
|
34
|
|
|
* @return string |
|
35
|
|
|
* the absolute path of the cache file |
|
36
|
|
|
*/ |
|
37
|
83 |
|
protected function getCacheFile($fileName) |
|
38
|
|
|
{ |
|
39
|
83 |
|
return $this->cachePath.'/'.basename($fileName).'CRUDlexCache.php'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Reads the content of the cached file if it exists. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $fileName |
|
46
|
|
|
* the cache file to read from |
|
47
|
|
|
* @return null|array |
|
48
|
|
|
* the cached data structure or null if the cache file was not available |
|
49
|
|
|
*/ |
|
50
|
83 |
|
protected function readFromCache($fileName) |
|
51
|
|
|
{ |
|
52
|
83 |
|
$cacheFile = $this->getCacheFile($fileName); |
|
53
|
83 |
|
if (file_exists($cacheFile) && is_readable($cacheFile)) { |
|
54
|
1 |
|
include($cacheFile); |
|
55
|
1 |
|
if (isset($crudlexCacheContent)) { |
|
56
|
1 |
|
return $crudlexCacheContent; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
83 |
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Writes the given content to a cached PHP file. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $fileName |
|
66
|
|
|
* the original filename |
|
67
|
|
|
* @param array $content |
|
68
|
|
|
* the content to cache |
|
69
|
|
|
*/ |
|
70
|
81 |
|
protected function writeToCache($fileName, $content) |
|
71
|
|
|
{ |
|
72
|
81 |
|
if ($this->cachePath === null || !is_dir($this->cachePath) || !is_writable($this->cachePath)) { |
|
73
|
80 |
|
return; |
|
74
|
|
|
} |
|
75
|
1 |
|
$encoder = new \Riimu\Kit\PHPEncoder\PHPEncoder(); |
|
76
|
1 |
|
$contentPHP = $encoder->encode($content, [ |
|
77
|
1 |
|
'whitespace' => false, |
|
78
|
|
|
'recursion.detect' => false |
|
79
|
|
|
]); |
|
80
|
1 |
|
$cache = '<?php $crudlexCacheContent = '.$contentPHP.';'; |
|
81
|
1 |
|
file_put_contents($this->getCacheFile($fileName), $cache); |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* YamlReader constructor. |
|
86
|
|
|
* @param string|null $cachePath |
|
87
|
|
|
* if given, the path for the cache files which should be a writable directory |
|
88
|
|
|
*/ |
|
89
|
83 |
|
public function __construct($cachePath) |
|
90
|
|
|
{ |
|
91
|
83 |
|
$this->cachePath = $cachePath; |
|
92
|
83 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Reads and returns the contents of the given Yaml file. If |
|
96
|
|
|
* it goes wrong, it throws an exception. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $fileName |
|
99
|
|
|
* the file to read |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
* the file contents |
|
103
|
|
|
* |
|
104
|
|
|
* @throws \RuntimeException |
|
105
|
|
|
* thrown if the file could not be read or parsed |
|
106
|
|
|
*/ |
|
107
|
83 |
|
public function read($fileName) |
|
108
|
|
|
{ |
|
109
|
83 |
|
$parsedYaml = $this->readFromCache($fileName); |
|
110
|
83 |
|
if ($parsedYaml !== null) { |
|
111
|
1 |
|
return $parsedYaml; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
try { |
|
115
|
83 |
|
$fileContent = file_get_contents($fileName); |
|
116
|
81 |
|
$parsedYaml = Yaml::parse($fileContent); |
|
117
|
81 |
|
if (!is_array($parsedYaml)) { |
|
118
|
1 |
|
$parsedYaml = []; |
|
119
|
|
|
} |
|
120
|
81 |
|
$this->writeToCache($fileName, $parsedYaml); |
|
121
|
81 |
|
return $parsedYaml; |
|
122
|
2 |
|
} catch (\Exception $e) { |
|
123
|
2 |
|
throw new \RuntimeException('Could not read Yaml file '.$fileName, $e->getCode(), $e); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|