1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consigliere\Components\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Consigliere\Components\Support\Migrations\NameParser; |
7
|
|
|
use Consigliere\Components\Support\Migrations\SchemaParser; |
8
|
|
|
use Consigliere\Components\Support\Stub; |
9
|
|
|
use Consigliere\Components\Traits\ComponentCommandTrait; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
|
13
|
|
|
class MigrationCommand extends Command |
14
|
|
|
{ |
15
|
|
|
use ComponentCommandTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name = 'component:make-migration'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Generate a new migration for the specified component.'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the console command arguments. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
protected function getArguments() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
['name', InputArgument::REQUIRED, 'The migration name will be created.'], |
40
|
|
|
['component', InputArgument::OPTIONAL, 'The name of component will be created.'], |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the console command options. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
protected function getOptions() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
['fields', null, InputOption::VALUE_OPTIONAL, 'The specified fields table.', null], |
53
|
|
|
['plain', null, InputOption::VALUE_NONE, 'Create plain migration.'], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get schema parser. |
59
|
|
|
* |
60
|
|
|
* @return SchemaParser |
61
|
|
|
*/ |
62
|
|
|
public function getSchemaParser() |
63
|
|
|
{ |
64
|
|
|
return new SchemaParser($this->option('fields')); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @throws \InvalidArgumentException |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
protected function getTemplateContents() |
73
|
|
|
{ |
74
|
|
|
$parser = new NameParser($this->argument('name')); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if ($parser->isCreate()) { |
77
|
|
|
return Stub::create('/migration/create.stub', [ |
78
|
|
|
'class' => $this->getClass(), |
79
|
|
|
'table' => $parser->getTableName(), |
80
|
|
|
'fields' => $this->getSchemaParser()->render(), |
81
|
|
|
]); |
82
|
|
View Code Duplication |
} elseif ($parser->isAdd()) { |
|
|
|
|
83
|
|
|
return Stub::create('/migration/add.stub', [ |
84
|
|
|
'class' => $this->getClass(), |
85
|
|
|
'table' => $parser->getTableName(), |
86
|
|
|
'fields_up' => $this->getSchemaParser()->up(), |
87
|
|
|
'fields_down' => $this->getSchemaParser()->down(), |
88
|
|
|
]); |
89
|
|
|
} elseif ($parser->isDelete()) { |
90
|
|
|
return Stub::create('/migration/delete.stub', [ |
91
|
|
|
'class' => $this->getClass(), |
92
|
|
|
'table' => $parser->getTableName(), |
93
|
|
|
'fields_down' => $this->getSchemaParser()->up(), |
94
|
|
|
'fields_up' => $this->getSchemaParser()->down(), |
95
|
|
|
]); |
96
|
|
View Code Duplication |
} elseif ($parser->isDrop()) { |
|
|
|
|
97
|
|
|
return Stub::create('/migration/drop.stub', [ |
98
|
|
|
'class' => $this->getClass(), |
99
|
|
|
'table' => $parser->getTableName(), |
100
|
|
|
'fields' => $this->getSchemaParser()->render(), |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return Stub::create('/migration/plain.stub', [ |
105
|
|
|
'class' => $this->getClass(), |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
protected function getDestinationFilePath() |
113
|
|
|
{ |
114
|
|
|
$path = $this->laravel['components']->getComponentPath($this->getComponentName()); |
115
|
|
|
|
116
|
|
|
$generatorPath = $this->laravel['components']->config('paths.generator.migration'); |
117
|
|
|
|
118
|
|
|
return $path . $generatorPath . '/' . $this->getFileName() . '.php'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
private function getFileName() |
125
|
|
|
{ |
126
|
|
|
return date('Y_m_d_His_') . $this->getSchemaName(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array|string |
131
|
|
|
*/ |
132
|
|
|
private function getSchemaName() |
133
|
|
|
{ |
134
|
|
|
return $this->argument('name'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
private function getClassName() |
141
|
|
|
{ |
142
|
|
|
return Str::studly($this->argument('name')); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getClass() |
146
|
|
|
{ |
147
|
|
|
return $this->getClassName(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Run the command. |
152
|
|
|
*/ |
153
|
|
|
public function fire() |
154
|
|
|
{ |
155
|
|
|
parent::fire(); |
156
|
|
|
|
157
|
|
|
if (app()->environment() === 'testing') { |
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
$this->call('optimize'); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.