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 'init': |
26
|
|
|
return $this->commandInit(); |
27
|
|
|
case 'list': |
28
|
|
|
return $this->commandList(); |
29
|
|
|
case 'create': |
30
|
|
|
return $this->commandCreate(); |
31
|
|
|
default: |
32
|
|
|
return $this->commandHelp(); |
33
|
|
|
} |
34
|
|
|
return 1; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function commandMigrate() |
38
|
|
|
{ |
39
|
|
|
Suricate::Migration()->doMigrations(); |
40
|
|
|
return 0; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function commandInit(): int |
44
|
|
|
{ |
45
|
|
|
$result = Suricate::Migration()->initMigrationTable(); |
46
|
|
|
if ($result) { |
47
|
|
|
echo 'Migration table created successfully' . "\n"; |
48
|
|
|
return 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return 1; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function commandList(): int |
55
|
|
|
{ |
56
|
|
|
$migrations = Suricate::Migration()->listMigrations(); |
57
|
|
|
if (count($migrations) === 0) { |
58
|
|
|
echo "No migration\n"; |
59
|
|
|
return 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
echo str_repeat("-", 76) . "\n"; |
63
|
|
|
foreach ($migrations as $migrationKey=>$migrationDate) { |
64
|
|
|
echo "| " . str_pad(trim($migrationKey), 50, ' ', STR_PAD_RIGHT) . " | " . ($migrationDate !== false ? $migrationDate : str_pad('-', 19, ' ', STR_PAD_BOTH)). " |\n"; |
65
|
|
|
} |
66
|
|
|
echo str_repeat("-", 76) . "\n"; |
67
|
|
|
|
68
|
|
|
return 0; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function commandCreate(): int |
72
|
|
|
{ |
73
|
|
|
$migrationName = Suricate::Migration()->createMigration(); |
74
|
|
|
if ($migrationName === false) { |
|
|
|
|
75
|
|
|
echo "Failed to create migration file\n"; |
76
|
|
|
return 1; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
echo "Migration $migrationName created successfully\n"; |
80
|
|
|
|
81
|
|
|
return 0; |
82
|
|
|
} |
83
|
|
|
private function commandHelp(): int |
84
|
|
|
{ |
85
|
|
|
$str = "Help:" . "\n"; |
86
|
|
|
$str .= Console::coloredString('init:', 'green') . "\n"; |
87
|
|
|
$str .= "\tInitialize the migrations table\n"; |
88
|
|
|
$str .= Console::coloredString('list:', 'green') . "\n"; |
89
|
|
|
$str .= "\tList migrations\n"; |
90
|
|
|
$str .= Console::coloredString('migrate:', 'green') . "\n"; |
91
|
|
|
$str .= "\tExecute pending migrations\n"; |
92
|
|
|
|
93
|
|
|
echo $str; |
94
|
|
|
return 0; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.