|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
namespace Xervice\Config; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Xervice\Config\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\XerviceConfig |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $instance; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Xervice\Config\XerviceConfigFactory |
|
23
|
|
|
*/ |
|
24
|
|
|
private $factory; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Xervice\Config\Parser\Parser |
|
28
|
|
|
*/ |
|
29
|
|
|
private $parser; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* XerviceConfig constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Xervice\Config\XerviceConfigFactory $factory |
|
35
|
|
|
*/ |
|
36
|
5 |
|
public function __construct(XerviceConfigFactory $factory) |
|
37
|
|
|
{ |
|
38
|
5 |
|
$this->factory = $factory; |
|
39
|
5 |
|
$this->parser = $this->factory->createParser(); |
|
40
|
5 |
|
$this->init(); |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return \Xervice\Config\XerviceConfig |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function getInstance(): XerviceConfig |
|
47
|
|
|
{ |
|
48
|
|
|
if (self::$instance === null) { |
|
49
|
|
|
self::$instance = new self(new XerviceConfigFactory()); |
|
50
|
|
|
} |
|
51
|
|
|
return self::$instance; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return \Xervice\Config\Container\ConfigContainer |
|
56
|
|
|
*/ |
|
57
|
5 |
|
public function getConfig(): ConfigContainer |
|
58
|
|
|
{ |
|
59
|
5 |
|
return $this->factory->getConfigContainer(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $file |
|
64
|
|
|
*/ |
|
65
|
5 |
|
private function parseFileIfExist(string $file): void |
|
66
|
|
|
{ |
|
67
|
5 |
|
$this->parser->parseFile($file); |
|
68
|
5 |
|
} |
|
69
|
|
|
|
|
70
|
5 |
|
private function init(): void |
|
71
|
|
|
{ |
|
72
|
5 |
|
$environment = $this->factory->createEnvironment(); |
|
73
|
5 |
|
$rootPath = getenv('APPLICATION_PATH') ?: getcwd(); |
|
74
|
5 |
|
$configDir = $rootPath . '/config/'; |
|
75
|
|
|
|
|
76
|
5 |
|
$this->parseFileIfExist($configDir . '/config_default.php'); |
|
77
|
5 |
|
$this->parseFileIfExist($configDir . '/config_' . $environment->getEnvironment() . '.php'); |
|
78
|
5 |
|
$this->parseFileIfExist($configDir . '/config_local.php'); |
|
79
|
|
|
|
|
80
|
5 |
|
$additionalFiles = (array) $this->getConfig()->get(self::ADDITIONAL_CONFIG_FILES, []); |
|
81
|
5 |
|
if ($additionalFiles) { |
|
|
|
|
|
|
82
|
5 |
|
foreach ($additionalFiles as $file) { |
|
83
|
5 |
|
$this->parseFileIfExist($file); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
5 |
|
$this->factory->getConfigContainer()->set(self::APPLICATION_PATH, $rootPath); |
|
88
|
5 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.