1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Salah3id\Domains\Repository\Generators; |
4
|
|
|
|
5
|
|
|
use Salah3id\Domains\Repository\Generators\Migrations\NameParser; |
6
|
|
|
use Salah3id\Domains\Repository\Generators\Migrations\SchemaParser; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MigrationGenerator |
11
|
|
|
* @package Salah3id\Domains\Repository\Generators |
12
|
|
|
* @author Anderson Andrade <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class MigrationGenerator extends Generator |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get stub name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $stub = 'migration/plain'; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get base path of destination file. |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function getBasePath() |
31
|
|
|
{ |
32
|
|
|
return $this->domainPath . '/database/migrations/'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get destination path for generated file. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getPath() |
42
|
|
|
{ |
43
|
|
|
return $this->getBasePath() . $this->getFileName() . '.php'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get generator path config node. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getPathConfigNode() |
53
|
|
|
{ |
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get root namespace. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getRootNamespace() |
64
|
|
|
{ |
65
|
|
|
return ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get migration name. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getMigrationName() |
75
|
|
|
{ |
76
|
|
|
return strtolower($this->name); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get file name. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getFileName() |
86
|
|
|
{ |
87
|
|
|
return date('Y_m_d_His_') . $this->getMigrationName(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get schema parser. |
93
|
|
|
* |
94
|
|
|
* @return SchemaParser |
95
|
|
|
*/ |
96
|
|
|
public function getSchemaParser() |
97
|
|
|
{ |
98
|
|
|
return new SchemaParser($this->fields); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get name parser. |
104
|
|
|
* |
105
|
|
|
* @return NameParser |
106
|
|
|
*/ |
107
|
|
|
public function getNameParser() |
108
|
|
|
{ |
109
|
|
|
return new NameParser($this->name); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get stub templates. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getStub() |
119
|
|
|
{ |
120
|
|
|
$parser = $this->getNameParser(); |
121
|
|
|
|
122
|
|
|
$action = $parser->getAction(); |
123
|
|
|
switch ($action) { |
124
|
|
|
case 'add': |
125
|
|
|
case 'append': |
126
|
|
|
case 'update': |
127
|
|
|
case 'insert': |
128
|
|
|
$file = 'change'; |
129
|
|
|
$replacements = [ |
130
|
|
|
'class' => $this->getClass(), |
131
|
|
|
'table' => $parser->getTable(), |
132
|
|
|
'fields_up' => $this->getSchemaParser()->up(), |
133
|
|
|
'fields_down' => $this->getSchemaParser()->down(), |
134
|
|
|
]; |
135
|
|
|
break; |
136
|
|
|
|
137
|
|
|
case 'delete': |
138
|
|
|
case 'remove': |
139
|
|
|
case 'alter': |
140
|
|
|
$file = 'change'; |
141
|
|
|
$replacements = [ |
142
|
|
|
'class' => $this->getClass(), |
143
|
|
|
'table' => $parser->getTable(), |
144
|
|
|
'fields_down' => $this->getSchemaParser()->up(), |
145
|
|
|
'fields_up' => $this->getSchemaParser()->down(), |
146
|
|
|
]; |
147
|
|
|
break; |
148
|
|
|
default: |
149
|
|
|
$file = 'create'; |
150
|
|
|
$replacements = [ |
151
|
|
|
'class' => $this->getClass(), |
152
|
|
|
'table' => $parser->getTable(), |
153
|
|
|
'fields' => $this->getSchemaParser()->up(), |
154
|
|
|
]; |
155
|
|
|
break; |
156
|
|
|
} |
157
|
|
|
$path = config('domains.paths.repo-generator.stubsOverridePath', __DIR__); |
158
|
|
|
|
159
|
|
|
if (!file_exists($path . "/Stubs/migration/{$file}.stub")) { |
160
|
|
|
$path = __DIR__; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!file_exists($path . "/Stubs/migration/{$file}.stub")) { |
164
|
|
|
throw new FileNotFoundException($path . "/Stubs/migration/{$file}.stub"); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return Stub::create($path . "/Stubs/migration/{$file}.stub", $replacements); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|