|
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
|
|
|
$item['model'] = $this->prependNamespace($item['model'] ?: $item['name']); |
|
98
|
|
|
} else if ($parser != 'relations') { |
|
99
|
|
|
switch($type) { |
|
100
|
|
|
case "hasManyThrough": |
|
101
|
|
|
if(! $item['through'] && $item['model']){ |
|
102
|
|
|
$item['through'] = $this->prependNamespace($item['model']); |
|
103
|
|
|
$item['model'] = $this->prependNamespace($item['name']); |
|
104
|
|
|
} else { |
|
105
|
|
|
$item['through'] = $this->prependNamespace($item['through']); |
|
106
|
|
|
$item['model'] = $this->prependNamespace($item['model']); |
|
107
|
|
|
} |
|
108
|
|
|
break; |
|
109
|
|
|
case "morphMany": |
|
110
|
|
|
case "morphToMany": |
|
111
|
|
|
case "morphedByMany": |
|
112
|
|
|
if(! $item['through']){ |
|
113
|
|
|
$item['through'] = $item['model']; |
|
114
|
|
|
$item['model'] = $this->prependNamespace($item['name']); |
|
115
|
|
|
} else { |
|
116
|
|
|
$item['model'] = $this->prependNamespace($item['model']); |
|
117
|
|
|
} |
|
118
|
|
|
break; |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
$relations[] = $template->with($item)->get(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
return $relations; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
protected function getRules() |
|
129
|
|
|
{ |
|
130
|
|
|
$items = $this->parseValue($this->option('rules'), 'rules'); |
|
131
|
|
|
if ($items === false) { |
|
132
|
|
|
return $this->spaces(8) . "// Validation rules"; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$template = $this->getTemplate('model/rule'); |
|
136
|
|
|
$rules = []; |
|
137
|
|
|
foreach ($items as $item) { |
|
138
|
|
|
$rules[] = $template->with($item)->get(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return implode(PHP_EOL, $rules); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function getAdditional() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->option('timestamps') === 'false' |
|
147
|
|
|
? " public \$timestamps = false;" . PHP_EOL . PHP_EOL |
|
148
|
|
|
: ''; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
protected function getUses() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->option('soft-deletes') == 'true' |
|
154
|
|
|
? ' use \Illuminate\Database\Eloquent\SoftDeletes;' . PHP_EOL . PHP_EOL |
|
155
|
|
|
: ''; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|