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
|
|
|
{--rules= : fields validation rules.} |
15
|
|
|
{--timestamps=true : enables timestamps on the model.} |
16
|
|
|
{--path=app : where to store the model php file.} |
17
|
|
|
{--soft-deletes= : adds SoftDeletes trait to the model.} |
18
|
|
|
{--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} |
19
|
|
|
{--force= : override the existing files} |
20
|
|
|
'; |
21
|
|
|
|
22
|
|
|
protected $description = 'Generates a model class for a RESTfull resource'; |
23
|
|
|
|
24
|
|
|
public function handle() |
25
|
|
|
{ |
26
|
|
|
$name = $this->argument('name'); |
27
|
|
|
$path = $this->option('path'); |
28
|
|
|
|
29
|
|
|
$content = $this->getTemplate('model') |
30
|
|
|
->with([ |
31
|
|
|
'name' => $name, |
32
|
|
|
'namespace' => $this->getNamespace(), |
33
|
|
|
'fillable' => $this->getAsArrayFields('fillable'), |
34
|
|
|
'dates' => $this->getAsArrayFields('dates'), |
35
|
|
|
'relations' => $this->getRelations(), |
36
|
|
|
'rules' => $this->getRules(), |
37
|
|
|
'additional' => $this->getAdditional(), |
38
|
|
|
'uses' => $this->getUses() |
39
|
|
|
]) |
40
|
|
|
->get(); |
41
|
|
|
|
42
|
|
|
$this->save($content, "./{$path}/{$name}.php", "{$name} model"); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getAsArrayFields($arg, $isOption = true) |
46
|
|
|
{ |
47
|
|
|
$arg = ($isOption) ? $this->option($arg) : $this->argument($arg); |
48
|
|
|
if(is_string($arg)){ |
49
|
|
|
$arg = explode(',', $arg); |
50
|
|
|
} else if(! is_array($arg)) { |
51
|
|
|
$arg = []; |
52
|
|
|
} |
53
|
|
|
return implode(', ', array_map(function($item){ |
54
|
|
|
return '"' . $item . '"'; |
55
|
|
|
}, $arg)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getNamespace() |
59
|
|
|
{ |
60
|
|
|
return str_replace(' ', '\\', ucwords(str_replace('/', ' ', $this->option('path')))); |
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
|
|
|
); |
71
|
|
|
|
72
|
|
|
if(empty($relations)){ |
73
|
|
|
return " // Relationships"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return implode(PHP_EOL, $relations); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function getRelationsByType($type, $option, $withTimestamps = false) |
80
|
|
|
{ |
81
|
|
|
$relations = []; |
82
|
|
|
$option = $this->option($option); |
83
|
|
|
if($option){ |
84
|
|
|
|
85
|
|
|
$items = $this->getArgumentParser('relations')->parse($option); |
86
|
|
|
|
87
|
|
|
$template = ($withTimestamps) ? 'model/relation-with-timestamps' : 'model/relation'; |
88
|
|
|
$template = $this->getTemplate($template); |
89
|
|
|
foreach ($items as $item) { |
90
|
|
|
$item['type'] = $type; |
91
|
|
|
if(! $item['model']){ |
92
|
|
|
$item['model'] = $this->getNamespace() . '\\' . ucwords(str_singular($item['name'])); |
93
|
|
|
} else if(strpos($item['model'], '\\') === false ){ |
94
|
|
|
$item['model'] = $this->getNamespace() . '\\' . $item['model']; |
95
|
|
|
} |
96
|
|
|
$relations[] = $template->with($item)->get(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return $relations; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
protected function getRules() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$rules = $this->option('rules'); |
105
|
|
|
if(! $rules){ |
106
|
|
|
return " // Validation rules"; |
107
|
|
|
} |
108
|
|
|
$items = $rules; |
109
|
|
|
if(! $this->option('parsed')){ |
110
|
|
|
$items = $this->getArgumentParser('rules')->parse($rules); |
111
|
|
|
} |
112
|
|
|
$rules = []; |
113
|
|
|
$template = $this->getTemplate('model/rule'); |
114
|
|
|
foreach ($items as $item) { |
115
|
|
|
$rules[] = $template->with($item)->get(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return implode(PHP_EOL, $rules); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getAdditional() |
122
|
|
|
{ |
123
|
|
|
return $this->option('timestamps') == 'false' |
124
|
|
|
? " public \$timestamps = false;" . PHP_EOL . PHP_EOL |
125
|
|
|
: ''; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function getUses() |
129
|
|
|
{ |
130
|
|
|
return $this->option('soft-deletes') == 'true' |
131
|
|
|
? ' use Illuminate\Database\Eloquent\SoftDeletes;' . PHP_EOL . PHP_EOL |
132
|
|
|
: ''; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
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.