1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Contains Wiring class. |
5
|
|
|
* |
6
|
|
|
* PHP version 7.0+ |
7
|
|
|
* |
8
|
|
|
* LICENSE: |
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
11
|
|
|
* database. |
12
|
|
|
* Copyright (C) 2014-2016 Michael Cummings |
13
|
|
|
* |
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
17
|
|
|
* option) any later version. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
22
|
|
|
* for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
25
|
|
|
* along with this program. If not, see |
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
27
|
|
|
* |
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
30
|
|
|
* |
31
|
|
|
* @copyright 2014-2016 Michael Cummings |
32
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html GNU LGPL |
33
|
|
|
* @author Michael Cummings <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
namespace Yapeal\Configuration; |
36
|
|
|
|
37
|
|
|
use FilePathNormalizer\FilePathNormalizerTrait; |
38
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
39
|
|
|
use Symfony\Component\Yaml\Parser; |
40
|
|
|
use Yapeal\Container\ContainerInterface; |
41
|
|
|
use Yapeal\Exception\YapealException; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Class Wiring |
45
|
|
|
*/ |
46
|
|
|
class Wiring |
47
|
|
|
{ |
48
|
|
|
use FilePathNormalizerTrait; |
49
|
|
|
/** |
50
|
|
|
* @param ContainerInterface $dic |
51
|
|
|
*/ |
52
|
1 |
|
public function __construct(ContainerInterface $dic) |
53
|
|
|
{ |
54
|
1 |
|
$this->dic = $dic; |
55
|
|
|
} |
56
|
|
|
/** |
57
|
|
|
* @param string $configFile |
58
|
|
|
* @param array $settings |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
* @throws \DomainException |
62
|
|
|
* @throws \Yapeal\Exception\YapealException |
63
|
|
|
*/ |
64
|
|
|
public function parserConfigFile(string $configFile, array $settings = []): array |
65
|
|
|
{ |
66
|
|
|
if (!is_readable($configFile) || !is_file($configFile)) { |
67
|
|
|
return $settings; |
68
|
|
|
} |
69
|
|
|
try { |
70
|
|
|
/** |
71
|
|
|
* @var \RecursiveIteratorIterator|\Traversable $rItIt |
72
|
|
|
*/ |
73
|
|
|
$rItIt = new \RecursiveIteratorIterator(new \RecursiveArrayIterator((new Parser())->parse(file_get_contents($configFile), |
|
|
|
|
74
|
|
|
true, |
75
|
|
|
false))); |
76
|
|
|
} catch (ParseException $exc) { |
77
|
|
|
$mess = sprintf('Unable to parse the YAML configuration file %2$s. The error message was %1$s', |
78
|
|
|
$exc->getMessage(), |
79
|
|
|
$configFile); |
80
|
|
|
throw new YapealException($mess, 0, $exc); |
81
|
|
|
} |
82
|
|
|
foreach ($rItIt as $leafValue) { |
83
|
|
|
$keys = []; |
84
|
|
|
foreach (range(0, $rItIt->getDepth()) as $depth) { |
85
|
|
|
$keys[] = $rItIt->getSubIterator($depth) |
86
|
|
|
->key(); |
87
|
|
|
} |
88
|
|
|
$settings[implode('.', $keys)] = $leafValue; |
89
|
|
|
} |
90
|
|
|
return $this->doSubs($settings); |
91
|
|
|
} |
92
|
|
|
/** |
93
|
|
|
* @return self Fluent interface. |
94
|
|
|
* @throws \LogicException |
95
|
|
|
*/ |
96
|
|
|
public function wireAll() |
97
|
|
|
{ |
98
|
|
|
$dic = $this->dic; |
99
|
|
|
$base = 'Yapeal.Wiring.Handlers.'; |
100
|
|
|
$names = ['Config', 'Error', 'Event', 'Log', 'Sql', 'Xml', 'Xsd', 'Xsl', 'FileSystem', 'Network', 'EveApi']; |
101
|
|
|
/** |
102
|
|
|
* @var WiringInterface $class |
103
|
|
|
*/ |
104
|
|
|
foreach ($names as $name) { |
105
|
|
|
$setting = $base . strtolower($name); |
106
|
|
|
if (!empty($dic[$setting]) |
107
|
|
|
&& is_subclass_of($dic[$setting], '\\Yapeal\\Configuration\\WiringInterface', true) |
108
|
|
|
) { |
109
|
|
|
$class = new $dic[$setting]; |
110
|
|
|
$class->wire($dic); |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
$methodName = 'wire' . $name; |
114
|
|
|
if (method_exists($this, $methodName)) { |
115
|
|
|
$this->$methodName(); |
116
|
|
|
} else { |
117
|
|
|
$mess = 'Could NOT find class or method for ' . $name; |
118
|
|
|
throw new \LogicException($mess); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
/** |
124
|
|
|
* @param array $settings |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
* @throws \DomainException |
128
|
|
|
*/ |
129
|
|
|
protected function doSubs(array $settings): array |
130
|
|
|
{ |
131
|
|
|
if (0 === count($settings)) { |
132
|
|
|
return []; |
133
|
|
|
} |
134
|
|
|
$depth = 0; |
135
|
|
|
$maxDepth = 10; |
136
|
|
|
$regEx = '/(?<all>\{(?<name>Yapeal(?:\.\w+)+)\})/'; |
137
|
|
|
$dic = $this->dic; |
138
|
|
|
do { |
139
|
|
|
$settings = preg_replace_callback($regEx, |
140
|
|
|
function ($match) use ($settings, $dic) { |
141
|
|
|
if (!empty($settings[$match['name']])) { |
142
|
|
|
return $settings[$match['name']]; |
143
|
|
|
} |
144
|
|
|
if (!empty($dic[$match['name']])) { |
145
|
|
|
return $dic[$match['name']]; |
146
|
|
|
} |
147
|
|
|
return $match['all']; |
148
|
|
|
}, |
149
|
|
|
$settings, |
150
|
|
|
-1, |
151
|
|
|
$count); |
152
|
|
|
if (++$depth > $maxDepth) { |
153
|
|
|
$mess = 'Exceeded maximum depth, check for possible circular reference(s)'; |
154
|
|
|
throw new \DomainException($mess); |
155
|
|
|
} |
156
|
|
|
$lastError = preg_last_error(); |
157
|
|
|
if (PREG_NO_ERROR !== $lastError) { |
158
|
|
|
$constants = array_flip(get_defined_constants(true)['pcre']); |
159
|
|
|
$lastError = $constants[$lastError]; |
160
|
|
|
$mess = 'Received preg error ' . $lastError; |
161
|
|
|
throw new \DomainException($mess); |
162
|
|
|
} |
163
|
|
|
} while ($count > 0); |
164
|
|
|
return $settings; |
165
|
|
|
} |
166
|
|
|
/** |
167
|
|
|
* @return Wiring Fluent interface. |
168
|
|
|
* @throws \DomainException |
169
|
|
|
* @throws \Yapeal\Exception\YapealException |
170
|
|
|
*/ |
171
|
|
|
protected function wireConfig(): Wiring |
172
|
|
|
{ |
173
|
|
|
$dic = $this->dic; |
174
|
|
|
$fpn = $this->getFpn(); |
175
|
|
|
$path = $fpn->normalizePath(dirname(dirname(__DIR__))); |
176
|
|
|
if (empty($dic['Yapeal.baseDir'])) { |
177
|
|
|
$dic['Yapeal.baseDir'] = $path; |
178
|
|
|
} |
179
|
|
|
if (empty($dic['Yapeal.libDir'])) { |
180
|
|
|
$dic['Yapeal.libDir'] = $path . 'lib/'; |
181
|
|
|
} |
182
|
|
|
$configFiles = [ |
183
|
|
|
$fpn->normalizeFile(__DIR__ . '/yapeal_defaults.yaml'), |
184
|
|
|
$fpn->normalizeFile($dic['Yapeal.baseDir'] . 'config/yapeal.yaml') |
185
|
|
|
]; |
186
|
|
|
$vendorPos = strpos($path, 'vendor/'); |
187
|
|
|
if (false !== $vendorPos) { |
188
|
|
|
$dic['Yapeal.vendorParentDir'] = substr($path, 0, $vendorPos); |
189
|
|
|
$configFiles[] = $fpn->normalizeFile($dic['Yapeal.vendorParentDir'] . 'config/yapeal.yaml'); |
190
|
|
|
} |
191
|
|
|
$settings = []; |
192
|
|
|
// Process each file in turn so any substitutions are done in a more |
193
|
|
|
// consistent way. |
194
|
|
|
foreach ($configFiles as $configFile) { |
195
|
|
|
$settings = $this->parserConfigFile($configFile, $settings); |
196
|
|
|
} |
197
|
|
|
if (0 !== count($settings)) { |
198
|
|
|
// Assure NOT overwriting already existing settings from previously processed CLI or application settings. |
199
|
|
|
foreach ($settings as $key => $value) { |
200
|
|
|
$dic[$key] = $dic[$key] ?? $value; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
/** |
206
|
|
|
* @var ContainerInterface $dic |
207
|
|
|
*/ |
208
|
|
|
protected $dic; |
209
|
|
|
} |
210
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.