-
Notifications
You must be signed in to change notification settings - Fork 21
/
ScaffoldGenerator.php
403 lines (348 loc) · 9.59 KB
/
ScaffoldGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
namespace Pingpong\Generators;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Pingpong\Generators\FormDumpers\FieldsDumper;
use Pingpong\Generators\FormDumpers\TableDumper;
use Pingpong\Generators\Scaffolders\ControllerScaffolder;
class ScaffoldGenerator
{
/**
* The illuminate command instance.
*
* @var \Illuminate\Console\Command
*/
protected $console;
/**
* The laravel instance.
*
* @var \Illuminate\Foundation\Application
*/
protected $laravel;
/**
* The array of view names being created.
*
* @var array
*/
protected $views = ['index', 'edit', 'show', 'create', 'form'];
/**
* Indicates the migration has been migrated.
*
* @var bool
*/
protected $migrated = false;
/**
* The constructor.
*
* @param Command $console
*/
public function __construct(Command $console)
{
$this->console = $console;
$this->laravel = $console->getLaravel();
}
/**
* Get entity name.
*
* @return string
*/
public function getEntity()
{
return strtolower(str_singular($this->console->argument('entity')));
}
/**
* Get entities name.
*
* @return string
*/
public function getEntities()
{
return str_plural($this->getEntity());
}
/**
* Determine whether the creation is from an existing table.
*
* @return boolen
*/
public function existing()
{
return $this->console->option('existing');
}
/**
* Get schema fields.
*
* @return string
*/
public function getFields()
{
if ($this->existing()) {
return TableDumper::make($this->getEntities())->toSchema();
}
return $this->console->option('fields');
}
/**
* Get controller name.
*
* @return string
*/
public function getControllerName()
{
$controller = Str::studly($this->getEntities()).'Controller';
if ($this->console->option('prefix')) {
$controller = Str::studly($this->getPrefix('/')).$controller;
}
return str_replace('/', '\\', $controller);
}
/**
* Confirm a question with the user.
*
* @param string $message
*
* @return string
*/
public function confirm($message)
{
if ($this->console->option('no-question')) {
return true;
}
return $this->console->confirm($message);
}
/**
* Generate model.
*/
public function generateModel()
{
if (!$this->confirm('Do you want to create a model?')) {
return;
}
$this->console->call('generate:model', [
'name' => $this->getEntity(),
'--fillable' => $this->getFields(),
'--force' => $this->console->option('force'),
]);
}
/**
* Generate seed.
*/
public function generateSeed()
{
if (!$this->confirm('Do you want to create a database seeder class?')) {
return;
}
$this->console->call('generate:seed', [
'name' => $this->getEntities(),
'--force' => $this->console->option('force'),
]);
}
/**
* Generate migration.
*/
public function generateMigration()
{
if (!$this->confirm('Do you want to create a migration?')) {
return;
}
$existing = $this->existing();
$table = $this->getEntities();
$this->console->call('generate:migration', [
'name' => $existing ? $table : "create_{$table}_table",
'--fields' => $this->console->option('fields'),
'--force' => $this->console->option('force'),
'--existing' => $existing,
]);
}
/**
* Generate controller.
*/
public function generateController()
{
if (!$this->confirm('Do you want to generate a controller?')) {
return;
}
$this->console->call('generate:controller', [
'name' => $this->getControllerName(),
'--force' => $this->console->option('force'),
'--scaffold' => true,
]);
}
/**
* Get view layout.
*
* @return string
*/
public function getViewLayout()
{
return $this->getPrefix('/').'layouts/master';
}
/**
* Generate a view layout.
*/
public function generateViewLayout()
{
if ($this->confirm('Do you want to create master view?')) {
$this->console->call('generate:view', [
'name' => $this->getViewLayout(),
'--master' => true,
'--force' => $this->console->option('force'),
]);
}
}
/**
* Get controller scaffolder instance.
*
* @return string
*/
public function getControllerScaffolder()
{
return new ControllerScaffolder($this->getEntity(), $this->getPrefix());
}
/**
* Get form generator instance.
*
* @return string
*/
public function getFormGenerator()
{
return new FormGenerator($this->getEntities(), $this->getFields());
}
/**
* Get table dumper.
*
* @return mixed
*/
public function getTableDumper()
{
if ($this->migrated) {
return new TableDumper($this->getEntities());
}
if ($this->existing()) {
return TableDumper::make($this->getEntities())
->except(['id', 'created_at', 'updated_at', 'deleted_at']);
}
return new FieldsDumper($fields);
}
/**
* Generate views.
*/
public function generateViews()
{
$this->generateViewLayout();
if (!$this->confirm('Do you want to create view resources?')) {
return;
}
foreach ($this->views as $view) {
$this->generateView($view);
}
}
/**
* Generate a scaffold view.
*
* @param string $view
*/
public function generateView($view)
{
$generator = new ViewGenerator([
'name' => $this->getPrefix('/').$this->getEntities().'/'.$view,
'extends' => str_replace('/', '.', $this->getViewLayout()),
'template' => '/scaffold/views/'.$view.'.stub',
'force' => $this->console->option('force'),
]);
$generator->appendReplacement(array_merge($this->getControllerScaffolder()->toArray(), [
'lower_plural_entity' => strtolower($this->getEntities()),
'studly_singular_entity' => Str::studly($this->getEntity()),
'form' => $this->getFormGenerator()->render(),
'table_heading' => $this->getTableDumper()->toHeading(),
'table_body' => $this->getTableDumper()->toBody($this->getEntity()),
'show_body' => $this->getTableDumper()->toRows($this->getEntity()),
]));
$generator->run();
$this->console->info('View created successfully.');
}
/**
* Append new route.
*/
public function appendRoute()
{
if (!$this->confirm('Do you want to append new route?')) {
return;
}
$contents = $this->laravel['files']->get($path = app_path('Http/routes.php'));
$contents .= PHP_EOL."Route::group(['middleware' => ['web']], function () {";
$contents .= PHP_EOL."\tRoute::resource('{$this->getRouteName()}', '{$this->getControllerName()}');";
$contents .= PHP_EOL."});";
$this->laravel['files']->put($path, $contents);
$this->console->info('Route appended successfully.');
}
/**
* Get route name.
*
* @return string
*/
public function getRouteName()
{
$route = $this->getEntities();
if ($this->console->option('prefix')) {
$route = strtolower($this->getPrefix('/')).$route;
}
return $route;
}
/**
* Get prefix name.
*
* @param string|null $suffix
*
* @return string|null
*/
public function getPrefix($suffix = null)
{
$prefix = $this->console->option('prefix');
return $prefix ? $prefix.$suffix : null;
}
/**
* Run the migrations.
*/
public function runMigration()
{
if ($this->confirm('Do you want to run all migration now?')) {
$this->migrated = true;
$this->console->call('migrate', [
'--force' => $this->console->option('force'),
]);
}
}
/**
* Generate request classes.
*/
public function generateRequest()
{
if (!$this->confirm('Do you want to create form request classes?')) {
return;
}
foreach (['Create', 'Update'] as $request) {
$name = $this->getPrefix('/').$this->getEntities().'/'.$request.Str::studly($this->getEntity()).'Request';
$this->console->call('generate:request', [
'name' => $name,
'--scaffold' => true,
'--auth' => true,
'--rules' => $this->existing() ? $this->getTableDumper()->toSchema() : $this->getFields(),
'--force' => $this->console->option('force'),
]);
}
}
/**
* Run the generator.
*/
public function run()
{
$this->generateModel();
$this->generateMigration();
$this->generateSeed();
$this->generateRequest();
$this->generateController();
if (!$this->existing()) {
$this->runMigration();
}
$this->generateViews();
$this->appendRoute();
}
}