|
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 SeedCreateDbCommand.php |
|
34
|
|
|
* |
|
35
|
|
|
* The seed generation using an existing database data command class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\Migration\Seed\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\Seed\Command; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Config\Config; |
|
51
|
|
|
use Platine\Console\Input\Reader; |
|
52
|
|
|
use Platine\Console\Output\Writer; |
|
53
|
|
|
use Platine\Database\QueryBuilder; |
|
54
|
|
|
use Platine\Database\Schema; |
|
55
|
|
|
use Platine\Filesystem\Filesystem; |
|
56
|
|
|
use Platine\Framework\App\Application; |
|
57
|
|
|
use Platine\Framework\Migration\Seed\Command\AbstractSeedCommand; |
|
58
|
|
|
use Platine\Stdlib\Helper\Str; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @class SeedCreateDbCommand |
|
62
|
|
|
* @package Platine\Framework\Migration\Seed\Command |
|
63
|
|
|
* @template T |
|
64
|
|
|
* @extends AbstractSeedCommand<T> |
|
65
|
|
|
*/ |
|
66
|
|
|
class SeedCreateDbCommand extends AbstractSeedCommand |
|
67
|
|
|
{ |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The seed name |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected string $name = ''; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* The table name |
|
77
|
|
|
* @var string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected string $table = ''; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* The schema to use |
|
83
|
|
|
* @var Schema |
|
84
|
|
|
*/ |
|
85
|
|
|
protected Schema $schema; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* The instance of query builder |
|
89
|
|
|
* @var QueryBuilder |
|
90
|
|
|
*/ |
|
91
|
|
|
protected QueryBuilder $queryBuilder; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Create new instance |
|
95
|
|
|
* {@inheritodc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __construct( |
|
98
|
|
|
Application $app, |
|
99
|
|
|
Config $config, |
|
100
|
|
|
Filesystem $filesystem, |
|
101
|
|
|
Schema $schema, |
|
102
|
|
|
QueryBuilder $queryBuilder |
|
103
|
|
|
) { |
|
104
|
|
|
parent::__construct($app, $config, $filesystem); |
|
105
|
|
|
$this->schema = $schema; |
|
106
|
|
|
$this->queryBuilder = $queryBuilder; |
|
107
|
|
|
|
|
108
|
|
|
$this->setName('seed:createdb') |
|
109
|
|
|
->setDescription('Create a new seed using existing data'); |
|
110
|
|
|
|
|
111
|
|
|
$this->addArgument('table', 'name of the table', null, true, false); |
|
112
|
|
|
$this->addArgument('name', 'name of seed', null, false, true); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritodc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function execute() |
|
119
|
|
|
{ |
|
120
|
|
|
$writer = $this->io()->writer(); |
|
121
|
|
|
|
|
122
|
|
|
$this->table = $this->getArgumentValue('table'); |
|
123
|
|
|
$className = $this->getSeedClassName($this->name); |
|
124
|
|
|
$filename = $this->getFilenameFromClass($className); |
|
125
|
|
|
$fullPath = $this->seedPath . $filename; |
|
126
|
|
|
|
|
127
|
|
|
$writer->boldGreen('Seed detail: ')->eol(); |
|
128
|
|
|
$writer->bold('Name: '); |
|
129
|
|
|
$writer->boldBlueBgBlack($this->name, true); |
|
130
|
|
|
$writer->bold('Table : '); |
|
131
|
|
|
$writer->boldBlueBgBlack($this->table, true); |
|
132
|
|
|
$writer->bold('Class name: '); |
|
133
|
|
|
$writer->boldBlueBgBlack($className, true); |
|
134
|
|
|
$writer->bold('Filename: '); |
|
135
|
|
|
$writer->boldBlueBgBlack($filename, true); |
|
136
|
|
|
$writer->bold('Path: '); |
|
137
|
|
|
$writer->boldBlueBgBlack($fullPath, true)->eol(); |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
$io = $this->io(); |
|
141
|
|
|
|
|
142
|
|
|
if ($io->confirm('Are you confirm the generation of new seed?', 'n')) { |
|
143
|
|
|
if (!$this->schema->hasTable($this->table, true)) { |
|
144
|
|
|
$writer->boldRed(sprintf( |
|
145
|
|
|
'Database table [%s] does not exist', |
|
146
|
|
|
$this->table |
|
147
|
|
|
)); |
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
$this->checkSeedPath(); |
|
151
|
|
|
$this->generateClass($fullPath, $className); |
|
152
|
|
|
$writer->boldGreen(sprintf( |
|
153
|
|
|
'Seed [%s] generated successfully', |
|
154
|
|
|
$this->name |
|
155
|
|
|
))->eol(); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritodc} |
|
161
|
|
|
*/ |
|
162
|
|
|
public function interact(Reader $reader, Writer $writer): void |
|
163
|
|
|
{ |
|
164
|
|
|
$writer->boldYellow('SEED GENERATION USING EXISTING DATA', true)->eol(); |
|
165
|
|
|
|
|
166
|
|
|
$name = $this->getArgumentValue('name'); |
|
167
|
|
|
if (!$name) { |
|
168
|
|
|
$io = $this->io(); |
|
169
|
|
|
$name = $io->prompt('Enter the name of the seed', 'Seed description'); |
|
170
|
|
|
} |
|
171
|
|
|
$this->name = $name; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Generate the migration class |
|
176
|
|
|
* @param string $path |
|
177
|
|
|
* @param string $className |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function generateClass(string $path, string $className): void |
|
181
|
|
|
{ |
|
182
|
|
|
$template = $this->getTemplateClass(); |
|
183
|
|
|
$seedDefinition = $this->generateSeedFromTableData(); |
|
184
|
|
|
$content = Str::replaceFirst('%classname%', $className, $template); |
|
185
|
|
|
$seedContent = Str::replaceFirst('%seed_content%', $seedDefinition, $content); |
|
186
|
|
|
|
|
187
|
|
|
$file = $this->filesystem->file($path); |
|
188
|
|
|
$file->write($seedContent); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Return the seed template class |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function getTemplateClass(): string |
|
196
|
|
|
{ |
|
197
|
|
|
return <<<EOF |
|
198
|
|
|
<?php |
|
199
|
|
|
declare(strict_types=1); |
|
200
|
|
|
|
|
201
|
|
|
namespace Platine\Framework\Migration\Seed; |
|
202
|
|
|
|
|
203
|
|
|
use Platine\Framework\Migration\Seed\AbstractSeed; |
|
204
|
|
|
|
|
205
|
|
|
class %classname% extends AbstractSeed |
|
206
|
|
|
{ |
|
207
|
|
|
|
|
208
|
|
|
public function run(): void |
|
209
|
|
|
{ |
|
210
|
|
|
//Action when run the seed |
|
211
|
|
|
%seed_content% |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
EOF; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Generate the seed content using the current table data |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function generateSeedFromTableData(): string |
|
222
|
|
|
{ |
|
223
|
|
|
$content = ''; |
|
224
|
|
|
$data = $this->queryBuilder->from($this->table) |
|
225
|
|
|
->select() |
|
226
|
|
|
->fetchAssoc() |
|
227
|
|
|
->all(); |
|
228
|
|
|
|
|
229
|
|
|
if (empty($data)) { |
|
230
|
|
|
return sprintf('// No data found on table "%s"', $this->table); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
$content .= ' |
|
234
|
|
|
$data = ' . $this->exportSeedData($data) . '; |
|
235
|
|
|
foreach ($data as $row) { |
|
236
|
|
|
$this->insert($row)->into(\'' . $this->table . '\'); |
|
237
|
|
|
} |
|
238
|
|
|
'; |
|
239
|
|
|
|
|
240
|
|
|
return $content; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Export the data to string representation |
|
245
|
|
|
* @param array<array<string, mixed>> $data |
|
246
|
|
|
* @return string |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function exportSeedData(array $data):string |
|
249
|
|
|
{ |
|
250
|
|
|
$export = var_export($data, true); |
|
251
|
|
|
$export = preg_replace('/^([ ]*)(.*)/m', '$1$1$2', $export); |
|
252
|
|
|
$array = preg_split("/\r\n|\n|\r/", $export); |
|
253
|
|
|
$array = preg_replace( |
|
254
|
|
|
["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], |
|
255
|
|
|
[null, ']$1', ' => ['], |
|
256
|
|
|
$array |
|
257
|
|
|
); |
|
258
|
|
|
$export = join(PHP_EOL, array_filter(['['] + $array)); |
|
259
|
|
|
|
|
260
|
|
|
return $export; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|