4
4
5
5
use Blueprint \Contracts \Generator ;
6
6
use Blueprint \Model ;
7
+ use Illuminate \Support \Facades \File ;
7
8
use Illuminate \Support \Str ;
8
9
9
10
class MigrationGenerator implements Generator
10
11
{
12
+ const INDENT = ' ' ;
13
+
11
14
public function output (array $ tree ): void
12
15
{
13
16
// TODO: what if changing an existing model
14
- $ stub = file_get_contents ('stubs/migration.stub ' );
17
+ $ stub = File:: get ('stubs/migration.stub ' );
15
18
16
19
/** @var \Blueprint\Model $model */
17
20
foreach ($ tree ['models ' ] as $ model ) {
18
- file_put_contents (
21
+ File:: put (
19
22
$ this ->getPath ($ model ),
20
23
$ this ->populateStub ($ stub , $ model )
21
24
);
@@ -37,16 +40,27 @@ protected function buildDefinition(Model $model)
37
40
38
41
/** @var \Blueprint\Column $column */
39
42
foreach ($ model ->columns () as $ column ) {
40
- $ definition .= '$table-> ' . $ column ->dataType () . "(' {$ column ->name ()}' " ;
43
+ $ dataType = $ column ->dataType ();
44
+ if ($ column ->name () === 'id ' ) {
45
+ $ dataType = 'increments ' ;
46
+ } elseif ($ column ->dataType () === 'id ' ) {
47
+ $ dataType = 'unsignedBigInteger ' ;
48
+ }
49
+
50
+ $ definition .= self ::INDENT . '$table-> ' . $ dataType . "(' {$ column ->name ()}' " ;
51
+
41
52
if (!empty ($ column ->attributes ())) {
42
- // TODO: what about set and enum?
43
- $ definition .= ', ' . implode (', ' , $ column ->attributes ());
53
+ $ definition .= ', ' ;
54
+ if (in_array ($ column ->dataType (), ['set ' , 'enum ' ])) {
55
+ $ definition .= json_encode ($ column ->attributes ());
56
+ } else {
57
+ $ definition .= implode (', ' , $ column ->attributes ());
58
+ }
44
59
}
45
60
$ definition .= ') ' ;
46
61
47
62
foreach ($ column ->modifiers () as $ modifier ) {
48
63
if (is_array ($ modifier )) {
49
- // TODO: properly handle quoted values
50
64
$ definition .= "-> " . key ($ modifier ) . "( " . current ($ modifier ) . ") " ;
51
65
} else {
52
66
$ definition .= '-> ' . $ modifier . '() ' ;
@@ -57,7 +71,7 @@ protected function buildDefinition(Model $model)
57
71
}
58
72
59
73
if ($ model ->usesTimestamps ()) {
60
- $ definition .= '$table->timestamps(); ' . PHP_EOL ;
74
+ $ definition .= self :: INDENT . '$table->timestamps(); ' . PHP_EOL ;
61
75
}
62
76
63
77
return trim ($ definition );
@@ -70,6 +84,6 @@ protected function getClassName(Model $model)
70
84
71
85
protected function getPath (Model $ model )
72
86
{
73
- return 'build/ ' . date ('Y_m_d_His ' ) . '_create_ ' . $ model ->tableName () . '_table.php ' ;
87
+ return 'build/ ' . \ Carbon \Carbon:: now ()-> format ('Y_m_d_His ' ) . '_create_ ' . $ model ->tableName () . '_table.php ' ;
74
88
}
75
89
}
0 commit comments