1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate\Console; |
6
|
|
|
|
7
|
|
|
use Suricate\Console; |
8
|
|
|
use Suricate\Suricate; |
9
|
|
|
|
10
|
|
|
class Migration |
11
|
|
|
{ |
12
|
|
|
protected $app; |
13
|
|
|
|
14
|
|
|
public function __construct(Suricate $app) |
15
|
|
|
{ |
16
|
|
|
$this->app = $app; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function execute(array $arguments): int |
20
|
|
|
{ |
21
|
|
|
$command = $arguments[0] ?? ''; |
22
|
|
|
switch ($command) { |
23
|
|
|
case 'migrate': |
24
|
|
|
return $this->commandMigrate(); |
25
|
|
|
case 'list': |
26
|
|
|
return $this->commandList(); |
27
|
|
|
case 'create': |
28
|
|
|
return $this->commandCreate(); |
29
|
|
|
default: |
30
|
|
|
return $this->commandHelp(); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function commandMigrate() |
35
|
|
|
{ |
36
|
|
|
Suricate::Migration()->doMigrations(); |
37
|
|
|
return 0; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function commandList(): int |
41
|
|
|
{ |
42
|
|
|
$migrations = Suricate::Migration()->listMigrations(); |
43
|
|
|
if (count($migrations) === 0) { |
44
|
|
|
echo "No migration\n"; |
45
|
|
|
return 0; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($migrations as $configName => $currentConfigMigrations) { |
49
|
|
|
echo str_repeat("-", 13+strlen($configName)) . "\n"; |
50
|
|
|
echo "| Config : $configName |\n"; |
51
|
|
|
echo str_repeat("-", 76) . "\n"; |
52
|
|
|
if (count($currentConfigMigrations) === 0) { |
53
|
|
|
echo "| 🎉 no migration\n"; |
54
|
|
|
} else { |
55
|
|
|
foreach ($currentConfigMigrations as $migrationKey=>$migrationDate) { |
56
|
|
|
echo "| " . str_pad(trim($migrationKey), 50, ' ', STR_PAD_RIGHT) . " | " . ($migrationDate !== false ? $migrationDate : str_pad('-', 19, ' ', STR_PAD_BOTH)). " |\n"; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
echo str_repeat("-", 76) . "\n\n"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function commandCreate(): int |
66
|
|
|
{ |
67
|
|
|
$migrationName = Suricate::Migration()->createMigration(); |
68
|
|
|
if ($migrationName === false) { |
69
|
|
|
echo "Failed to create migration file\n"; |
70
|
|
|
return 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
echo "Migration $migrationName created successfully\n"; |
74
|
|
|
|
75
|
|
|
return 0; |
76
|
|
|
} |
77
|
|
|
private function commandHelp(): int |
78
|
|
|
{ |
79
|
|
|
$str = "Help:" . "\n"; |
80
|
|
|
$str .= Console::coloredString('init:', 'green') . "\n"; |
81
|
|
|
$str .= "\tInitialize the migrations table\n"; |
82
|
|
|
$str .= Console::coloredString('list:', 'green') . "\n"; |
83
|
|
|
$str .= "\tList migrations\n"; |
84
|
|
|
$str .= Console::coloredString('migrate:', 'green') . "\n"; |
85
|
|
|
$str .= "\tExecute pending migrations\n"; |
86
|
|
|
|
87
|
|
|
echo $str; |
88
|
|
|
return 0; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|