|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
namespace Xervice\Config\Business; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Xervice\Config\Business\Container\ConfigContainer; |
|
9
|
|
|
|
|
10
|
|
|
class XerviceConfig |
|
11
|
|
|
{ |
|
12
|
|
|
public const APPLICATION_PATH = 'APPLICATION_PATH'; |
|
13
|
|
|
|
|
14
|
|
|
public const ADDITIONAL_CONFIG_FILES = 'ADDITIONAL_CONFIG_FILES'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Xervice\Config\Business\XerviceConfig |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $instance; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Xervice\Config\Business\ConfigBusinessFactory |
|
23
|
|
|
*/ |
|
24
|
|
|
private $factory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Xervice\Config\Business\Parser\Parser |
|
28
|
|
|
*/ |
|
29
|
|
|
private $parser; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* XerviceConfig constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Xervice\Config\Business\ConfigBusinessFactory $factory |
|
35
|
|
|
*/ |
|
36
|
14 |
|
public function __construct(ConfigBusinessFactory $factory) |
|
37
|
|
|
{ |
|
38
|
14 |
|
$this->factory = $factory; |
|
39
|
14 |
|
$this->parser = $this->factory->createParser(); |
|
40
|
14 |
|
$this->init(); |
|
41
|
14 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return \Xervice\Config\Business\XerviceConfig |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public static function getInstance(): XerviceConfig |
|
47
|
|
|
{ |
|
48
|
6 |
|
if (self::$instance === null) { |
|
49
|
1 |
|
self::$instance = new self(new ConfigBusinessFactory()); |
|
50
|
|
|
} |
|
51
|
6 |
|
return self::$instance; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $key |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
6 |
|
public static function get(string $key) |
|
60
|
|
|
{ |
|
61
|
6 |
|
return self::getInstance()->getConfig()->get($key); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
* |
|
68
|
|
|
* @return \Xervice\Config\Business\Container\ConfigContainer |
|
69
|
|
|
*/ |
|
70
|
6 |
|
public static function set(string $key, $value) |
|
71
|
|
|
{ |
|
72
|
6 |
|
return self::getInstance()->getConfig()->set($key, $value); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return \Xervice\Config\Business\Container\ConfigContainer |
|
77
|
|
|
*/ |
|
78
|
14 |
|
public function getConfig(): ConfigContainer |
|
79
|
|
|
{ |
|
80
|
14 |
|
return $this->factory->getConfigContainer(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $file |
|
85
|
|
|
*/ |
|
86
|
14 |
|
private function parseFileIfExist(string $file): void |
|
87
|
|
|
{ |
|
88
|
14 |
|
$this->parser->parseFile($file); |
|
89
|
14 |
|
} |
|
90
|
|
|
|
|
91
|
14 |
|
private function init(): void |
|
92
|
|
|
{ |
|
93
|
14 |
|
$environment = $this->factory->createEnvironment(); |
|
94
|
14 |
|
$rootPath = $this->getRootPath(); |
|
95
|
14 |
|
$configDir = $this->getConfigPath($rootPath); |
|
96
|
|
|
|
|
97
|
14 |
|
$this->parseFileIfExist($configDir . '/config_default.php'); |
|
98
|
14 |
|
$this->parseFileIfExist($configDir . '/config_' . $environment->getEnvironment() . '.php'); |
|
99
|
14 |
|
$this->parseFileIfExist( |
|
100
|
14 |
|
$configDir . '/config_' . $environment->getEnvironment() . '_' . $environment->getScope() . '.php' |
|
101
|
|
|
); |
|
102
|
14 |
|
$this->parseFileIfExist($configDir . '/config_local.php'); |
|
103
|
|
|
|
|
104
|
14 |
|
$this->parseAdditionalFiles(); |
|
105
|
|
|
|
|
106
|
14 |
|
$this->factory->getConfigContainer()->set(self::APPLICATION_PATH, $rootPath); |
|
107
|
14 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
14 |
|
private function getRootPath(): string |
|
113
|
|
|
{ |
|
114
|
14 |
|
return getenv('APPLICATION_PATH') ?: getcwd(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $rootPath |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
14 |
|
private function getConfigPath($rootPath): string |
|
123
|
|
|
{ |
|
124
|
14 |
|
return getenv('CONFIG_PATH') ?: $rootPath . '/config/'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
14 |
|
private function parseAdditionalFiles(): void |
|
128
|
|
|
{ |
|
129
|
14 |
|
$additionalFiles = (array)$this->getConfig()->get(self::ADDITIONAL_CONFIG_FILES, []); |
|
130
|
14 |
|
if ($additionalFiles) { |
|
|
|
|
|
|
131
|
13 |
|
foreach ($additionalFiles as $file) { |
|
132
|
13 |
|
$this->parseFileIfExist($file); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
14 |
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.