-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoragePhp.php
156 lines (129 loc) · 3.42 KB
/
StoragePhp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace yii1tech\config;
use Yii;
/**
* StoragePhp represents the configuration storage based on local PHP files.
*
* @property string $fileName public alias of {@see _fileName}.
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class StoragePhp extends Storage
{
/**
* @var string name of the file, which should be used to store values.
*/
protected $_fileName;
/**
* @param string $fileName
* @return static self reference.
*/
public function setFileName($fileName): self
{
$this->_fileName = $fileName;
return $this;
}
/**
* @return string
*/
public function getFileName(): string
{
if (empty($this->_fileName)) {
$this->_fileName = $this->defaultFileName();
}
return $this->_fileName;
}
/**
* Creates default {@see fileName} value.
* @return string default file name.
*/
protected function defaultFileName(): string
{
return Yii::getPathOfAlias('application.runtime') . DIRECTORY_SEPARATOR . 'app_config_data.php';
}
/**
* {@inheritdoc}
*/
public function save(array $values): bool
{
$values = array_merge($this->get(), $values);
$bytesWritten = $this->saveToFile($values);
return ($bytesWritten > 0);
}
/**
* {@inheritdoc}
*/
public function get(): array
{
$fileName = $this->getFileName();
if (file_exists($fileName)) {
return require($fileName);
}
return [];
}
/**
* {@inheritdoc}
*/
public function clear(): bool
{
$fileName = $this->getFileName();
if (file_exists($fileName)) {
$this->invalidateScriptCache($fileName);
return unlink($fileName);
}
return true;
}
/**
* {@inheritdoc}
*/
public function clearValue($key): bool
{
$values = $this->get();
if (empty($values)) {
return true;
}
unset($values[$key]);
$bytesWritten = $this->saveToFile($values);
return ($bytesWritten > 0);
}
/**
* Saves given values to the file.
* @param array $values values to be saved.
* @return int number of bytes written.
*/
protected function saveToFile(array $values)
{
$fileName = $this->getFileName();
$dirName = dirname($fileName);
if (!file_exists($dirName)) {
mkdir($dirName, 0777, true);
}
$bytesWritten = file_put_contents($fileName, $this->composeFileContent($values));
$this->invalidateScriptCache($fileName);
return $bytesWritten;
}
/**
* Composes file content for the given values.
* @param array $values values to be saved.
* @return string file content.
*/
protected function composeFileContent(array $values): string
{
$content = '<?php return ' . var_export($values, true) . ';';
return $content;
}
/**
* Invalidates precompiled script cache (such as OPCache or APC) for the given file.
* @param string $fileName file name.
*/
protected function invalidateScriptCache($fileName)
{
if (function_exists('opcache_invalidate')) {
opcache_invalidate($fileName, true);
}
if (function_exists('apc_delete_file')) {
@apc_delete_file($fileName);
}
}
}