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