|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ffcms\Core; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Ffcms\Core\Exception\NativeException; |
|
7
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
|
8
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
|
9
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
|
10
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Properties. Provide methods to work with ffcms configs. |
|
14
|
|
|
* @package Ffcms\Core |
|
15
|
|
|
*/ |
|
16
|
|
|
class Properties |
|
17
|
|
|
{ |
|
18
|
|
|
protected $data; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Load default configuration |
|
22
|
|
|
* @throws NativeException |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
if ($this->load('default') === false) { |
|
27
|
|
|
throw new NativeException('Default configurations is not founded: /Private/Config/Default.php'); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Load configurations from file in /Private/Config/ |
|
33
|
|
|
* @param string $configName |
|
34
|
|
|
* @param bool $overload |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
private function load($configName, $overload = false) |
|
38
|
|
|
{ |
|
39
|
|
|
// check if always loaded |
|
40
|
|
|
if (Obj::isArray($this->data) && array_key_exists($configName, $this->data) && $overload === false) { |
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// try to load from file |
|
45
|
|
|
$configFile = ucfirst(Str::lowerCase($configName)) . '.php'; |
|
46
|
|
|
if (File::exist('/Private/Config/' . $configFile)) { |
|
47
|
|
|
$this->data[$configName] = File::inc('/Private/Config/' . $configFile, true); |
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get config value by config key from configuration file |
|
56
|
|
|
* @param string $configKey |
|
57
|
|
|
* @param string $configFile |
|
58
|
|
|
* @param bool|false $parseType |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function get($configKey, $configFile = 'default', $parseType = false) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->load($configFile); |
|
64
|
|
|
// check if configs for this file is loaded |
|
65
|
|
|
if (!isset($this->data[$configFile])) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// check if config key is exist |
|
70
|
|
|
if (!isset($this->data[$configFile][$configKey])) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$response = $this->data[$configFile][$configKey]; |
|
75
|
|
|
|
|
76
|
|
|
// try to convert config value by defined parse type |
|
77
|
|
|
$parseType = Str::lowerCase($parseType); |
|
|
|
|
|
|
78
|
|
|
switch ($parseType) { |
|
79
|
|
|
case 'int': |
|
80
|
|
|
case 'integer': |
|
81
|
|
|
$response = (int)$response; |
|
82
|
|
|
break; |
|
83
|
|
|
case 'bool': |
|
84
|
|
|
case 'boolean': |
|
85
|
|
|
$response = (bool)$response; |
|
86
|
|
|
break; |
|
87
|
|
|
case 'float': |
|
88
|
|
|
$response = (float)$response; |
|
89
|
|
|
break; |
|
90
|
|
|
case 'double': |
|
91
|
|
|
$response = (double)$response; |
|
92
|
|
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $response; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get all configuration data of selected file |
|
100
|
|
|
* @param string $configFile |
|
101
|
|
|
* @return bool|array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getAll($configFile = 'default') |
|
104
|
|
|
{ |
|
105
|
|
|
$this->load($configFile); |
|
106
|
|
|
if (!Obj::isArray($this->data) || !array_key_exists($configFile, $this->data)) { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->data[$configFile]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Update configuration data based on key-value array of new data. Do not pass multi-level array on new position without existing keys |
|
115
|
|
|
* @param string $configFile |
|
116
|
|
|
* @param array $newData |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function updateConfig($configFile, array $newData) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->load($configFile); |
|
122
|
|
|
if (!isset($this->data[$configFile])) { |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$saveData = Arr::merge($this->data[$configFile], $newData); |
|
127
|
|
|
return $this->writeConfig($configFile, $saveData); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Write configurations data from array to cfg file |
|
132
|
|
|
* @param string $configFile |
|
133
|
|
|
* @param array $data |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function writeConfig($configFile, array $data) |
|
137
|
|
|
{ |
|
138
|
|
|
$path = '/Private/Config/' . ucfirst(Str::lowerCase($configFile)) . '.php'; |
|
139
|
|
|
if (!File::exist($path) || !File::writable($path)) { |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
$saveData = '<?php return ' . Arr::exportVar($data) . ';'; |
|
143
|
|
|
File::write($path, $saveData); |
|
144
|
|
|
// overload config values if changed |
|
145
|
|
|
$this->load($configFile, true); |
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: