1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Phalcon Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Phalcon\Config\Adapter; |
15
|
|
|
|
16
|
|
|
use Phalcon\Config; |
17
|
|
|
use Phalcon\Config\ConfigFactory; |
18
|
|
|
use Phalcon\Config\Exception; |
19
|
|
|
use Phalcon\Factory\Exception as ExceptionAlias; |
20
|
|
|
|
21
|
|
|
use function is_string; |
22
|
|
|
|
23
|
|
|
class Grouped extends Config |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Grouped constructor. |
27
|
|
|
* |
28
|
|
|
* @param array $arrayConfig |
29
|
|
|
* @param string $defaultAdapter |
30
|
|
|
* |
31
|
|
|
* @throws Exception |
32
|
|
|
* @throws ExceptionAlias |
33
|
|
|
*/ |
34
|
15 |
|
public function __construct(array $arrayConfig, string $defaultAdapter = "php") |
35
|
|
|
{ |
36
|
15 |
|
parent::__construct([]); |
37
|
|
|
|
38
|
15 |
|
foreach ($arrayConfig as $configName) { |
39
|
15 |
|
$configInstance = $configName; |
40
|
|
|
|
41
|
|
|
// Set to default adapter if passed as string |
42
|
15 |
|
if ($configName instanceof Config) { |
43
|
1 |
|
$this->merge($configInstance); |
44
|
1 |
|
continue; |
45
|
15 |
|
} elseif (is_string($configName)) { |
46
|
13 |
|
if ("" === $defaultAdapter) { |
47
|
1 |
|
$this->merge( |
48
|
1 |
|
(new ConfigFactory())->load($configName) |
49
|
|
|
); |
50
|
|
|
|
51
|
1 |
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$configInstance = [ |
55
|
13 |
|
"filePath" => $configName, |
56
|
13 |
|
"adapter" => $defaultAdapter, |
57
|
|
|
]; |
58
|
15 |
|
} elseif (!isset($configInstance["adapter"])) { |
59
|
1 |
|
$configInstance["adapter"] = $defaultAdapter; |
60
|
|
|
} |
61
|
|
|
|
62
|
15 |
|
$configInstance = $this->getConfigObject($configInstance); |
63
|
14 |
|
$this->merge($configInstance); |
64
|
|
|
} |
65
|
14 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $configInstance |
69
|
|
|
* |
70
|
|
|
* @return Config |
71
|
|
|
* @throws Exception |
72
|
|
|
*/ |
73
|
15 |
|
private function checkOptionsArray(array $configInstance): Config |
74
|
|
|
{ |
75
|
15 |
|
if (!isset($configInstance["config"])) { |
76
|
1 |
|
throw new Exception( |
77
|
|
|
"To use 'array' adapter you have to specify " . |
78
|
1 |
|
"the 'config' as an array." |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
14 |
|
$configArray = $configInstance["config"]; |
83
|
|
|
|
84
|
14 |
|
return new Config($configArray); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $configInstance |
89
|
|
|
* |
90
|
|
|
* @return Config |
91
|
|
|
* @throws Exception |
92
|
|
|
* @throws ExceptionAlias |
93
|
|
|
*/ |
94
|
15 |
|
private function getConfigObject(array $configInstance): Config |
95
|
|
|
{ |
96
|
15 |
|
if ("array" === $configInstance["adapter"]) { |
97
|
15 |
|
$configInstance = $this->checkOptionsArray($configInstance); |
98
|
|
|
} else { |
99
|
14 |
|
$configInstance = (new ConfigFactory())->load($configInstance); |
100
|
|
|
} |
101
|
|
|
|
102
|
14 |
|
return $configInstance; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|