1
|
|
|
<?php namespace Wn\Generators\Commands; |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class ModelCommand extends BaseCommand { |
5
|
|
|
|
6
|
|
|
protected $signature = 'wn:model |
7
|
|
|
{name : Name of the model.} |
8
|
|
|
{--fillable= : the fillable fields.} |
9
|
|
|
{--dates= : date fields.} |
10
|
|
|
{--has-many= : hasMany relationships.} |
11
|
|
|
{--has-one= : hasOne relationships.} |
12
|
|
|
{--belongs-to= : belongsTo relationships.} |
13
|
|
|
{--belongs-to-many= : belongsToMany relationships.} |
14
|
|
|
{--has-many-through= : hasManyThrough relationships.} |
15
|
|
|
{--morph-to= : morphTo relationships.} |
16
|
|
|
{--morph-many= : morphMany relationships.} |
17
|
|
|
{--morph-to-many= : morphToMany relationships.} |
18
|
|
|
{--morphed-by-many= : morphedByMany relationships.} |
19
|
|
|
{--rules= : fields validation rules.} |
20
|
|
|
{--timestamps=true : enables timestamps on the model.} |
21
|
|
|
{--path=app : where to store the model php file.} |
22
|
|
|
{--soft-deletes= : adds SoftDeletes trait to the model.} |
23
|
|
|
{--parsed : tells the command that arguments have been already parsed. To use when calling the command from an other command and passing the parsed arguments and options} |
24
|
|
|
{--force= : override the existing files} |
25
|
|
|
'; |
26
|
|
|
|
27
|
|
|
protected $description = 'Generates a model class for a RESTfull resource'; |
28
|
|
|
|
29
|
|
|
public function handle() |
30
|
|
|
{ |
31
|
|
|
$name = $this->argument('name'); |
32
|
|
|
$path = $this->option('path'); |
33
|
|
|
|
34
|
|
|
$content = $this->getTemplate('model') |
35
|
|
|
->with([ |
36
|
|
|
'name' => $name, |
37
|
|
|
'namespace' => $this->getNamespace(), |
38
|
|
|
'fillable' => $this->getAsArrayFields('fillable'), |
39
|
|
|
'dates' => $this->getAsArrayFields('dates'), |
40
|
|
|
'relations' => $this->getRelations(), |
41
|
|
|
'rules' => $this->getRules(), |
42
|
|
|
'additional' => $this->getAdditional(), |
43
|
|
|
'uses' => $this->getUses() |
44
|
|
|
]) |
45
|
|
|
->get(); |
46
|
|
|
|
47
|
|
|
$this->save($content, "./{$path}/{$name}.php", "{$name} model"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function getAsArrayFields($arg, $isOption = true) |
51
|
|
|
{ |
52
|
|
|
$arg = ($isOption) ? $this->option($arg) : $this->argument($arg); |
53
|
|
|
if(is_string($arg)){ |
54
|
|
|
$arg = explode(',', $arg); |
55
|
|
|
} else if(! is_array($arg)) { |
56
|
|
|
$arg = []; |
57
|
|
|
} |
58
|
|
|
return implode(', ', array_map(function($item){ |
59
|
|
|
return '"' . $item . '"'; |
60
|
|
|
}, $arg)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function getRelations() |
64
|
|
|
{ |
65
|
|
|
$relations = array_merge([], |
66
|
|
|
$this->getRelationsByType('hasOne', 'has-one'), |
67
|
|
|
$this->getRelationsByType('hasMany', 'has-many'), |
68
|
|
|
$this->getRelationsByType('belongsTo', 'belongs-to'), |
69
|
|
|
$this->getRelationsByType('belongsToMany', 'belongs-to-many', true), |
70
|
|
|
$this->getRelationsByType('hasManyThrough', 'has-many-through', false, 'relations-hasManyThrough'), |
71
|
|
|
$this->getRelationsByType('morphTo', 'morph-to', false, 'relations-morphTo'), |
72
|
|
|
$this->getRelationsByType('morphMany', 'morph-many', false, 'relations-morphMany'), |
73
|
|
|
$this->getRelationsByType('morphToMany', 'morph-to-many', false, 'relations-morphMany'), |
74
|
|
|
$this->getRelationsByType('morphedByMany', 'morphed-by-many', false, 'relations-morphMany') |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
if(empty($relations)){ |
78
|
|
|
return " // Relationships"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return implode(PHP_EOL, $relations); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getRelationsByType($type, $option, $withTimestamps = false, $parser = 'relations') |
85
|
|
|
{ |
86
|
|
|
$relations = []; |
87
|
|
|
$option = $this->option($option); |
88
|
|
|
if($option){ |
89
|
|
|
|
90
|
|
|
$items = $this->getArgumentParser($parser)->parse($option); |
91
|
|
|
|
92
|
|
|
$template = ($withTimestamps) ? 'model/relation-with-timestamps' : ($parser=='relations'?'model/relation':'model/relation-'.$type); |
93
|
|
|
$template = $this->getTemplate($template); |
94
|
|
|
foreach ($items as $item) { |
95
|
|
|
$item['type'] = $type; |
96
|
|
|
if ($parser == 'relations') { |
97
|
|
View Code Duplication |
if(! $item['model']){ |
|
|
|
|
98
|
|
|
$item['model'] = $this->getNamespace() . '\\' . ucwords(str_singular($item['name'])); |
99
|
|
|
} else if(strpos($item['model'], '\\') === false ){ |
100
|
|
|
$item['model'] = $this->getNamespace() . '\\' . $item['model']; |
101
|
|
|
} |
102
|
|
|
} else if ($parser != 'relations') { |
103
|
|
|
switch($type) { |
104
|
|
|
case "hasManyThrough": |
105
|
|
|
if(! $item['through'] && $item['model']){ |
106
|
|
|
$item['through'] = $item['model']; |
107
|
|
|
$item['model'] = $item['name']; |
108
|
|
|
|
109
|
|
|
if(strpos($item['through'], '\\') === false ){ |
110
|
|
|
$item['through'] = $this->getNamespace() . '\\' . ucwords(str_singular($item['through'])); |
111
|
|
|
} |
112
|
|
|
if(strpos($item['model'], '\\') === false ){ |
113
|
|
|
$item['model'] = $this->getNamespace() . '\\' . ucwords(str_singular($item['model'])); |
114
|
|
|
} |
115
|
|
View Code Duplication |
} else { |
|
|
|
|
116
|
|
|
if(strpos($item['through'], '\\') === false ){ |
117
|
|
|
$item['through'] = $this->getNamespace() . '\\' . $item['through']; |
118
|
|
|
} |
119
|
|
|
if(strpos($item['model'], '\\') === false ){ |
120
|
|
|
$item['model'] = $this->getNamespace() . '\\' . $item['model']; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
break; |
124
|
|
|
case "morphMany": |
125
|
|
|
case "morphToMany": |
126
|
|
|
case "morphedByMany": |
127
|
|
|
if(! $item['through']){ |
128
|
|
|
$item['through'] = $item['model']; |
129
|
|
|
$item['model'] = ucwords(str_singular($item['name'])); |
130
|
|
|
} |
131
|
|
|
if(strpos($item['model'], '\\') === false ){ |
132
|
|
|
$item['model'] = $this->getNamespace() . '\\' . $item['model']; |
133
|
|
|
} |
134
|
|
|
break; |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
$relations[] = $template->with($item)->get(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
return $relations; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
protected function getRules() |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$rules = $this->option('rules'); |
147
|
|
|
if(! $rules){ |
148
|
|
|
return " // Validation rules"; |
149
|
|
|
} |
150
|
|
|
$items = $rules; |
151
|
|
|
if(! $this->option('parsed')){ |
152
|
|
|
$items = $this->getArgumentParser('rules')->parse($rules); |
153
|
|
|
} |
154
|
|
|
$rules = []; |
155
|
|
|
$template = $this->getTemplate('model/rule'); |
156
|
|
|
foreach ($items as $item) { |
157
|
|
|
$rules[] = $template->with($item)->get(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return implode(PHP_EOL, $rules); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function getAdditional() |
164
|
|
|
{ |
165
|
|
|
return $this->option('timestamps') == 'false' |
166
|
|
|
? " public \$timestamps = false;" . PHP_EOL . PHP_EOL |
167
|
|
|
: ''; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
protected function getUses() |
171
|
|
|
{ |
172
|
|
|
return $this->option('soft-deletes') == 'true' |
173
|
|
|
? ' use \Illuminate\Database\Eloquent\SoftDeletes;' . PHP_EOL . PHP_EOL |
174
|
|
|
: ''; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.