These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Redaxscript; |
||
3 | |||
4 | use function array_key_exists; |
||
5 | use function array_keys; |
||
6 | use function basename; |
||
7 | use function dirname; |
||
8 | use function end; |
||
9 | use function is_array; |
||
10 | use function is_file; |
||
11 | use function parse_url; |
||
12 | use function str_replace; |
||
13 | use function trim; |
||
14 | |||
15 | /** |
||
16 | * children class to store database config |
||
17 | * |
||
18 | * @since 2.4.0 |
||
19 | * |
||
20 | * @package Redaxscript |
||
21 | * @category Config |
||
22 | * @author Henry Ruhs |
||
23 | */ |
||
24 | |||
25 | class Config extends Singleton |
||
26 | { |
||
27 | /** |
||
28 | * path to the config |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | |||
33 | protected static $_configPath = 'config.php'; |
||
34 | |||
35 | /** |
||
36 | * array of the config |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | |||
41 | protected static $_configArray = []; |
||
42 | |||
43 | /** |
||
44 | * init the class |
||
45 | * |
||
46 | * @since 2.4.0 |
||
47 | * |
||
48 | * @param string $configPath path to the config |
||
49 | */ |
||
50 | |||
51 | 6 | public function init(string $configPath = null) : void |
|
52 | { |
||
53 | 6 | if (is_file($configPath)) |
|
54 | { |
||
55 | 6 | self::$_configPath = $configPath; |
|
56 | } |
||
57 | |||
58 | /* load config */ |
||
59 | |||
60 | 6 | $configArray = include(self::$_configPath); |
|
61 | 6 | if (is_array($configArray)) |
|
62 | { |
||
63 | 1 | self::$_configArray = $configArray; |
|
64 | } |
||
65 | 6 | } |
|
66 | |||
67 | /** |
||
68 | * get the value from config |
||
69 | * |
||
70 | * @since 2.2.0 |
||
71 | * |
||
72 | * @param string $key key of the item |
||
73 | * |
||
74 | * @return string|array|null |
||
75 | */ |
||
76 | |||
77 | 8 | public function get(string $key = null) |
|
78 | { |
||
79 | 8 | if (is_array(self::$_configArray) && array_key_exists($key, self::$_configArray)) |
|
80 | { |
||
81 | 1 | return self::$_configArray[$key]; |
|
82 | } |
||
83 | 8 | if (!$key) |
|
0 ignored issues
–
show
|
|||
84 | { |
||
85 | 8 | return self::$_configArray; |
|
86 | } |
||
87 | return null; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * set the value to config |
||
92 | * |
||
93 | * @since 2.2.0 |
||
94 | * |
||
95 | * @param string $key key of the item |
||
96 | * @param string $value value of the item |
||
97 | */ |
||
98 | |||
99 | 8 | public function set(string $key = null, string $value = null) : void |
|
100 | { |
||
101 | 8 | self::$_configArray[$key] = $value; |
|
102 | 8 | } |
|
103 | |||
104 | /** |
||
105 | * parse from database url |
||
106 | * |
||
107 | * @since 3.0.0 |
||
108 | * |
||
109 | * @param string $dbUrl database url to be parsed |
||
110 | */ |
||
111 | |||
112 | 4 | public function parse(string $dbUrl = null) : void |
|
113 | { |
||
114 | 4 | $dbUrl = parse_url($dbUrl); |
|
115 | 4 | $this->clear(); |
|
116 | 4 | $this->set('dbType', str_replace('postgres', 'pgsql', $dbUrl['scheme'])); |
|
117 | 4 | $this->set('dbHost', $dbUrl['port'] ? $dbUrl['host'] . ':' . $dbUrl['port'] : $dbUrl['host']); |
|
118 | 4 | $this->set('dbName', trim($dbUrl['path'], '/')); |
|
119 | 4 | $this->set('dbUser', $dbUrl['user']); |
|
120 | 4 | $this->set('dbPassword', $dbUrl['pass']); |
|
121 | 4 | } |
|
122 | |||
123 | /** |
||
124 | * write the config |
||
125 | * |
||
126 | * @since 2.4.0 |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | |||
131 | 1 | public function write() : bool |
|
132 | { |
||
133 | 1 | $configKeys = array_keys(self::$_configArray); |
|
134 | 1 | $lastKey = end($configKeys); |
|
135 | |||
136 | /* collect content */ |
||
137 | |||
138 | 1 | $content = '<?php' . PHP_EOL . 'return' . PHP_EOL . '[' . PHP_EOL; |
|
139 | 1 | foreach (self::$_configArray as $key => $value) |
|
140 | { |
||
141 | 1 | if ($value) |
|
142 | { |
||
143 | 1 | $content .= ' \'' . $key . '\' => \'' . $value . '\''; |
|
144 | } |
||
145 | else |
||
146 | { |
||
147 | 1 | $content .= ' \'' . $key . '\' => null'; |
|
148 | } |
||
149 | 1 | if ($key !== $lastKey) |
|
150 | { |
||
151 | 1 | $content .= ','; |
|
152 | } |
||
153 | 1 | $content .= PHP_EOL; |
|
154 | } |
||
155 | 1 | $content .= '];'; |
|
156 | |||
157 | /* write content */ |
||
158 | |||
159 | 1 | return $this->_writeContent($content); |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * clear the config |
||
164 | * |
||
165 | * @since 3.0.0 |
||
166 | */ |
||
167 | |||
168 | 4 | public function clear() : void |
|
169 | { |
||
170 | 4 | self::$_configArray = []; |
|
171 | 4 | } |
|
172 | |||
173 | /** |
||
174 | * write content to file |
||
175 | * |
||
176 | * @since 2.4.0 |
||
177 | * |
||
178 | * @param string|null $content |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | |||
183 | 1 | protected function _writeContent(string $content = null) : bool |
|
184 | { |
||
185 | 1 | $filesystem = new Filesystem\File(); |
|
186 | 1 | $filesystem->init(dirname(self::$_configPath)); |
|
187 | 1 | return $filesystem->writeFile(basename (self::$_configPath), $content); |
|
188 | } |
||
189 | } |
||
190 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: