|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file MigrationCreateCommand.php |
|
34
|
|
|
* |
|
35
|
|
|
* The migration generation command class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\Migration\Command |
|
38
|
|
|
* @author Platine Developers team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link http://www.iacademy.cf |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Framework\Migration\Command; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Config\Config; |
|
51
|
|
|
use Platine\Console\Input\Reader; |
|
52
|
|
|
use Platine\Console\Output\Writer; |
|
53
|
|
|
use Platine\Database\Schema; |
|
54
|
|
|
use Platine\Filesystem\Filesystem; |
|
55
|
|
|
use Platine\Framework\App\Application; |
|
56
|
|
|
use Platine\Framework\Migration\MigrationRepository; |
|
57
|
|
|
use Platine\Stdlib\Helper\Str; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* class MigrationCreateCommand |
|
61
|
|
|
* @package Platine\Framework\Migration\Command |
|
62
|
|
|
*/ |
|
63
|
|
|
class MigrationCreateCommand extends AbstractCommand |
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The migration name |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected string $name = ''; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create new instance |
|
74
|
|
|
* {@inheritodc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct( |
|
77
|
|
|
Application $app, |
|
78
|
|
|
MigrationRepository $repository, |
|
79
|
|
|
Schema $schema, |
|
80
|
|
|
Config $config, |
|
81
|
|
|
Filesystem $filesystem |
|
82
|
|
|
) { |
|
83
|
|
|
parent::__construct($app, $repository, $schema, $config, $filesystem); |
|
84
|
|
|
$this->setName('migration:create') |
|
85
|
|
|
->setDescription('Create a new migration'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritodc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function execute() |
|
92
|
|
|
{ |
|
93
|
|
|
$writer = $this->io()->writer(); |
|
94
|
|
|
|
|
95
|
|
|
$version = date('YmdHis'); |
|
96
|
|
|
$className = $this->getMigrationClassName($this->name, $version); |
|
97
|
|
|
$filename = sprintf( |
|
98
|
|
|
'%s_%s.php', |
|
99
|
|
|
$version, |
|
100
|
|
|
str_replace($version, '', Str::snake($className)) |
|
101
|
|
|
); |
|
102
|
|
|
$fullPath = $this->migrationPath . $filename; |
|
103
|
|
|
|
|
104
|
|
|
$writer->boldGreen('Migration detail: ')->eol(); |
|
105
|
|
|
$writer->bold('Name: '); |
|
106
|
|
|
$writer->boldBlueBgBlack($this->name, true); |
|
107
|
|
|
$writer->bold('Version: '); |
|
108
|
|
|
$writer->boldBlueBgBlack($version, true); |
|
109
|
|
|
$writer->bold('Class name: '); |
|
110
|
|
|
$writer->boldBlueBgBlack($className, true); |
|
111
|
|
|
$writer->bold('Filename: '); |
|
112
|
|
|
$writer->boldBlueBgBlack($filename, true); |
|
113
|
|
|
$writer->bold('Path: '); |
|
114
|
|
|
$writer->boldBlueBgBlack($fullPath, true)->eol(); |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$io = $this->io(); |
|
118
|
|
|
|
|
119
|
|
|
if ($io->confirm('Are you confirm the generation of new migration?', 'n')) { |
|
120
|
|
|
$this->generateClass($fullPath, $className); |
|
121
|
|
|
$writer->boldGreen(sprintf( |
|
122
|
|
|
'Migration [%s] generated successfully', |
|
123
|
|
|
$this->name |
|
124
|
|
|
))->eol(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritodc} |
|
130
|
|
|
*/ |
|
131
|
|
|
public function interact(Reader $reader, Writer $writer): void |
|
132
|
|
|
{ |
|
133
|
|
|
$writer->boldYellow('MIGRATION GENERATION', true)->eol(); |
|
134
|
|
|
|
|
135
|
|
|
$io = $this->io(); |
|
136
|
|
|
$name = $io->prompt('Enter the name of the migration', 'Migration description'); |
|
137
|
|
|
$this->name = $name; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Generate the migration class |
|
142
|
|
|
* @param string $path |
|
143
|
|
|
* @param string $className |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function generateClass(string $path, string $className): void |
|
147
|
|
|
{ |
|
148
|
|
|
$template = $this->getTemplateClass(); |
|
149
|
|
|
$content = Str::replaceFirst('%classname%', $className, $template); |
|
150
|
|
|
|
|
151
|
|
|
$file = $this->filesystem->file($path); |
|
152
|
|
|
$file->write($content); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Return the migration template class |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
private function getTemplateClass(): string |
|
160
|
|
|
{ |
|
161
|
|
|
return <<<EOF |
|
162
|
|
|
<?php |
|
163
|
|
|
namespace Platine\Framework\Migration; |
|
164
|
|
|
|
|
165
|
|
|
use Platine\Framework\Migration\AbstractMigration; |
|
166
|
|
|
|
|
167
|
|
|
class %classname% extends AbstractMigration |
|
168
|
|
|
{ |
|
169
|
|
|
|
|
170
|
|
|
public function up(): void |
|
171
|
|
|
{ |
|
172
|
|
|
//Action when migrate up |
|
173
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function down(): void |
|
177
|
|
|
{ |
|
178
|
|
|
//Action when migrate down |
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
EOF; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|