1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/LAV45/yii2-settings |
4
|
|
|
* @copyright Copyright (c) 2016 LAV45 |
5
|
|
|
* @author Alexey Loban <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace lav45\settings\storage; |
10
|
|
|
|
11
|
|
|
use yii\helpers\VarDumper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PhpFileStorage |
15
|
|
|
* @package lav45\settings\storage |
16
|
|
|
*/ |
17
|
|
|
class PhpFileStorage extends FileStorage |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string settings file suffix. |
21
|
|
|
*/ |
22
|
|
|
public $fileSuffix = '.php'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $key |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
public function getValue($key) |
29
|
|
|
{ |
30
|
|
|
$fileName = $this->getFile($key); |
31
|
|
|
if (file_exists($fileName)) { |
32
|
|
|
return include $fileName; |
33
|
|
|
} |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $key |
39
|
|
|
* @param mixed $value |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function setValue($key, $value) |
43
|
|
|
{ |
44
|
|
|
$value = is_string($value) ? "'{$value}'" : VarDumper::export($value); |
45
|
|
|
$value = "<?php\nreturn {$value};\n"; |
46
|
|
|
|
47
|
|
|
$result = parent::setValue($key, $value); |
48
|
|
|
|
49
|
|
|
if ($result === true) { |
50
|
|
|
$fileName = $this->getFile($key); |
51
|
|
|
$this->forceScriptCache($fileName); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Forcibly caches data of this file in OPCache or APC. |
59
|
|
|
* @param string $fileName file name. |
60
|
|
|
* @since 1.0.4 |
61
|
|
|
*/ |
62
|
|
|
protected function forceScriptCache($fileName) |
63
|
|
|
{ |
64
|
|
|
if (function_exists('opcache_invalidate')) { |
65
|
|
|
opcache_invalidate($fileName, true); // @codeCoverageIgnore |
66
|
|
|
} |
67
|
|
|
if (function_exists('opcache_compile_file')) { |
68
|
|
|
opcache_compile_file($fileName); // @codeCoverageIgnore |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|