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