|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Ship\Command; |
|
4
|
|
|
|
|
5
|
|
|
use App\Ship\Utils\FileCreator; |
|
6
|
|
|
use Rudra\Container\Facades\Rudra; |
|
7
|
|
|
use Rudra\Cli\ConsoleFacade as Cli; |
|
8
|
|
|
|
|
9
|
|
|
class CreateMigrationCommand extends FileCreator |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Creates a file with Migration data |
|
13
|
|
|
* ----------------------------- |
|
14
|
|
|
* Создает файл с данными Migration |
|
15
|
|
|
*/ |
|
16
|
|
|
public function actionIndex(): void |
|
17
|
|
|
{ |
|
18
|
|
|
Cli::printer("Enter table name: ", "magneta"); |
|
19
|
|
|
$table = str_replace(PHP_EOL, "", Cli::reader()); |
|
20
|
|
|
|
|
21
|
|
|
Cli::printer("Enter container (empty for Ship): ", "magneta"); |
|
22
|
|
|
$container = ucfirst(str_replace(PHP_EOL, "", Cli::reader())); |
|
23
|
|
|
|
|
24
|
|
|
$date = date("_dmYHis"); |
|
25
|
|
|
$className = ucfirst($table) . $date; |
|
26
|
|
|
|
|
27
|
|
|
if (!empty($container)) { |
|
28
|
|
|
$namespace = 'App\Containers\\' . $container . '\Migration'; |
|
29
|
|
|
|
|
30
|
|
|
$this->writeFile([str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/" . $container . "/Migration/"), "{$className}_migration.php"], |
|
31
|
|
|
$this->createMigration($className, $table, $namespace) |
|
32
|
|
|
); |
|
33
|
|
|
} else { |
|
34
|
|
|
$namespace = "App\Ship\Migration"; |
|
35
|
|
|
|
|
36
|
|
|
$this->writeFile([str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Ship/Migration/"), "{$className}_migration.php"], |
|
37
|
|
|
$this->createMigration($className, $table, $namespace) |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Creates class data |
|
44
|
|
|
* ------------------ |
|
45
|
|
|
* Создает данные класса |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $className |
|
48
|
|
|
* @param string $table |
|
49
|
|
|
* @param string $namespace |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
private function createMigration(string $className, string $table, string $namespace): string |
|
53
|
|
|
{ |
|
54
|
|
|
return <<<EOT |
|
55
|
|
|
<?php |
|
56
|
|
|
|
|
57
|
|
|
namespace $namespace; |
|
58
|
|
|
|
|
59
|
|
|
use Rudra\Model\QBFacade; |
|
60
|
|
|
use Rudra\Container\Facades\Rudra; |
|
61
|
|
|
|
|
62
|
|
|
class {$className}_migration |
|
63
|
|
|
{ |
|
64
|
|
|
public function up(): void |
|
65
|
|
|
{ |
|
66
|
|
|
\$table = "$table"; |
|
67
|
|
|
|
|
68
|
|
|
Rudra::get("DSN")->prepare(QBFacade::create(\$table) |
|
69
|
|
|
->integer('id', '', true) |
|
70
|
|
|
->created_at() |
|
71
|
|
|
->updated_at() |
|
72
|
|
|
->pk('id') |
|
73
|
|
|
->close() |
|
74
|
|
|
->get() |
|
75
|
|
|
)->execute(); |
|
76
|
|
|
} |
|
77
|
|
|
}\r\n |
|
78
|
|
|
EOT; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|