Total Complexity | 41 |
Total Lines | 262 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Config |
||
16 | { |
||
17 | private const BASE_DIR_MARKER = '<<<base-dir>>>'; |
||
18 | |||
19 | /** |
||
20 | * @var string config name |
||
21 | */ |
||
22 | private string $name; |
||
23 | |||
24 | /** |
||
25 | * @var array sources - paths to config source files |
||
26 | */ |
||
27 | private array $sources = []; |
||
28 | |||
29 | /** |
||
30 | * @var array config value |
||
31 | */ |
||
32 | protected array $values = []; |
||
33 | |||
34 | protected Builder $builder; |
||
35 | |||
36 | protected ContentWriter $contentWriter; |
||
37 | |||
38 | public function __construct(Builder $builder, string $name) |
||
39 | { |
||
40 | $this->builder = $builder; |
||
41 | $this->name = $name; |
||
42 | $this->contentWriter = new ContentWriter(); |
||
43 | } |
||
44 | |||
45 | public function clone(Builder $builder): self |
||
46 | { |
||
47 | $config = new Config($builder, $this->name); |
||
48 | $config->sources = $this->sources; |
||
49 | $config->values = $this->values; |
||
50 | |||
51 | return $config; |
||
52 | } |
||
53 | |||
54 | public function getValues(): array |
||
57 | } |
||
58 | |||
59 | public function load(array $paths = []): self |
||
60 | { |
||
61 | $this->sources = $this->loadFiles($paths); |
||
62 | |||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | private function loadFiles(array $paths): array |
||
67 | { |
||
68 | switch (count($paths)) { |
||
69 | case 0: |
||
70 | return []; |
||
71 | case 1: |
||
72 | return [$this->loadFile(reset($paths))]; |
||
73 | } |
||
74 | |||
75 | $configs = []; |
||
76 | foreach ($paths as $path) { |
||
77 | $cs = $this->loadFiles($this->glob($path)); |
||
78 | foreach ($cs as $config) { |
||
79 | if (!empty($config)) { |
||
80 | $configs[] = $config; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return $configs; |
||
86 | } |
||
87 | |||
88 | private function glob(string $path): array |
||
89 | { |
||
90 | if (strpos($path, '*') === false) { |
||
91 | return [$path]; |
||
92 | } |
||
93 | |||
94 | return glob($path); |
||
|
|||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Reads config file. |
||
99 | * |
||
100 | * @param string $path |
||
101 | * @return array configuration read from file |
||
102 | */ |
||
103 | protected function loadFile($path): array |
||
104 | { |
||
105 | $reader = ReaderFactory::get($this->builder, $path); |
||
106 | |||
107 | return $reader->read($path); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Merges given configs and writes at given name. |
||
112 | * |
||
113 | * @return Config |
||
114 | */ |
||
115 | public function build(): self |
||
116 | { |
||
117 | $this->values = $this->calcValues($this->sources); |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | public function write(): self |
||
123 | { |
||
124 | $this->writeFile($this->getOutputPath(), $this->values); |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | protected function calcValues(array $sources): array |
||
130 | { |
||
131 | $values = call_user_func_array([ConfigMergeHelper::class, 'mergeConfig'], $sources); |
||
132 | $values = Helper::fixConfig($values); |
||
133 | |||
134 | return $this->substituteOutputDirs($values); |
||
135 | } |
||
136 | |||
137 | protected function writeFile(string $path, array $data): void |
||
138 | { |
||
139 | $depth = $this->findDepth(); |
||
140 | $baseDir = $depth > 0 ? "dirname(__DIR__, $depth)" : '__DIR__'; |
||
141 | |||
142 | $content = $this->replaceMarkers(implode("\n\n", array_filter([ |
||
143 | 'header' => '<?php', |
||
144 | 'baseDir' => "\$baseDir = $baseDir;", |
||
145 | 'envs' => $this->envsRequired() |
||
146 | ? "\$_ENV = array_merge((array) require __DIR__ . '/envs.php', \$_ENV);" : '', |
||
147 | 'constants' => $this->constantsRequired() ? $this->builder->getConfig('constants')->buildRequires() : '', |
||
148 | 'params' => $this->paramsRequired() ? "\$params = require __DIR__ . '/params.php';" : '', |
||
149 | 'content' => $this->renderVars($data), |
||
150 | ]))); |
||
151 | $this->contentWriter->write($path, $content . "\n"); |
||
152 | } |
||
153 | |||
154 | public function envsRequired(): bool |
||
155 | { |
||
156 | return true; |
||
157 | } |
||
158 | |||
159 | public function constantsRequired(): bool |
||
160 | { |
||
161 | return true; |
||
162 | } |
||
163 | |||
164 | public function paramsRequired(): bool |
||
165 | { |
||
166 | return true; |
||
167 | } |
||
168 | |||
169 | private function findDepth(): int |
||
170 | { |
||
171 | $outDir = realpath(dirname($this->normalizePath($this->getOutputPath()))); |
||
172 | $diff = substr($outDir, strlen(realpath($this->getBaseDir()))); |
||
173 | |||
174 | return substr_count($diff, '/'); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param array $vars array to be exported |
||
179 | * @return string |
||
180 | * @throws ReflectionException |
||
181 | */ |
||
182 | private function renderVars(array $vars): string |
||
183 | { |
||
184 | return 'return ' . Helper::exportVar($vars) . ';'; |
||
185 | } |
||
186 | |||
187 | private function replaceMarkers(string $content): string |
||
188 | { |
||
189 | return str_replace( |
||
190 | ["'" . self::BASE_DIR_MARKER, "'?" . self::BASE_DIR_MARKER], |
||
191 | ["\$baseDir . '", "'?' . \$baseDir . '"], |
||
192 | $content |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Substitute output paths in given data array recursively with marker. |
||
198 | * |
||
199 | * @param array $data |
||
200 | * @return array |
||
201 | */ |
||
202 | protected function substituteOutputDirs(array $data): array |
||
203 | { |
||
204 | $dir = $this->normalizePath($this->getBaseDir()); |
||
205 | |||
206 | return $this->substitutePaths($data, $dir, self::BASE_DIR_MARKER); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Normalizes given path with given directory separator. |
||
211 | * Default forced to Unix directory separator for substitutePaths to work properly in Windows. |
||
212 | * |
||
213 | * @param string $path path to be normalized |
||
214 | * @param string $ds directory separator |
||
215 | * @return string |
||
216 | */ |
||
217 | private function normalizePath($path): string |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Substitute all paths in given array recursively with alias if applicable. |
||
224 | * |
||
225 | * @param array $data |
||
226 | * @param string $dir |
||
227 | * @param string $alias |
||
228 | * @return array |
||
229 | */ |
||
230 | private function substitutePaths($data, $dir, $alias): array |
||
231 | { |
||
232 | foreach ($data as &$value) { |
||
233 | if (is_string($value)) { |
||
234 | $value = $this->substitutePath($value, $dir, $alias); |
||
235 | } elseif (is_array($value)) { |
||
236 | $value = $this->substitutePaths($value, $dir, $alias); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | return $data; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Substitute path with alias if applicable. |
||
245 | * |
||
246 | * @param string $path |
||
247 | * @param string $dir |
||
248 | * @param string $alias |
||
249 | * @return string |
||
250 | */ |
||
251 | private function substitutePath($path, $dir, $alias): string |
||
267 | } |
||
268 | |||
269 | private function getBaseDir(): string |
||
270 | { |
||
271 | return $this->builder->getBaseDir(); |
||
272 | } |
||
273 | |||
274 | protected function getOutputPath(string $name = null): string |
||
275 | { |
||
276 | return $this->builder->getOutputPath($name ?: $this->name); |
||
277 | } |
||
278 | } |
||
279 |