1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Actions\Migration; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
use Mtolhuys\LaravelSchematics\Services\RuleParser; |
8
|
|
|
use Mtolhuys\LaravelSchematics\Actions\Migration\Traits\CreatesMigrations; |
9
|
|
|
use Mtolhuys\LaravelSchematics\Services\StubWriter; |
10
|
|
|
|
11
|
|
|
class CreateColumnsMigrationAction |
12
|
|
|
{ |
13
|
|
|
use CreatesMigrations; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param $request |
17
|
|
|
*/ |
18
|
|
|
public function execute($request) |
19
|
|
|
{ |
20
|
|
|
$table = app($request['model'])->getTable(); |
21
|
|
|
$stub = __DIR__ . '/../../../resources/stubs/migration/columns.stub'; |
22
|
|
|
$title = $this->title($request, $table); |
23
|
|
|
$this->filename = 'database/migrations/' . date('Y_m_d_His') . '_' . Str::snake($title) . '.php'; |
24
|
|
|
$request = $this->separateNameAndTypeChanges($request); |
25
|
|
|
|
26
|
|
|
(new StubWriter(base_path($this->filename), $stub))->write([ |
27
|
|
|
'$columnsUp$' => rtrim($this->getUpMethods($request)), |
28
|
|
|
'$columnsDown$' => rtrim($this->getDownMethods($request)), |
29
|
|
|
'$classname$' => $title, |
30
|
|
|
'$table$' => $table, |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Combine and return all up methods |
36
|
|
|
* |
37
|
|
|
* @param $request |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
private function getUpMethods($request): string |
41
|
|
|
{ |
42
|
|
|
$changes = array_filter($this->changes($request['fields'], true)); |
43
|
|
|
|
44
|
|
|
return (empty($changes) ? '' : RuleParser::fieldsToMigrationMethods($this->getFields($changes))) |
45
|
|
|
. (empty($request['created']) ? '' : RuleParser::fieldsToMigrationMethods( |
46
|
|
|
$this->getFields($request['created']) |
47
|
|
|
)) |
48
|
|
|
. (empty($request['deleted']) ? '' : RuleParser::fieldsToMigrationMethods( |
49
|
|
|
$this->getFields($this->deleted($request['deleted'])) |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Combine and return all down methods |
55
|
|
|
* |
56
|
|
|
* @param $request |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
private function getDownMethods($request): string |
60
|
|
|
{ |
61
|
|
|
$changes = array_filter( |
62
|
|
|
$this->changes(array_reverse($request['fields']), false) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return |
66
|
|
|
(empty($changes) ? '' : RuleParser::fieldsToMigrationMethods($this->getFields($changes))) |
67
|
|
|
. (empty($request['deleted']) ? '' : RuleParser::fieldsToMigrationMethods( |
68
|
|
|
$this->getFields(array_map(function ($field) { |
69
|
|
|
$field['type'] = $this->parseColumnType($field['columnType']); |
70
|
|
|
|
71
|
|
|
return $field; |
72
|
|
|
}, $request['deleted'])) |
73
|
|
|
)) |
74
|
|
|
. (empty($request['created']) ? '' : RuleParser::fieldsToMigrationMethods( |
75
|
|
|
$this->getFields( |
76
|
|
|
$this->deleted($request['created'])) |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add change specific type fields |
82
|
|
|
* |
83
|
|
|
* @param $fields |
84
|
|
|
* @param bool $up |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
private function changes($fields, bool $up): array |
88
|
|
|
{ |
89
|
|
|
return array_map(function ($field) use ($up) { |
90
|
|
|
$changedField = json_decode($field['exists'], false) |
91
|
|
|
&& json_decode($field['changed'], false); |
92
|
|
|
|
93
|
|
|
if ($changedField) { |
94
|
|
|
$field = $up ? $this->setUpChange($field) : $this->setDownChange($field); |
95
|
|
|
|
96
|
|
|
return $field; |
97
|
|
|
} |
98
|
|
|
}, $fields); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add dropColumn for deleted fields |
103
|
|
|
* |
104
|
|
|
* @param $deleted |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
private function deleted($deleted): array |
108
|
|
|
{ |
109
|
|
|
return array_map(static function ($field) { |
110
|
|
|
$field['type'] = 'dropColumn|required'; |
111
|
|
|
|
112
|
|
|
return $field; |
113
|
|
|
}, $deleted); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get array of field names |
118
|
|
|
* |
119
|
|
|
* @param $request |
120
|
|
|
* @param string $table |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
private function title($request, string $table): string |
124
|
|
|
{ |
125
|
|
|
$actions = static function ($action) use ($request) { |
126
|
|
|
if (isset($request[$action])) { |
127
|
|
|
return ucfirst($action) |
128
|
|
|
. ucfirst(Str::camel( |
129
|
|
|
implode('_and_', array_map(static function ($field) { |
130
|
|
|
return empty($field['from']) ? $field['name'] : $field['from']; |
131
|
|
|
}, array_merge( |
132
|
|
|
$request[$action] |
133
|
|
|
))) |
134
|
|
|
)); |
135
|
|
|
} |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
return implode('', array_filter([ |
139
|
|
|
$actions('created'), |
140
|
|
|
$actions('changed'), |
141
|
|
|
$actions('deleted') |
142
|
|
|
])) . 'ColumnIn' . ucfirst(Str::camel($table)) . 'Table'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* This is for cases where both the name and type |
147
|
|
|
* of the column where changed |
148
|
|
|
* |
149
|
|
|
* @param $request |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
private function separateNameAndTypeChanges($request) |
153
|
|
|
{ |
154
|
|
|
if (empty($request['changed'])) { |
155
|
|
|
return $request; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$changes = array_filter(array_map(static function ($field) { |
159
|
|
|
if (!empty($field['from']) && !empty($field['type'])) { |
160
|
|
|
$field['name'] = $field['from']; |
161
|
|
|
$field['changed'] = true; |
162
|
|
|
$field['from'] = null; |
163
|
|
|
|
164
|
|
|
return $field; |
165
|
|
|
} |
166
|
|
|
}, $request['changed'])); |
167
|
|
|
|
168
|
|
|
if (!empty($changes)) { |
169
|
|
|
$request['fields'] = array_merge([$changes[0]], $request['fields']); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $request; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|