1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JunaidQadirB\Cray\Console\Commands; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Config; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use JunaidQadirB\Cray\Console\Contracts\GeneratorCommand; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
|
12
|
|
|
class ViewMakeCommand extends GeneratorCommand |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The console command name. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name = 'cray:view'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Create a new View'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The type of class being generated. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $type = 'Model'; |
34
|
|
|
|
35
|
|
|
private $fileName = 'index'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Execute the console command. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function handle() |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/* if (parent::handle() === false && ! $this->option('force')) { |
46
|
|
|
return; |
47
|
|
|
}*/ |
48
|
|
|
if (!$this->option('index') && !$this->option('create') && !$this->option('edit') && !$this->option('show') && !$this->option('all')) { |
49
|
|
|
$this->input->setOption('all', true); |
50
|
|
|
} |
51
|
|
|
$this->createView(); |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a view for the model. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
protected function createView() |
61
|
|
|
{ |
62
|
|
|
$path = $this->createViewDirectory(); |
63
|
|
|
|
64
|
|
|
if ($this->option('all')) { |
65
|
|
|
$this->buildView('index', $path); |
66
|
|
|
$this->buildView('create', $path); |
67
|
|
|
$this->buildView('edit', $path); |
68
|
|
|
$this->buildView('show', $path); |
69
|
|
|
$this->createDeleteView($path); |
70
|
|
|
|
71
|
|
|
} else { |
72
|
|
View Code Duplication |
if ($this->option('index') || $this->option('all')) { |
|
|
|
|
73
|
|
|
$this->input->setOption('index', true); |
74
|
|
|
$this->buildView('index', $path); |
75
|
|
|
$this->createDeleteView($path); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
if ($this->option('create') || $this->option('all')) { |
|
|
|
|
79
|
|
|
$this->input->setOption('create', true); |
80
|
|
|
$this->buildView('create', $path); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
if ($this->option('edit') || $this->option('all')) { |
|
|
|
|
84
|
|
|
$this->input->setOption('edit', true); |
85
|
|
|
$this->buildView('edit', $path); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
if ($this->option('show') || $this->option('all')) { |
|
|
|
|
89
|
|
|
$this->input->setOption('show', true); |
90
|
|
|
$this->buildView('show', $path); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
|
|
protected function createViewDirectory() |
100
|
|
|
{ |
101
|
|
|
$name = Str::studly(class_basename($this->argument('name'))); |
|
|
|
|
102
|
|
|
$viewDirSlug = Str::slug(Str::plural(str_to_words($name), 2)); |
103
|
|
|
$viewPath = Config::get('view.paths')[0]; |
104
|
|
|
$dir = $this->option('dir'); |
105
|
|
|
|
106
|
|
|
$path = $viewPath . '/' . $viewDirSlug; |
107
|
|
|
|
108
|
|
|
if ($dir) { |
109
|
|
|
$path = $viewPath . '/' . $dir . '/' . $viewDirSlug; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!file_exists($path)) { |
113
|
|
|
mkdir($path, 0777, true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $path; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function buildView($type, $path) |
120
|
|
|
{ |
121
|
|
|
$name = Str::studly(class_basename($this->argument('name'))); |
|
|
|
|
122
|
|
|
$this->fileName = $type; |
123
|
|
|
$stub = $this->files->get($this->getStub()); |
124
|
|
|
$viewLabel = Str::plural(str_to_words($name), 2); |
125
|
|
|
$viewName = Str::camel($viewLabel); |
|
|
|
|
126
|
|
|
$stub = $this->replacePlaceholders($stub, $name, $path); |
127
|
|
|
|
128
|
|
|
$target = $path . '/' . $type . '.blade.php'; |
129
|
|
|
if($type=='delete'){ |
130
|
|
|
$target = $path . '/modals/' . $type . '.blade.php'; |
131
|
|
|
} |
132
|
|
|
$displayPath = str_replace(resource_path(), '/resources', $target); |
133
|
|
|
if (file_exists($target) && !$this->option('force')) { |
134
|
|
|
$this->error("File already exists. Cannot overwrite {$displayPath}."); |
135
|
|
|
} else { |
136
|
|
|
file_put_contents($target, $stub); |
137
|
|
|
$this->info("View created successfully in {$displayPath}"); |
138
|
|
|
} |
139
|
|
|
if ($type == 'create' || $type == 'edit') { |
140
|
|
|
/** |
141
|
|
|
* Create the _form partial form the stub |
142
|
|
|
*/ |
143
|
|
|
$formPartial = $path . '/_form.blade.php'; |
144
|
|
|
$formPartialDisplayPath = str_replace(resource_path(), '/resources', $formPartial); |
145
|
|
|
$formStub = $this->files->get($this->getStub('_form')); |
146
|
|
|
|
147
|
|
|
if (file_exists($formPartial) && !$this->option('force')) { |
148
|
|
|
// $this->error("File already exists. Cannot overwrite {$formPartialDisplayPath}."); |
149
|
|
|
} else { |
150
|
|
|
file_put_contents($formPartial, $formStub); |
151
|
|
|
$this->info("View created successfully in {$formPartialDisplayPath}"); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get the stub file for the generator. |
158
|
|
|
* |
159
|
|
|
* @param null|string $fileName |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
protected function getStub($fileName = null) |
163
|
|
|
{ |
164
|
|
|
if (isset($fileName)) { |
165
|
|
|
$this->fileName = $fileName; |
166
|
|
|
} |
167
|
|
|
$stubsPath = "stubs/view/{$this->fileName}.stub"; |
168
|
|
|
$stubs = $this->option('stubs'); |
169
|
|
|
|
170
|
|
|
if ($stubs) { |
171
|
|
|
$stubsPath = $stubs . '/' . $this->fileName . ".stub"; |
172
|
|
|
} |
173
|
|
|
return resource_path($stubsPath); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Replace all placeholders |
178
|
|
|
* |
179
|
|
|
* @param $stub |
180
|
|
|
* @param $name |
181
|
|
|
* @param null $path |
182
|
|
|
* |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
|
|
protected function replacePlaceholders($stub, $name, $path = null) |
186
|
|
|
{ |
187
|
|
|
$viewDir = str_replace(resource_path('views/'), '', $path); |
188
|
|
|
$viewDir = str_replace('/', '.', $viewDir); |
189
|
|
|
|
190
|
|
|
$modelSlug = Str::slug(Str::plural(str_to_words($name), 2)); |
191
|
|
|
|
192
|
|
|
$viewLabel = str_to_words($name); |
193
|
|
|
$viewLabelPlural = Str::plural(str_to_words($name)); |
194
|
|
|
$viewName = Str::camel($name); |
195
|
|
|
|
196
|
|
|
$replace = array_merge([], [ |
197
|
|
|
'$label$' => $viewLabel, |
198
|
|
|
'$labelPlural$' => $viewLabelPlural, |
199
|
|
|
'$name$' => $viewName, |
200
|
|
|
'$modelSlug$' => $modelSlug, |
201
|
|
|
'$model$' => $name, |
202
|
|
|
'$rows$' => '$' . Str::camel(Str::plural($name, 2)), |
203
|
|
|
'$row$' => '$' . Str::camel(Str::singular($name)), |
204
|
|
|
'$routeBase$' => $viewDir, |
205
|
|
|
'$viewDir$' => $viewDir, |
206
|
|
|
]); |
207
|
|
|
|
208
|
|
|
return str_replace( |
209
|
|
|
array_keys($replace), array_values($replace), $stub |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
protected function createDeleteView($path) |
214
|
|
|
{ |
215
|
|
|
if (!file_exists($path . '/modals')) { |
216
|
|
|
mkdir($path . '/modals'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$this->buildView('delete', $path); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the default namespace for the class. |
224
|
|
|
* |
225
|
|
|
* @param string $rootNamespace |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
protected function getDefaultNamespace($rootNamespace) |
230
|
|
|
{ |
231
|
|
|
return $rootNamespace; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
protected function getArguments() |
235
|
|
|
{ |
236
|
|
|
return [ |
237
|
|
|
['name', InputArgument::REQUIRED, 'The name of the model'], |
238
|
|
|
]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get the console command options. |
243
|
|
|
* |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
|
View Code Duplication |
protected function getOptions() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
return [ |
249
|
|
|
[ |
250
|
|
|
'all', |
251
|
|
|
'a', |
252
|
|
|
InputOption::VALUE_NONE, |
253
|
|
|
'Generate an index, create, and an edit view for the model', |
254
|
|
|
], |
255
|
|
|
|
256
|
|
|
['index', 'i', InputOption::VALUE_NONE, 'Create a only the index view for the model'], |
257
|
|
|
|
258
|
|
|
['create', 'c', InputOption::VALUE_NONE, 'Create only the create view for the model'], |
259
|
|
|
|
260
|
|
|
['edit', 'e', InputOption::VALUE_NONE, 'Create only the edit view for the model'], |
261
|
|
|
|
262
|
|
|
['show', 's', InputOption::VALUE_NONE, 'Create only the show view for the model'], |
263
|
|
|
|
264
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'Create the file even if the file already exists.'], |
265
|
|
|
|
266
|
|
|
['dir', 'd', InputOption::VALUE_OPTIONAL, 'Create the file inside this directory within the view.'], |
267
|
|
|
|
268
|
|
|
['stubs', 'b', InputOption::VALUE_OPTIONAL, 'Use stubs from the specified directory.'], |
269
|
|
|
]; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
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.