1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace MazenTouati\Simple2wayConfig; |
4
|
|
|
|
5
|
|
|
class S2WConfig implements S2WConfigInterface |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* a directory where the configuration files are served. |
10
|
|
|
* |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $directory = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* a runtime instance of the configuration |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $config = []; |
21
|
|
|
|
22
|
1 |
|
public function __construct(string $directory) |
23
|
|
|
{ |
24
|
1 |
|
$this->directory = $directory; |
25
|
1 |
|
} |
26
|
|
|
|
27
|
1 |
|
public function get(string $path, $default = null) |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
// when the path is empty or has only one part |
31
|
1 |
|
$explodedPath = explode('.', $path, 2); |
32
|
1 |
|
if (count($explodedPath) != 2) { |
33
|
1 |
|
return $default; |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
list($fileName, $valuePath) = $explodedPath; |
37
|
|
|
|
38
|
1 |
|
if (!isset($this->config[$fileName])) { |
39
|
1 |
|
$this->config[$fileName] = $this->loadConfigurationFile($fileName); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
$resolvedValue = $this->config[$fileName]; |
43
|
|
|
|
44
|
1 |
View Code Duplication |
foreach (explode('.', $valuePath) as $key) { |
|
|
|
|
45
|
1 |
|
if (isset($resolvedValue[$key])) { |
46
|
1 |
|
$resolvedValue = $resolvedValue[$key]; |
47
|
|
|
} else { |
48
|
1 |
|
$resolvedValue = $default; |
49
|
1 |
|
break; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $resolvedValue; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function set(string $path, $value) |
57
|
|
|
{ |
58
|
1 |
|
list($fileName, $valuePath) = explode('.', $path, 2); |
59
|
1 |
|
if (!isset($this->config[$fileName])) { |
60
|
1 |
|
$this->config[$fileName] = $this->loadConfigurationFile($fileName); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$reference = &$this->config[$fileName]; |
64
|
|
|
|
65
|
1 |
View Code Duplication |
foreach (explode('.', $valuePath) as $key) { |
|
|
|
|
66
|
1 |
|
if (!isset($reference[$key])) { |
67
|
1 |
|
$reference[$key] = []; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
$reference = &$reference[$key]; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$reference = $value; |
74
|
1 |
|
} |
75
|
|
|
|
76
|
1 |
|
public function sync($specificConfiguration = false) |
77
|
|
|
{ |
78
|
1 |
|
$toSync = []; |
79
|
|
|
|
80
|
1 |
|
if ($specificConfiguration !== false) { |
81
|
1 |
|
$toSync[$specificConfiguration] = &$this->config[$specificConfiguration]; |
82
|
|
|
} else { |
83
|
1 |
|
$toSync = &$this->config; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
foreach ($toSync as $fileName => $configuration) { |
87
|
1 |
|
$filePath = $this->directory . DIRECTORY_SEPARATOR . $fileName; |
88
|
1 |
|
if (is_file("{$filePath}.php") |
89
|
1 |
|
&& !copy("{$filePath}.php", "{$filePath}.backup.php")) { |
90
|
1 |
|
throw S2WConfigException::backupFail("{$filePath}.php"); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$content = "<?php " . PHP_EOL |
94
|
1 |
|
."return ".varexport($toSync[$fileName], true).';' |
95
|
1 |
|
.PHP_EOL; |
96
|
|
|
|
97
|
1 |
|
file_put_contents("{$filePath}.php", $content); |
98
|
|
|
} |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
public function loadConfigurationFile(string $fileName) |
102
|
|
|
{ |
103
|
1 |
|
$filePath = $this->directory . DIRECTORY_SEPARATOR . $fileName . '.php'; |
104
|
1 |
|
return is_file($filePath) ? include $filePath : []; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.