|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GarbuzIvan\LaravelGeneratorPackage\Builder\Components; |
|
6
|
|
|
|
|
7
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Builder\Package; |
|
8
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Configuration; |
|
9
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Contracts\FieldInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Migration |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Configuration |
|
15
|
|
|
*/ |
|
16
|
|
|
private Configuration $config; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Package |
|
20
|
|
|
*/ |
|
21
|
|
|
private Package $package; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* FileGenerator constructor. |
|
25
|
|
|
* @param Configuration $config |
|
26
|
2 |
|
*/ |
|
27
|
|
|
public function __construct(Configuration $config) |
|
28
|
2 |
|
{ |
|
29
|
2 |
|
$this->config = $config; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Package $package |
|
34
|
|
|
* @return bool |
|
35
|
2 |
|
*/ |
|
36
|
|
|
public function make(Package $package): bool |
|
37
|
2 |
|
{ |
|
38
|
2 |
|
$fieldsCode = ''; |
|
39
|
2 |
|
$this->package = $package; |
|
40
|
2 |
|
$fields = $this->package->getFields(); |
|
41
|
|
|
foreach ($fields as $field) { |
|
42
|
|
|
$fieldsCode .= $this->generationField($field); |
|
43
|
2 |
|
} |
|
44
|
2 |
|
$code = str_replace([ |
|
45
|
|
|
'%FIELDS%', |
|
46
|
|
|
'%TABLE%', |
|
47
|
|
|
'%MODEL%', |
|
48
|
2 |
|
], [ |
|
49
|
2 |
|
$fieldsCode, |
|
50
|
2 |
|
$this->package->getTable(), |
|
51
|
2 |
|
$this->package->getModel(), |
|
52
|
2 |
|
], $this->migrate); |
|
53
|
2 |
|
$nameFileMigration = date('Y_m_d_His') . '_create_' . $this->package->getTable() . '_table.php'; |
|
54
|
2 |
|
file_put_contents($this->package->getPath('migrations/' . $nameFileMigration), $code); |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param FieldInterface $field |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function generationField(FieldInterface $field): string |
|
63
|
|
|
{ |
|
64
|
|
|
$code = '$table->' . $field->getType() . '(\'' . $field->getColumn() . '\')'; |
|
|
|
|
|
|
65
|
|
|
$code .= $field->getNullable() ? '->nullable()' : ''; |
|
|
|
|
|
|
66
|
|
|
$code .= $field->getUnique() ? '->unique()' : ''; |
|
|
|
|
|
|
67
|
|
|
if (!is_null($field->getReferencesTable())) { |
|
|
|
|
|
|
68
|
|
|
$code .= '->references(\'' . $field->getReferencesField() . '\')->on(\'' . $field->getReferencesTable() . '\')'; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
return "\n\t\t\t" . $code . ";"; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* PHP code migration |
|
75
|
|
|
* |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
public string $migrate = <<<'EOD' |
|
79
|
|
|
<?php |
|
80
|
|
|
|
|
81
|
|
|
declare(strict_types=1); |
|
82
|
|
|
|
|
83
|
|
|
use Illuminate\Database\Migrations\Migration; |
|
84
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
85
|
|
|
use Illuminate\Support\Facades\Schema; |
|
86
|
|
|
|
|
87
|
|
|
class Create%MODEL%Table extends Migration |
|
88
|
|
|
{ |
|
89
|
|
|
/** |
|
90
|
|
|
* Run the migrations. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function up() |
|
95
|
|
|
{ |
|
96
|
|
|
Schema::create('%TABLE%', function (Blueprint $table) { |
|
97
|
|
|
$table->id(); %FIELDS% |
|
98
|
|
|
$table->timestamps(); |
|
99
|
|
|
}); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Reverse the migrations. |
|
104
|
|
|
* |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
public function down() |
|
108
|
|
|
{ |
|
109
|
|
|
Schema::dropIfExists('%TABLE%'); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
EOD; |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|