1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators; |
4
|
|
|
|
5
|
|
|
use Milkmeowo\Framework\Repository\Generators\Migrations\NameParser; |
6
|
|
|
use Milkmeowo\Framework\Repository\Generators\Migrations\SchemaParser; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; |
8
|
|
|
|
9
|
|
|
class MigrationGenerator extends Generator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get stub name. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $stub = 'migration/plain'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get base path of destination file. |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function getBasePath() |
24
|
|
|
{ |
25
|
|
|
return base_path().'/database/migrations/'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get destination path for generated file. |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getPath() |
34
|
|
|
{ |
35
|
|
|
return $this->getBasePath().$this->getFileName().'.php'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get generator path config node. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getPathConfigNode() |
44
|
|
|
{ |
45
|
|
|
return ''; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get root namespace. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getRootNamespace() |
54
|
|
|
{ |
55
|
|
|
return ''; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get migration name. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getMigrationName() |
64
|
|
|
{ |
65
|
|
|
return strtolower($this->name); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get file name. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getFileName() |
74
|
|
|
{ |
75
|
|
|
return date('Y_m_d_His_').$this->getMigrationName(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get schema parser. |
80
|
|
|
* |
81
|
|
|
* @return SchemaParser |
82
|
|
|
*/ |
83
|
|
|
public function getSchemaParser() |
84
|
|
|
{ |
85
|
|
|
return new SchemaParser($this->fields); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get name parser. |
90
|
|
|
* |
91
|
|
|
* @return NameParser |
92
|
|
|
*/ |
93
|
|
|
public function getNameParser() |
94
|
|
|
{ |
95
|
|
|
return new NameParser($this->name); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get stub templates. |
100
|
|
|
* |
101
|
|
|
* @return Stub |
102
|
|
|
*/ |
103
|
|
|
public function getStub() |
104
|
|
|
{ |
105
|
|
|
$parser = $this->getNameParser(); |
106
|
|
|
|
107
|
|
|
$action = $parser->getAction(); |
108
|
|
|
switch ($action) { |
109
|
|
|
case 'add': |
110
|
|
|
case 'append': |
111
|
|
|
case 'update': |
112
|
|
View Code Duplication |
case 'insert': |
|
|
|
|
113
|
|
|
$file = 'change'; |
114
|
|
|
$replacements = [ |
115
|
|
|
'class' => $this->getClass(), |
116
|
|
|
'table' => $parser->getTable(), |
117
|
|
|
'fields_up' => $this->getSchemaParser()->up(), |
118
|
|
|
'fields_down' => $this->getSchemaParser()->down(), |
119
|
|
|
]; |
120
|
|
|
break; |
121
|
|
|
|
122
|
|
|
case 'delete': |
123
|
|
|
case 'remove': |
124
|
|
View Code Duplication |
case 'alter': |
|
|
|
|
125
|
|
|
$file = 'change'; |
126
|
|
|
$replacements = [ |
127
|
|
|
'class' => $this->getClass(), |
128
|
|
|
'table' => $parser->getTable(), |
129
|
|
|
'fields_down' => $this->getSchemaParser()->up(), |
130
|
|
|
'fields_up' => $this->getSchemaParser()->down(), |
131
|
|
|
]; |
132
|
|
|
break; |
133
|
|
|
default: |
134
|
|
|
$file = 'create'; |
135
|
|
|
$replacements = [ |
136
|
|
|
'class' => $this->getClass(), |
137
|
|
|
'table' => $parser->getTable(), |
138
|
|
|
'fields' => $this->getSchemaParser()->up(), |
139
|
|
|
]; |
140
|
|
|
break; |
141
|
|
|
} |
142
|
|
|
$path = config('repository.generator.stubsOverridePath', __DIR__); |
143
|
|
|
|
144
|
|
|
if (! file_exists($path."/Stubs/migration/{$file}.stub")) { |
145
|
|
|
$path = __DIR__; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (! file_exists($path."/Stubs/migration/{$file}.stub")) { |
149
|
|
|
throw new FileNotFoundException($path."/Stubs/migration/{$file}.stub"); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return Stub::create($path."/Stubs/migration/{$file}.stub", $replacements); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.