1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phwoolcon\TestStarter; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
6
|
|
|
|
7
|
|
|
class ResourceAggregator |
8
|
|
|
{ |
9
|
|
|
const FLAG_CONTINUE = 0b0001; |
10
|
|
|
const FLAG_SPACE_PAD = 0b0010; |
11
|
|
|
const FLAG_SPACE_PAD_CONTINUE = 0b0011; |
12
|
|
|
|
13
|
|
|
protected $cliOutput; |
14
|
|
|
protected $packages = []; |
15
|
|
|
protected $rootDir; |
16
|
|
|
protected $testRootDir; |
17
|
|
|
|
18
|
1 |
|
public function __construct($rootDir) |
19
|
|
|
{ |
20
|
1 |
|
$this->rootDir = $rootDir; |
21
|
1 |
|
$this->testRootDir = $rootDir . '/tests/root'; |
22
|
1 |
|
} |
23
|
|
|
|
24
|
1 |
|
public function aggregate() |
25
|
|
|
{ |
26
|
1 |
|
$this->resetTestRoot(); |
27
|
1 |
|
$this->detectPackages(); |
28
|
|
|
|
29
|
1 |
|
$this->installAliases(); |
30
|
1 |
|
$this->installConfig(); |
31
|
1 |
|
$this->installDi(); |
32
|
1 |
|
$this->installLocale(); |
33
|
1 |
|
$this->installMigrations(); |
34
|
1 |
|
$this->installRoutes(); |
35
|
1 |
|
$this->installViews(); |
36
|
1 |
|
$this->appendLocalResources(); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
|
protected function appendLocalResources() |
40
|
|
|
{ |
41
|
1 |
|
$this->message('Appending local resources...', static::FLAG_SPACE_PAD_CONTINUE); |
42
|
1 |
|
$source = $this->rootDir . '/tests/resource'; |
43
|
1 |
|
$destination = $this->testRootDir; |
44
|
1 |
|
symlinkDirOverride($source, $destination); |
45
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
1 |
|
protected function detectPackages() |
49
|
|
|
{ |
50
|
1 |
|
$this->message('Detecting packages...', static::FLAG_SPACE_PAD_CONTINUE); |
51
|
1 |
|
$packageFiles = detectPhwoolconPackageFiles($this->rootDir . '/vendor'); |
52
|
1 |
|
$packageFiles = array_merge($packageFiles, glob($this->rootDir . '/phwoolcon-package/*package*.php')); |
53
|
1 |
|
foreach ($packageFiles as $file) { |
54
|
|
|
// @codeCoverageIgnoreStart |
55
|
|
|
if (!is_array($package = include($file))) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
// @codeCoverageIgnoreEnd |
59
|
1 |
|
$path = dirname(dirname($file)); |
60
|
1 |
|
$package['path'] = $path; |
61
|
1 |
|
$this->packages[$file] = $package; |
62
|
|
|
} |
63
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
View Code Duplication |
protected function installAliases() |
|
|
|
|
67
|
|
|
{ |
68
|
1 |
|
$this->message('Installing class aliases...', static::FLAG_SPACE_PAD_CONTINUE); |
69
|
1 |
|
$aliases = []; |
70
|
1 |
|
foreach ($this->packages as $packageFile => $package) { |
71
|
1 |
|
foreach ($package as $name => $resources) { |
72
|
1 |
|
if (empty($resources['class_aliases']) || !is_array($resources['class_aliases'])) { |
73
|
1 |
|
continue; |
74
|
|
|
} |
75
|
1 |
|
foreach ($resources['class_aliases'] as $sort => $detectedAliases) { |
76
|
1 |
|
$aliases[$sort . '-' . $name] = $detectedAliases; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
1 |
|
$target = $this->testRootDir . '/vendor/phwoolcon/class_aliases.php'; |
81
|
1 |
|
fileSaveArray($target, arraySortedMerge($aliases)); |
82
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
protected function installConfig() |
86
|
|
|
{ |
87
|
1 |
|
$this->message('Installing config...', static::FLAG_SPACE_PAD_CONTINUE); |
88
|
1 |
|
$configPath = $this->testRootDir . '/app/config'; |
89
|
1 |
|
foreach ($this->packages as $packageFile => $package) { |
90
|
1 |
|
$path = $package['path']; |
91
|
1 |
View Code Duplication |
if ($configFiles = glob($path . '/phwoolcon-package/config/*.php')) { |
|
|
|
|
92
|
1 |
|
foreach ($configFiles as $source) { |
93
|
1 |
|
$configFile = basename($source); |
94
|
1 |
|
$destination = $configPath . '/' . $configFile; |
95
|
1 |
|
is_file($destination) and unlink($destination); |
|
|
|
|
96
|
1 |
|
symlinkRelative($source, $destination); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
if ($overrides = glob($path . '/phwoolcon-package/config/override-*/*.php')) { |
101
|
1 |
|
foreach ($overrides as $override) { |
102
|
1 |
|
$configFile = basename($override); |
103
|
1 |
|
$dir = basename(dirname($override)); |
104
|
1 |
|
$destination = $configPath . '/' . $dir . '/' . $configFile; |
105
|
1 |
|
is_file($destination) and unlink($destination); |
|
|
|
|
106
|
1 |
|
symlinkRelative($override, $destination); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
View Code Duplication |
protected function installDi() |
|
|
|
|
114
|
|
|
{ |
115
|
1 |
|
$this->message('Installing DI...', static::FLAG_SPACE_PAD_CONTINUE); |
116
|
1 |
|
$diFiles = []; |
117
|
1 |
|
foreach ($this->packages as $packageFile => $package) { |
118
|
1 |
|
$path = $package['path']; |
119
|
1 |
|
foreach ($package as $name => $resources) { |
120
|
1 |
|
if (empty($resources['di'])) { |
121
|
1 |
|
continue; |
122
|
|
|
} |
123
|
1 |
|
foreach ((array)$resources['di'] as $sort => $file) { |
124
|
1 |
|
$diFiles[$sort][] = $path . '/phwoolcon-package/' . $file; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
1 |
|
$target = $this->testRootDir . '/vendor/phwoolcon/di.php'; |
129
|
1 |
|
fileSaveInclude($target, arraySortedMerge($diFiles)); |
130
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
1 |
View Code Duplication |
protected function installLocale() |
|
|
|
|
134
|
|
|
{ |
135
|
1 |
|
$this->message('Installing locales...', static::FLAG_SPACE_PAD_CONTINUE); |
136
|
1 |
|
$localePath = $this->testRootDir . '/app/locale/'; |
137
|
1 |
|
foreach ($this->packages as $packageFile => $package) { |
138
|
1 |
|
$path = $package['path']; |
139
|
1 |
|
if ($items = glob($path . '/phwoolcon-package/locale/*')) { |
140
|
1 |
|
foreach ($items as $source) { |
141
|
1 |
|
$destination = $localePath . basename($source); |
142
|
1 |
|
symlinkDirOverride($source, $destination); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
147
|
1 |
|
} |
148
|
|
|
|
149
|
1 |
|
protected function installMigrations() |
150
|
|
|
{ |
151
|
1 |
|
$this->message('Installing migrations...', static::FLAG_SPACE_PAD_CONTINUE); |
152
|
1 |
|
$migrationPath = $this->testRootDir . '/bin/migrations/'; |
153
|
1 |
|
foreach ($this->packages as $packageFile => $package) { |
154
|
1 |
|
$path = $package['path']; |
155
|
1 |
View Code Duplication |
if ($items = glob($path . '/phwoolcon-package/migrations/*')) { |
|
|
|
|
156
|
|
|
// @codeCoverageIgnoreStart |
157
|
|
|
foreach ($items as $source) { |
158
|
|
|
$destination = $migrationPath . basename($source); |
159
|
|
|
is_file($destination) and unlink($destination); |
|
|
|
|
160
|
|
|
symlinkRelative($source, $destination); |
161
|
|
|
} |
162
|
|
|
// @codeCoverageIgnoreEnd |
163
|
|
|
} |
164
|
|
|
} |
165
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
View Code Duplication |
protected function installRoutes() |
|
|
|
|
169
|
|
|
{ |
170
|
1 |
|
$this->message('Installing routes...', static::FLAG_SPACE_PAD_CONTINUE); |
171
|
1 |
|
$routeFiles = []; |
172
|
1 |
|
foreach ($this->packages as $packageFile => $package) { |
173
|
1 |
|
$path = $package['path']; |
174
|
1 |
|
foreach ($package as $name => $resources) { |
175
|
1 |
|
if (empty($resources['routes'])) { |
176
|
1 |
|
continue; |
177
|
|
|
} |
178
|
|
|
// @codeCoverageIgnoreStart |
179
|
|
|
foreach ((array)$resources['routes'] as $sort => $file) { |
180
|
|
|
$routeFiles[$sort][] = $path . '/phwoolcon-package/' . $file; |
181
|
|
|
} |
182
|
|
|
// @codeCoverageIgnoreEnd |
183
|
|
|
} |
184
|
|
|
} |
185
|
1 |
|
$target = $this->testRootDir . '/vendor/phwoolcon/routes.php'; |
186
|
1 |
|
fileSaveInclude($target, arraySortedMerge($routeFiles)); |
187
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
188
|
1 |
|
} |
189
|
|
|
|
190
|
1 |
View Code Duplication |
protected function installViews() |
|
|
|
|
191
|
|
|
{ |
192
|
1 |
|
$this->message('Installing views...', static::FLAG_SPACE_PAD_CONTINUE); |
193
|
1 |
|
$viewPath = $this->testRootDir . '/app/views/'; |
194
|
1 |
|
foreach ($this->packages as $packageFile => $package) { |
195
|
1 |
|
$path = $package['path']; |
196
|
1 |
|
if ($items = glob($path . '/phwoolcon-package/views/*')) { |
197
|
1 |
|
foreach ($items as $source) { |
198
|
1 |
|
$destination = $viewPath . basename($source); |
199
|
1 |
|
symlinkDirOverride($source, $destination); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
204
|
1 |
|
} |
205
|
|
|
|
206
|
1 |
|
protected function message($message, $flag = null) |
207
|
|
|
{ |
208
|
1 |
|
$eol = !($flag & static::FLAG_CONTINUE); |
209
|
1 |
|
$spacePad = $flag & static::FLAG_SPACE_PAD; |
210
|
1 |
|
if (!$this->cliOutput) { |
211
|
1 |
|
$this->cliOutput = new ConsoleOutput(); |
212
|
|
|
} |
213
|
1 |
|
$message = is_array($message) ? implode(PHP_EOL, $message) : $message; |
214
|
1 |
|
$message .= $spacePad ? $this->spacePad($message) : ''; |
215
|
1 |
|
$this->cliOutput->write($message, $eol); |
216
|
1 |
|
} |
217
|
|
|
|
218
|
1 |
|
protected function resetTestRoot() |
219
|
|
|
{ |
220
|
1 |
|
$this->message('Resetting test root...', static::FLAG_SPACE_PAD_CONTINUE); |
221
|
1 |
|
removeDir($this->testRootDir); |
222
|
|
|
$assembleDirs = [ |
223
|
1 |
|
'/app/config', |
224
|
|
|
'/app/locale', |
225
|
|
|
'/app/views', |
226
|
|
|
'/bin/migrations', |
227
|
|
|
'/storage/cache', |
228
|
|
|
'/storage/logs', |
229
|
|
|
'/storage/session', |
230
|
|
|
'/storage/remote-coverage', |
231
|
|
|
'/vendor/phwoolcon', |
232
|
|
|
]; |
233
|
1 |
|
foreach ($assembleDirs as $dir) { |
234
|
1 |
|
mkdir($this->testRootDir . $dir, 0777, true); |
235
|
|
|
} |
236
|
|
|
$mockCompiledFiles = [ |
237
|
1 |
|
'/vendor/phwoolcon/assets.php', |
238
|
|
|
'/vendor/phwoolcon/admin_assets.php', |
239
|
|
|
'/vendor/phwoolcon/commands.php', |
240
|
|
|
]; |
241
|
1 |
|
foreach ($mockCompiledFiles as $mockFile) { |
242
|
1 |
|
$filename = $this->testRootDir . $mockFile; |
243
|
1 |
|
fileSaveArray($filename, []); |
244
|
|
|
} |
245
|
1 |
|
$this->message(' <info>[ OK ]</info>'); |
246
|
1 |
|
} |
247
|
|
|
|
248
|
1 |
|
protected function spacePad($str, $length = 40) |
249
|
|
|
{ |
250
|
1 |
|
$spaces = $length - strlen($str); |
251
|
1 |
|
return $spaces > 0 ? str_repeat(' ', $spaces) : ' '; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.