1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JunaidQadirB\Cray\Console\Commands; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use JunaidQadirB\Cray\Console\Contracts\GeneratorCommand; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class ModelMakeCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The console command name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = 'cray:model'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Create a new Eloquent model class'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The type of class being generated. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $type = 'Model'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the console command. |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function handle() |
39
|
|
|
{ |
40
|
|
|
if (parent::handle() === false && !$this->option('force')) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
if ($this->option('all')) { |
44
|
|
|
$this->input->setOption('factory', true); |
45
|
|
|
$this->input->setOption('migration', true); |
46
|
|
|
$this->input->setOption('controller', true); |
47
|
|
|
$this->input->setOption('resource', true); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($this->option('factory')) { |
51
|
|
|
$this->createFactory(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($this->option('migration')) { |
55
|
|
|
$this->createMigration(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->option('controller') || $this->option('resource')) { |
59
|
|
|
$this->createController(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create a model factory for the model. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
protected function createFactory() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$factory = Str::studly(class_basename($this->argument('name'))); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->call('cray:factory', [ |
73
|
|
|
'name' => "{$factory}Factory", |
74
|
|
|
'--model' => $this->argument('name'), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create a migration file for the model. |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
protected function createMigration() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$table = Str::plural(Str::snake(class_basename($this->argument('name')))); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->call('cray:migration', [ |
88
|
|
|
'name' => "create_{$table}_table", |
89
|
|
|
'--create' => $table, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create a controller for the model. |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
protected function createController() |
99
|
|
|
{ |
100
|
|
|
$controller = Str::studly(class_basename($this->argument('name'))); |
|
|
|
|
101
|
|
|
|
102
|
|
|
$modelName = $this->qualifyClass($this->getNameInput()); |
103
|
|
|
|
104
|
|
|
$this->call('cray:controller', [ |
105
|
|
|
'name' => "{$controller}Controller", |
106
|
|
|
'--model' => $this->option('resource') ? $modelName : null, |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the stub file for the generator. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function getStub() |
116
|
|
|
{ |
117
|
|
|
if ($this->option('pivot')) { |
118
|
|
|
return config('cray.stubs_dir').'/pivot.model.stub'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return config('cray.stubs_dir').'/model.stub'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the default namespace for the class. |
126
|
|
|
* |
127
|
|
|
* @param string $rootNamespace |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function getDefaultNamespace($rootNamespace) |
132
|
|
|
{ |
133
|
|
|
return $rootNamespace; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the console command options. |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
protected function getOptions() |
142
|
|
|
{ |
143
|
|
|
return [ |
144
|
|
|
[ |
145
|
|
|
'all', |
146
|
|
|
'a', |
147
|
|
|
InputOption::VALUE_NONE, |
148
|
|
|
'Generate a migration, factory, and resource controller for the model', |
149
|
|
|
], |
150
|
|
|
|
151
|
|
|
['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'], |
152
|
|
|
|
153
|
|
|
['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the model'], |
154
|
|
|
|
155
|
|
|
['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists.'], |
156
|
|
|
|
157
|
|
|
['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model.'], |
158
|
|
|
|
159
|
|
|
[ |
160
|
|
|
'pivot', |
161
|
|
|
'p', |
162
|
|
|
InputOption::VALUE_NONE, |
163
|
|
|
'Indicates if the generated model should be a custom intermediate table model.', |
164
|
|
|
], |
165
|
|
|
|
166
|
|
|
[ |
167
|
|
|
'resource', |
168
|
|
|
'r', |
169
|
|
|
InputOption::VALUE_NONE, |
170
|
|
|
'Indicates if the generated controller should be a resource controller.', |
171
|
|
|
], |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
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.