1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Actions\Migration\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Artisan; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
trait CreatesMigrations |
10
|
|
|
{ |
11
|
|
|
public |
12
|
|
|
$autoMigrate, |
13
|
|
|
$filename, |
14
|
|
|
$path; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->autoMigrate = config('schematics.auto-migrate'); |
19
|
|
|
$this->path = database_path('migrations'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function __destruct() |
23
|
|
|
{ |
24
|
|
|
if ($this->autoMigrate) { |
25
|
|
|
try { |
26
|
|
|
Artisan::call('migrate'); |
27
|
|
|
} catch (\Throwable $e) { |
28
|
|
|
Cache::put('schematics-exception', [ |
29
|
|
|
'title' => get_class($e), |
30
|
|
|
'message' => $e->getMessage(), |
31
|
|
|
], 1440); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $request |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function getLocalKey($request): string |
41
|
|
|
{ |
42
|
|
|
return empty($request['method']['localKey']) |
43
|
|
|
? 'id' : $request['method']['localKey']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $request |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
protected function getForeignKey($request): string |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
return empty($request['method']['foreignKey']) |
53
|
|
|
? strtolower( |
54
|
|
|
Str::snake( |
55
|
|
|
substr(strrchr($request['target'], "\\"), 1) . '_id' |
56
|
|
|
) |
57
|
|
|
) |
58
|
|
|
: $request['method']['foreignKey']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function getSecondLocalKey($request): string |
62
|
|
|
{ |
63
|
|
|
return empty($request['method']['secondLocalKey']) |
64
|
|
|
? 'id' : $request['method']['secondLocalKey']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
protected function getSecondForeignKey($request): string |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return empty($request['method']['secondForeignKey']) |
70
|
|
|
? strtolower( |
71
|
|
|
Str::snake( |
72
|
|
|
substr(strrchr($request['source'], "\\"), 1) . '_id' |
73
|
|
|
) |
74
|
|
|
) |
75
|
|
|
: $request['method']['secondForeignKey']; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $fields |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
protected function getFields($fields): array |
85
|
|
|
{ |
86
|
|
|
return array_map(static function ($field) { |
87
|
|
|
return [ |
88
|
|
|
$field['name'] => empty($field['type']) ? 'string|max:255' : $field['type'] |
89
|
|
|
]; |
90
|
|
|
}, $fields); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $field |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
protected function setUpChange($field) |
98
|
|
|
{ |
99
|
|
|
if (isset($field['from'])) { |
100
|
|
|
$field['type'] = "renameColumn|from:{$field['from']}|required"; |
101
|
|
|
} else { |
102
|
|
|
$field['type'] .= '|change'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $field; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $field |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
protected function setDownChange($field) |
113
|
|
|
{ |
114
|
|
|
if (isset($field['from'])) { |
115
|
|
|
$field['type'] = "renameColumn|from:{$field['to']}|required"; |
116
|
|
|
$field['name'] = $field['from']; |
117
|
|
|
} else { |
118
|
|
|
if (isset($field['to'])) { |
119
|
|
|
$field['name'] = $field['to']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$field['type'] = $this->parseColumnType($field['columnType']) . '|change'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $field; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Parsing column type to migration rule |
131
|
|
|
* |
132
|
|
|
* @param $columnType |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function parseColumnType($columnType): string |
136
|
|
|
{ |
137
|
|
|
$max = (int)preg_replace('/\D/', '', $columnType); |
138
|
|
|
$type = str_replace(' ', '', |
139
|
|
|
preg_replace( |
140
|
|
|
'/[^a-zA-Z]+/', |
141
|
|
|
'', |
142
|
|
|
explode(' ', $columnType, 2)[0] |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$type = key(array_filter([ |
147
|
|
|
'date' => [ |
148
|
|
|
'date' |
149
|
|
|
], |
150
|
|
|
'dateTime' => [ |
151
|
|
|
'timestamp' |
152
|
|
|
], |
153
|
|
|
'string' => [ |
154
|
|
|
'varchar', |
155
|
|
|
'text' |
156
|
|
|
], |
157
|
|
|
'integer' => [ |
158
|
|
|
'int', |
159
|
|
|
'bigint', |
160
|
|
|
'tinyint' |
161
|
|
|
], |
162
|
|
|
], static function($values) use($type) { |
163
|
|
|
return in_array(strtolower($type), $values, true); |
164
|
|
|
}, ARRAY_FILTER_USE_BOTH)); |
165
|
|
|
|
166
|
|
|
if ($max > 0) { |
167
|
|
|
$type .= "|max:{$max}"; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $type ?? 'string'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.