1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consigliere\Components\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Consigliere\Components\Support\Stub; |
7
|
|
|
use Consigliere\Components\Traits\ComponentCommandTrait; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
|
11
|
|
|
class ModelCommand extends Command |
12
|
|
|
{ |
13
|
|
|
use ComponentCommandTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name of argument name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $argumentName = 'model'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command name. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $name = 'component:make-model'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Generate new model for the specified component.'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the console command arguments. |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
protected function getArguments() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
['model', InputArgument::REQUIRED, 'The name of model will be created.'], |
45
|
|
|
['component', InputArgument::OPTIONAL, 'The name of component will be used.'], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the console command options. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
protected function getOptions() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
['fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null], |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
protected function getTemplateContents() |
65
|
|
|
{ |
66
|
|
|
$component = $this->laravel['components']->findOrFail($this->getComponentName()); |
67
|
|
|
|
68
|
|
|
return (new Stub('/model.stub', [ |
69
|
|
|
'NAME' => $this->getModelName(), |
70
|
|
|
'FILLABLE' => $this->getFillable(), |
71
|
|
|
'NAMESPACE' => $this->getClassNamespace($component), |
72
|
|
|
'CLASS' => $this->getClass(), |
73
|
|
|
'LOWER_NAME' => $component->getLowerName(), |
74
|
|
|
'COMPONENT' => $this->getComponentName(), |
75
|
|
|
'STUDLY_NAME' => $component->getStudlyName(), |
76
|
|
|
'COMPONENT_NAMESPACE' => $this->laravel['components']->config('namespace'), |
77
|
|
|
]))->render(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
protected function getDestinationFilePath() |
84
|
|
|
{ |
85
|
|
|
$path = $this->laravel['components']->getComponentPath($this->getComponentName()); |
86
|
|
|
|
87
|
|
|
$seederPath = $this->laravel['components']->config('paths.generator.model'); |
88
|
|
|
|
89
|
|
|
return $path . $seederPath . '/' . $this->getModelName() . '.php'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed|string |
94
|
|
|
*/ |
95
|
|
|
private function getModelName() |
96
|
|
|
{ |
97
|
|
|
return Str::studly($this->argument('model')); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
private function getFillable() |
104
|
|
|
{ |
105
|
|
|
$fillable = $this->option('fillable'); |
106
|
|
|
|
107
|
|
|
if (!is_null($fillable)) { |
108
|
|
|
$arrays = explode(',', $fillable); |
109
|
|
|
|
110
|
|
|
return json_encode($arrays); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return '[]'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get default namespace. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getDefaultNamespace() |
122
|
|
|
{ |
123
|
|
|
return $this->laravel['components']->config('paths.generator.model'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.