|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
|
7
|
|
|
use Nwidart\Modules\Support\Migrations\NameParser; |
|
8
|
|
|
use Nwidart\Modules\Support\Migrations\SchemaParser; |
|
9
|
|
|
use Nwidart\Modules\Support\Stub; |
|
10
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
13
|
|
|
|
|
14
|
|
|
class MigrationMakeCommand extends GeneratorCommand |
|
15
|
|
|
{ |
|
16
|
|
|
use ModuleCommandTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The console command name. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name = 'module:make-migration'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The console command description. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $description = 'Create a new migration for the specified module.'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the console command arguments. |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
92 |
|
protected function getArguments() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
92 |
|
['name', InputArgument::REQUIRED, 'The migration name will be created.'], |
|
41
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be created.'], |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the console command options. |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
92 |
|
protected function getOptions() |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
92 |
|
['fields', null, InputOption::VALUE_OPTIONAL, 'The specified fields table.', null], |
|
54
|
|
|
['plain', null, InputOption::VALUE_NONE, 'Create plain migration.'], |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get schema parser. |
|
60
|
|
|
* |
|
61
|
|
|
* @return SchemaParser |
|
62
|
|
|
*/ |
|
63
|
9 |
|
public function getSchemaParser() |
|
64
|
|
|
{ |
|
65
|
9 |
|
return new SchemaParser($this->option('fields')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @throws \InvalidArgumentException |
|
70
|
|
|
* |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
10 |
|
protected function getTemplateContents() |
|
74
|
|
|
{ |
|
75
|
10 |
|
$parser = new NameParser($this->argument('name')); |
|
76
|
|
|
|
|
77
|
10 |
|
if ($parser->isCreate()) { |
|
78
|
6 |
|
return Stub::create('/migration/create.stub', [ |
|
|
|
|
|
|
79
|
6 |
|
'class' => $this->getClass(), |
|
80
|
6 |
|
'table' => $parser->getTableName(), |
|
81
|
6 |
|
'fields' => $this->getSchemaParser()->render(), |
|
82
|
|
|
]); |
|
83
|
4 |
View Code Duplication |
} elseif ($parser->isAdd()) { |
|
|
|
|
|
|
84
|
1 |
|
return Stub::create('/migration/add.stub', [ |
|
|
|
|
|
|
85
|
1 |
|
'class' => $this->getClass(), |
|
86
|
1 |
|
'table' => $parser->getTableName(), |
|
87
|
1 |
|
'fields_up' => $this->getSchemaParser()->up(), |
|
88
|
1 |
|
'fields_down' => $this->getSchemaParser()->down(), |
|
89
|
|
|
]); |
|
90
|
3 |
|
} elseif ($parser->isDelete()) { |
|
91
|
1 |
|
return Stub::create('/migration/delete.stub', [ |
|
|
|
|
|
|
92
|
1 |
|
'class' => $this->getClass(), |
|
93
|
1 |
|
'table' => $parser->getTableName(), |
|
94
|
1 |
|
'fields_down' => $this->getSchemaParser()->up(), |
|
95
|
1 |
|
'fields_up' => $this->getSchemaParser()->down(), |
|
96
|
|
|
]); |
|
97
|
2 |
View Code Duplication |
} elseif ($parser->isDrop()) { |
|
|
|
|
|
|
98
|
1 |
|
return Stub::create('/migration/drop.stub', [ |
|
|
|
|
|
|
99
|
1 |
|
'class' => $this->getClass(), |
|
100
|
1 |
|
'table' => $parser->getTableName(), |
|
101
|
1 |
|
'fields' => $this->getSchemaParser()->render(), |
|
102
|
|
|
]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return Stub::create('/migration/plain.stub', [ |
|
|
|
|
|
|
106
|
1 |
|
'class' => $this->getClass(), |
|
107
|
|
|
]); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
10 |
|
protected function getDestinationFilePath() |
|
114
|
|
|
{ |
|
115
|
10 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
|
116
|
|
|
|
|
117
|
10 |
|
$generatorPath = GenerateConfigReader::read('migration'); |
|
118
|
|
|
|
|
119
|
10 |
|
return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php'; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
10 |
|
private function getFileName() |
|
126
|
|
|
{ |
|
127
|
10 |
|
return date('Y_m_d_His_') . $this->getSchemaName(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return array|string |
|
132
|
|
|
*/ |
|
133
|
10 |
|
private function getSchemaName() |
|
134
|
|
|
{ |
|
135
|
10 |
|
return $this->argument('name'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
10 |
|
private function getClassName() |
|
142
|
|
|
{ |
|
143
|
10 |
|
return Str::studly($this->argument('name')); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
10 |
|
public function getClass() |
|
147
|
|
|
{ |
|
148
|
10 |
|
return $this->getClassName(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Run the command. |
|
153
|
|
|
*/ |
|
154
|
10 |
|
public function handle() |
|
155
|
|
|
{ |
|
156
|
10 |
|
parent::handle(); |
|
157
|
|
|
|
|
158
|
10 |
|
if (app()->environment() === 'testing') { |
|
159
|
10 |
|
return; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.