Repository navigation
Expand file tree
/
Copy pathinit.php
More file actions
326 lines (234 loc) · 11.3 KB
/
Copy pathinit.php
File metadata and controls
326 lines (234 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env php
<?php
use BrickLayer\Lay\BobDBuilder\Helper\Console\Console;
use BrickLayer\Lay\BobDBuilder\Helper\Console\Format\Foreground;
use BrickLayer\Lay\Core\LayConfig;
use BrickLayer\Lay\Libs\Dir\LayDir;
use BrickLayer\Lay\Libs\LayArray;
use BrickLayer\Lay\Libs\LayFn;
use BrickLayer\Lay\Libs\Primitives\Enums\LayLoop;
const SAFE_TO_INIT_LAY = true;
include_once __DIR__ . "/../../../foundation.php";
$project = LayConfig::server_data();
if(!file_exists($project->root . "phinx.php"))
copy(__DIR__ . "/phinx.php", $project->root . "phinx.php");
$config = $project->root . "elevator-config.json";
if(!file_exists($config)) {
copy(__DIR__ . DIRECTORY_SEPARATOR . "elevator-config.json", $config);
Console::log(
"Noticed an Elevator config file was missing, so one was created for you the root of your project.
\nPlease review it, add the modules you want and rerun this command",
Foreground::purple
);
Console::log($config);
return;
}
define("ELV_CONFIG", json_decode(file_get_contents($config), true));
$db_dir = $project->db;
$brick_root = $project->bricks . "Elevator" . DIRECTORY_SEPARATOR;
$config = ELV_CONFIG['config'];
$modules = ELV_CONFIG['modules'];
$list_modules = LayFn::extract_cli_tag("--modules", false) ?? LayFn::extract_cli_tag("--help", false) ?? false;
$overwrite_hooks = LayFn::extract_cli_tag("--force", false) ?? $config['overwrite_hooks'] ?? false;
$overwrite_migrations = LayFn::extract_cli_tag("--force-migrations", false) ?? $config['overwrite_migrations'] ?? false;
$overwrite_foundation = LayFn::extract_cli_tag("--force-foundation", false) ?? $config['overwrite_foundation'] ?? false;
$ignore_hooks = LayFn::extract_cli_tag("--ignore-hooks", false) ?? $config['ignore_hooks'] ?? false;
$ignore_hooks = !$ignore_hooks ? empty($modules) : $ignore_hooks;
$default_foundation = $config['default_foundation'] ?? 'FoundationUtil';
if($list_modules) {
Console::log("Elevator Module List", Foreground::green);
LayDir::read(
__DIR__ . "/bricks",
function ($file, $dir, $handler, $entry) {
if($file == "Utils") return LayLoop::CONTINUE;
Console::log(" $file", Foreground::light_cyan);
$migrations = $dir . DIRECTORY_SEPARATOR . $file . DIRECTORY_SEPARATOR . "migration-files.json";
if(file_exists($migrations)) {
Console::log(" > migrations:" .
implode(
LayArray::map(
json_decode(file_get_contents($migrations), true),
function($v) {
$x = explode("_", $v, 2);
return "\n + " . $x[1];
}
)
)
);
}
return LayLoop::FLOW;
}
);
Console::bell();
return;
}
function in_modules(string $file) : ?array {
foreach (ELV_CONFIG['modules'] as $module) {
if($file == $module['module']) return $module;
}
return null;
}
function class_from_migration($migration) : string
{
$x = explode("_", $migration);
array_shift($x);
return str_replace(".php", "", implode(LayArray::map( $x, fn($a) => trim(ucfirst($a)) )) );
}
function hook_temp(string $brick) : string {
return <<<TMP
<?php
declare(strict_types=1);
namespace Bricks\\Elevator\\{$brick}\\Api;
class Hook extends \\Elevator\\{$brick}\\DefaultHooks
{
// You can overwrite whatever applies to the context of this project
// You can extend the existing hooks by calling parent::hooks().
// Ensure you go-to-definition for `DefaultHooks` to see what you can and can\'t do
}
TMP;
}
function update_migration_alias(string $destination, string $migration, string $alias): void
{
$class = class_from_migration($migration);
$class_alias = class_from_migration($alias);
$updated_lines = [];
$handle = fopen($destination, 'r');
if (!$handle)
throw new \RuntimeException("Unable to open file: $destination");
while (!feof($handle)) {
$line = fgets($handle);
if ($line === false) break;
$updated_line = str_replace(" $class ", " $class_alias ", $line);
$updated_lines[] = $updated_line;
}
fclose($handle);
file_put_contents($destination, implode('', $updated_lines));
}
Console::log("[+] Installing your selected modules", Foreground::light_cyan);
Console::log(" - Overwrite Hooks: " . ($overwrite_hooks ? 'true' : 'false'));
Console::log(" - Overwrite Migrations: " . ($overwrite_migrations ? 'true' : 'false'));
Console::log(" - Ignore Hooks: " . ($ignore_hooks ? 'true' : 'false'));
Console::log(" - Default Foundation: $default_foundation");
Console::log(" - Overwrite Foundation: $overwrite_foundation");
print "\n";
LayDir::read(
__DIR__ . "/bricks",
function ($file, $dir, $handler, $entry) use ($brick_root, $ignore_hooks, $overwrite_hooks, $overwrite_migrations, $db_dir) {
$module = in_modules($file);
$module_root = $entry['full_path'] . DIRECTORY_SEPARATOR;
if(str_starts_with($file, ".") || $file == "Utils" || !$module)
return LayLoop::CONTINUE;
$overwrite_migrations = $module['config']['overwrite_migrations'] ?? $overwrite_migrations;
$overwrite_hooks = $module['config']['overwrite_hooks'] ?? $overwrite_hooks;
Console::log("[✔︎] Found: $file", Foreground::light_green);
if(!$ignore_hooks && file_exists($module_root . "DefaultHooks.php")) {
$dest_dir = $brick_root . $file . DIRECTORY_SEPARATOR . "Api" . DIRECTORY_SEPARATOR;
$dest = $dest_dir . "Hook.php";
$exists = false;
if (!$overwrite_hooks && file_exists($dest)) {
Console::log(" Exists already: $dest", Foreground::yellow);
Console::log(" Use [ --force ] tag to overwrite or update your elevator config file", Foreground::purple);
$exists = true;
}
if(!$exists) {
LayDir::make($dest_dir, 0777, true);
if (!file_put_contents($dest, hook_temp($file))) {
Console::log(" Could not create Hook for $file! Stopping process!", Foreground::light_red);
Console::bell();
die;
}
Console::log(" - Hook Generated at: $dest");
}
}
Console::log(" [...] Getting migrations", Foreground::light_cyan);
if(!file_exists($module_root . "migration-files.json")) {
Console::log(" [x] No migrations for module");
return LayLoop::CONTINUE;
}
$migration_dir = $db_dir . "migrations" . DIRECTORY_SEPARATOR . "elevator" . DIRECTORY_SEPARATOR;
LayDir::make($migration_dir, 0777, true);
$migration_alias = $module['migration_alias'];
foreach (json_decode(file_get_contents($module_root . "migration-files.json"), true) as $migration) {
if(!empty($migration_alias)) {
$found = false;
foreach ($migration_alias as $key => $alias) {
$og = $alias[0] . ".php";
if(!str_ends_with($migration, $og))
continue;
$found = true;
$og = $migration;
$migration = explode("_", $og, 2)[0] . "_" . $alias[1] . ".php";
$destination = $migration_dir . $migration;
unset($migration_alias[$key]);
if(!$overwrite_migrations && file_exists($destination)) {
Console::log(" Migration exists already: $destination", Foreground::yellow);
Console::log(" Use [ --force-migrations ] tag to overwrite migrations or update your config file", Foreground::purple);
continue;
}
copy(__DIR__ . "/db/migrations/$og", $destination);
Console::log(" - Updating migration From [$og] to [$migration]\n $destination");
update_migration_alias($destination, $og, $migration);
}
if($found) continue;
}
$destination = $migration_dir . $migration;
if(!$overwrite_migrations && file_exists($destination)) {
Console::log(" Migration exists already: $destination", Foreground::yellow);
Console::log(" Use [ --force-migrations ] tag to overwrite migrations or update your config file", Foreground::purple);
continue;
}
copy(__DIR__ . "/db/migrations/$migration", $destination);
}
Console::log(" [o] Migrations copy complete!");
Console::log(" [...] Getting Seeders", Foreground::light_cyan);
if(!file_exists($module_root . "seeder-files.json")) {
Console::log(" [x] No Seeder for module");
return LayLoop::CONTINUE;
}
$seeder_dir = $db_dir . "seeds" . DIRECTORY_SEPARATOR . "elevator" . DIRECTORY_SEPARATOR;
LayDir::make($seeder_dir . "data", 0777, true);
$seeder = json_decode(file_get_contents($module_root . "seeder-files.json"), true);
foreach ($seeder['seeds'] as $seed) {
$destination = $seeder_dir . $seed;
if(!$overwrite_migrations && file_exists($destination)) {
Console::log(" Seeder exists already: $destination", Foreground::yellow);
Console::log(" Use [ --force-migrations ] tag to overwrite seeders or update your config file", Foreground::purple);
continue;
}
copy(__DIR__ . "/db/seeds/$seed", $destination);
}
Console::log(" [o] Seeders copy complete!");
Console::log(" [...] Getting Seeder Data", Foreground::light_cyan);
$seeder_dir = $db_dir . "seeds" . DIRECTORY_SEPARATOR . "elevator" . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR;
foreach ($seeder['data'] as $seed) {
$destination = $seeder_dir . $seed;
if(!$overwrite_migrations && file_exists($destination)) {
Console::log(" Seeder Data exists already: $destination", Foreground::yellow);
Console::log(" Use [ --force-migrations ] tag to overwrite seeders data or update your config file", Foreground::purple);
continue;
}
copy(__DIR__ . "/db/seeds/data/$seed", $destination);
}
Console::log(" [o] Seeder Data copy complete!");
return LayLoop::FLOW;
}
);
if(!$ignore_hooks)
file_put_contents(
$brick_root . "child-has-hooks.php",
"<?php // AUTO GEN BY Elevator. If you want to use Elevator's Hooks as is, then don't delete, else delete this file to avoid clashes!"
);
if (!file_exists($brick_root . $default_foundation . ".php") || $overwrite_foundation)
file_put_contents($brick_root . $default_foundation . ".php", <<<HK
<?php
declare(strict_types=1);
namespace Bricks\\Elevator;
class $default_foundation extends \\Elevator\\Utils\\ElevatorFoundation
{
// You can overwrite whatever controller you wish.
// Ensure you go-to-definition for `ElevatorFoundation` to see what you can and can\'t do
}
HK
);
Console::log("Operation complete!", Foreground::blue);
Console::bell();