1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/lav45/yii2-settings |
4
|
|
|
* @copyright Copyright (c) 2016 LAV45 |
5
|
|
|
* @author Aleksey Loban <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace lav45\settings\storage; |
10
|
|
|
|
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\BaseObject; |
13
|
|
|
use yii\helpers\FileHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class FileStorage |
17
|
|
|
* @package lav45\settings\storage |
18
|
|
|
*/ |
19
|
|
|
class FileStorage extends BaseObject implements StorageInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string the directory to store settings files. You may use path alias here. |
23
|
|
|
* If not set, it will use the "settings" subdirectory under the application runtime path. |
24
|
|
|
*/ |
25
|
|
|
public $path = '@runtime/settings'; |
26
|
|
|
/** |
27
|
|
|
* @var int the permission to be set for newly created cache files. |
28
|
|
|
* This value will be used by PHP chmod() function. No umask will be applied. |
29
|
|
|
* If not set, the permission will be determined by the current environment. |
30
|
|
|
*/ |
31
|
|
|
public $fileMode; |
32
|
|
|
/** |
33
|
|
|
* @var int the permission to be set for newly created directories. |
34
|
|
|
* This value will be used by PHP chmod() function. No umask will be applied. |
35
|
|
|
* Defaults to 0775, meaning the directory is read-writable by owner, but read-only for other users. |
36
|
|
|
*/ |
37
|
|
|
public $dirMode = 0755; |
38
|
|
|
/** |
39
|
|
|
* @var string settings file suffix. Defaults to '.bin'. |
40
|
|
|
*/ |
41
|
|
|
public $fileSuffix = '.bin'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initializes this component by ensuring the existence of the settings path. |
45
|
|
|
*/ |
46
|
5 |
|
public function init() |
47
|
|
|
{ |
48
|
5 |
|
parent::init(); |
49
|
5 |
|
$this->path = Yii::getAlias($this->path); |
|
|
|
|
50
|
5 |
|
FileHelper::createDirectory($this->path, $this->dirMode, true); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $key |
55
|
|
|
* @return false|null|string |
56
|
|
|
*/ |
57
|
1 |
|
public function getValue($key) |
58
|
|
|
{ |
59
|
1 |
|
$file = $this->getFile($key); |
60
|
|
|
|
61
|
1 |
|
$fp = @fopen($file, 'rb'); |
62
|
1 |
|
if ($fp !== false) { |
63
|
1 |
|
@flock($fp, LOCK_SH); |
|
|
|
|
64
|
1 |
|
$value = @stream_get_contents($fp); |
65
|
1 |
|
@flock($fp, LOCK_UN); |
66
|
1 |
|
@fclose($fp); |
|
|
|
|
67
|
1 |
|
return $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $key |
75
|
|
|
* @param string $value |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
5 |
|
public function setValue($key, $value) |
79
|
|
|
{ |
80
|
5 |
|
$file = $this->getFile($key); |
81
|
|
|
|
82
|
5 |
|
if (@file_put_contents($file, $value, LOCK_EX) !== false) { |
83
|
4 |
|
if ($this->fileMode !== null) { |
84
|
1 |
|
@chmod($file, $this->fileMode); |
|
|
|
|
85
|
|
|
} |
86
|
4 |
|
return true; |
87
|
|
|
} |
88
|
1 |
|
$error = error_get_last(); |
89
|
1 |
|
Yii::error("Unable to write file '{$file}': {$error['message']}"); |
90
|
1 |
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $key |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
4 |
|
public function deleteValue($key) |
98
|
|
|
{ |
99
|
4 |
|
return @unlink($this->getFile($key)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the storage file path given the key. |
104
|
|
|
* @param string $key |
105
|
|
|
* @return string the file path |
106
|
|
|
*/ |
107
|
5 |
|
protected function getFile($key) |
108
|
|
|
{ |
109
|
5 |
|
return $this->path . DIRECTORY_SEPARATOR . $key . $this->fileSuffix; |
110
|
|
|
} |
111
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.