|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheIconic\Config; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Config cache handler |
|
7
|
|
|
* |
|
8
|
|
|
* detects modifications on passed config source paths |
|
9
|
|
|
* and generates/updates a per-environment cache config php file |
|
10
|
|
|
* |
|
11
|
|
|
* @package Shared\Helper\Config |
|
12
|
|
|
*/ |
|
13
|
|
|
class Cache |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $basePath; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* constructor takes the base path for cache files |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $basePath the base path |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($basePath) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setBasePath($basePath); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* get the config array |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $key the config key |
|
35
|
|
|
* @return array the config array |
|
36
|
|
|
*/ |
|
37
|
|
|
public function read($key) |
|
38
|
|
|
{ |
|
39
|
|
|
return include($this->getCacheFile($key)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* get the path to the cache config file for the current environment |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $key the cache key |
|
46
|
|
|
* @return string the file path |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function getCacheFile($key) |
|
49
|
|
|
{ |
|
50
|
|
|
return sprintf('%s/%s.php', $this->basePath, $key); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* checks if the cached config is still |
|
55
|
|
|
* up to date or needs to be regenerated |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $key the cache key |
|
58
|
|
|
* @param $timestamp |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
public function isValid($key, $timestamp) |
|
62
|
|
|
{ |
|
63
|
|
|
$file = $this->getCacheFile($key); |
|
64
|
|
|
|
|
65
|
|
|
if (!is_readable($file)) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return (filemtime($file) >= $timestamp); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* writes the cache config php file |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $key the cache key |
|
76
|
|
|
* @param array $config the config array |
|
77
|
|
|
* @param array $sourcePaths the array of source config paths cached in this file |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function write($key, array $config, array $sourcePaths = []) |
|
81
|
|
|
{ |
|
82
|
|
|
$cacheFile = $this->getCacheFile($key); |
|
83
|
|
|
|
|
84
|
|
|
if (!is_dir(dirname($cacheFile))) { |
|
85
|
|
|
mkdir(dirname($cacheFile), 0777, true); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
file_put_contents($cacheFile, $this->compile($config, $sourcePaths)); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $config |
|
95
|
|
|
* @param array $sourcePaths |
|
96
|
|
|
* @return mixed|string |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function compile(array $config, array $sourcePaths = []) |
|
99
|
|
|
{ |
|
100
|
|
|
$content = <<<EOF |
|
101
|
|
|
<?php |
|
102
|
|
|
/* |
|
103
|
|
|
* this file- is autogenerated by %CLASSNAME% |
|
104
|
|
|
* and will be automatically overwritten |
|
105
|
|
|
* please edit the source files instead |
|
106
|
|
|
%SOURCES%*/ |
|
107
|
|
|
EOF; |
|
108
|
|
|
$content = str_replace('%CLASSNAME%', get_class($this), $content); |
|
109
|
|
|
$content = str_replace('%SOURCES%', $this->formatSourcePaths($sourcePaths), $content); |
|
110
|
|
|
$content .= PHP_EOL; |
|
111
|
|
|
$content .= 'return ' . var_export($config, true) . ';'; |
|
112
|
|
|
|
|
113
|
|
|
return $content; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* format source paths - only used for the cache config file teaser above |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $sourcePaths |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function formatSourcePaths(array $sourcePaths) |
|
123
|
|
|
{ |
|
124
|
|
|
$formatted = ''; |
|
125
|
|
|
|
|
126
|
|
|
foreach ($sourcePaths as $path) { |
|
127
|
|
|
$formatted .= sprintf(' * %s', $path) . PHP_EOL; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $formatted; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* set the base path |
|
135
|
|
|
* |
|
136
|
|
|
* @param $path |
|
137
|
|
|
* @return $this |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setBasePath($path) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->basePath = $path; |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* get the base path |
|
148
|
|
|
* |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getBasePath() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->basePath; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|