1
|
|
|
<?php namespace Limoncello\Core\Config; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Core\Contracts\Config\ConfigInterface; |
20
|
|
|
use Limoncello\Core\Contracts\Config\ConfigManagerInterface; |
21
|
|
|
use ReflectionClass; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Limoncello\Core |
25
|
|
|
*/ |
26
|
|
|
class ConfigManager implements ConfigManagerInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $globConfigPatterns; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $globFlags = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $configNamespace = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $globConfigPatterns |
45
|
|
|
*/ |
46
|
1 |
|
public function __construct($globConfigPatterns = '*.php') |
47
|
|
|
{ |
48
|
1 |
|
$this->setGlobConfigPatterns($globConfigPatterns); |
49
|
1 |
|
$this->setGlobFlags(0); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
1 |
|
public function loadConfigs($configNamespace, $pathToDirectory) |
56
|
|
|
{ |
57
|
1 |
|
$this->setConfigNamespace($configNamespace); |
58
|
|
|
|
59
|
1 |
|
$configs = $this->loadConfigsImpl($pathToDirectory, $this->getGlobConfigPatterns()); |
60
|
1 |
|
$result = new ArrayConfig($configs); |
61
|
|
|
|
62
|
1 |
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $configsPath |
67
|
|
|
* @param string $globConfigPatterns |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
1 |
|
protected function loadConfigsImpl($configsPath, $globConfigPatterns) |
72
|
|
|
{ |
73
|
1 |
|
assert(is_string($configsPath) === true && empty($configsPath) === false); |
74
|
1 |
|
assert(is_string($globConfigPatterns) === true && empty($globConfigPatterns) === false); |
75
|
|
|
|
76
|
1 |
|
$configsPath = realpath($configsPath); |
77
|
|
|
|
78
|
1 |
|
foreach (glob($configsPath . DIRECTORY_SEPARATOR . $globConfigPatterns, $this->getGlobFlags()) as $fileName) { |
79
|
|
|
/** @noinspection PhpIncludeInspection */ |
80
|
1 |
|
require_once $fileName; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$globalConfigs = []; |
84
|
1 |
|
foreach (get_declared_classes() as $class) { |
85
|
1 |
|
if ($this->isConfigClassToRead($class) === true) { |
86
|
1 |
|
$this->readConfigsFromClass($class, $globalConfigs); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $globalConfigs; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
1 |
|
protected function getGlobConfigPatterns() |
97
|
|
|
{ |
98
|
1 |
|
return $this->globConfigPatterns; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $globConfigPatterns |
103
|
|
|
*/ |
104
|
1 |
|
protected function setGlobConfigPatterns($globConfigPatterns) |
105
|
|
|
{ |
106
|
1 |
|
assert(is_string($globConfigPatterns) === true && empty($globConfigPatterns) === false); |
107
|
1 |
|
$this->globConfigPatterns = $globConfigPatterns; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
1 |
|
protected function getGlobFlags() |
114
|
|
|
{ |
115
|
1 |
|
return $this->globFlags; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param int $globFlags |
120
|
|
|
*/ |
121
|
1 |
|
protected function setGlobFlags($globFlags) |
122
|
|
|
{ |
123
|
1 |
|
assert(is_int($globFlags)); |
124
|
1 |
|
$this->globFlags = $globFlags; |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
1 |
|
protected function getConfigNamespace() |
131
|
|
|
{ |
132
|
1 |
|
return $this->configNamespace; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $configNamespace |
137
|
|
|
*/ |
138
|
1 |
|
protected function setConfigNamespace($configNamespace) |
139
|
|
|
{ |
140
|
1 |
|
assert(is_string($configNamespace) === true && empty($configNamespace) === false); |
141
|
1 |
|
$this->configNamespace = $configNamespace; |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
1 |
|
protected function hasConfigNamespace() |
148
|
|
|
{ |
149
|
1 |
|
return empty($this->getConfigNamespace()) === false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $class |
154
|
|
|
* @param $globalConfigs |
155
|
|
|
*/ |
156
|
1 |
|
private function readConfigsFromClass($class, &$globalConfigs) |
157
|
|
|
{ |
158
|
|
|
/** @var ConfigInterface $config */ |
159
|
1 |
|
$config = new $class(); |
160
|
1 |
|
foreach ($config->getConfigInterfaces() as $interface) { |
161
|
1 |
|
$globalConfigs[$interface] = $config->getConfig($interface); |
162
|
|
|
} |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $class |
167
|
|
|
* |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
1 |
|
private function isConfigClassToRead($class) |
171
|
|
|
{ |
172
|
1 |
|
$reflection = new ReflectionClass($class); |
173
|
1 |
|
$shouldBeInNamespace = $this->hasConfigNamespace(); |
174
|
|
|
$isConfigToRead = |
175
|
1 |
|
$reflection->inNamespace() === $shouldBeInNamespace && |
176
|
1 |
|
($shouldBeInNamespace === false || $reflection->getNamespaceName() === $this->getConfigNamespace()) && |
177
|
1 |
|
$reflection->implementsInterface(ConfigInterface::class) === true; |
178
|
|
|
|
179
|
1 |
|
return $isConfigToRead; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|