1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Pingpong\Generators\Migrations\NameParser; |
7
|
|
|
use Pingpong\Generators\Migrations\SchemaParser; |
8
|
|
|
use Pingpong\Support\Stub; |
9
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
|
13
|
|
|
class MigrationCommand extends GeneratorCommand |
14
|
|
|
{ |
15
|
|
|
use ModuleCommandTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name = 'module:make-migration'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Generate a new migration for the specified module.'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the console command arguments. |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
16 |
|
protected function getArguments() |
37
|
|
|
{ |
38
|
|
|
return array( |
39
|
16 |
|
array('name', InputArgument::REQUIRED, 'The migration name will be created.'), |
40
|
16 |
|
array('module', InputArgument::OPTIONAL, 'The name of module will be created.'), |
41
|
16 |
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the console command options. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
16 |
|
protected function getOptions() |
50
|
|
|
{ |
51
|
|
|
return array( |
52
|
16 |
|
array('fields', null, InputOption::VALUE_OPTIONAL, 'The specified fields table.', null), |
53
|
16 |
|
array('plain', null, InputOption::VALUE_NONE, 'Create plain migration.'), |
54
|
16 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get schema parser. |
59
|
|
|
* |
60
|
|
|
* @return SchemaParser |
61
|
|
|
*/ |
62
|
1 |
|
public function getSchemaParser() |
63
|
|
|
{ |
64
|
1 |
|
return new SchemaParser($this->option('fields')); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @throws InvalidMigrationNameException |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
1 |
|
protected function getTemplateContents() |
73
|
|
|
{ |
74
|
1 |
|
$parser = new NameParser($this->argument('name')); |
|
|
|
|
75
|
|
|
|
76
|
1 |
|
if ($parser->isCreate()) { |
77
|
1 |
|
return Stub::create('/migration/create.stub', [ |
78
|
1 |
|
'class' => $this->getClass(), |
79
|
1 |
|
'table' => $parser->getTable(), |
80
|
1 |
|
'fields' => $this->getSchemaParser()->render(), |
81
|
1 |
|
]); |
82
|
|
View Code Duplication |
} elseif ($parser->isAdd()) { |
|
|
|
|
83
|
|
|
return Stub::create('/migration/add.stub', [ |
84
|
|
|
'class' => $this->getClass(), |
85
|
|
|
'table' => $parser->getTable(), |
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->getTable(), |
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->getTable(), |
100
|
|
|
'fields' => $this->getSchemaParser()->render(), |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw new \InvalidArgumentException('Invalid migration name'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
1 |
|
protected function getDestinationFilePath() |
111
|
|
|
{ |
112
|
1 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
113
|
|
|
|
114
|
1 |
|
$generatorPath = $this->laravel['modules']->config('paths.generator.migration'); |
115
|
|
|
|
116
|
1 |
|
return $path.$generatorPath.'/'.$this->getFileName().'.php'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
1 |
|
private function getFileName() |
123
|
|
|
{ |
124
|
1 |
|
return date('Y_m_d_His_').$this->getSchemaName(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array|string |
129
|
|
|
*/ |
130
|
1 |
|
private function getSchemaName() |
131
|
|
|
{ |
132
|
1 |
|
return $this->argument('name'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
1 |
|
private function getClassName() |
139
|
|
|
{ |
140
|
1 |
|
return Str::studly($this->argument('name')); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
public function getClass() |
144
|
|
|
{ |
145
|
1 |
|
return $this->getClassName(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Run the command. |
150
|
|
|
*/ |
151
|
1 |
|
public function fire() |
152
|
|
|
{ |
153
|
1 |
|
parent::fire(); |
154
|
|
|
|
155
|
1 |
|
if (app()->environment() === 'testing') { |
156
|
1 |
|
return; |
157
|
|
|
} |
158
|
|
|
$this->call('optimize'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
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.