1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace JustSteveKing\Laravel\ApiToolkit\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class ResourceMakeCommand extends Command |
9
|
|
|
{ |
10
|
|
|
public $signature = 'api-toolkit:resource {name : The name of the Model you want to generate}'; |
11
|
|
|
|
12
|
|
|
public $description = 'Generate all the usual parts for an API Resource'; |
13
|
|
|
|
14
|
|
|
public function handle() |
15
|
|
|
{ |
16
|
|
|
if (! is_string($this->argument('name'))) { |
17
|
|
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$name = Str::studly($this->argument('name')); |
|
|
|
|
21
|
|
|
$this->comment("Generating boilerplate for {$name}"); |
22
|
|
|
|
23
|
|
|
$this->makeModel($name); |
24
|
|
|
$this->makeFactory($name); |
25
|
|
|
$this->makeSeeder($name); |
26
|
|
|
$this->makePolicy($name); |
27
|
|
|
$this->makeFormRequests($name); |
28
|
|
|
$this->makeApiResource($name); |
29
|
|
|
$this->makeControllers($name); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function makeModel(string $name): void |
33
|
|
|
{ |
34
|
|
|
$this->comment("Creating Model and Migration for {$name}"); |
35
|
|
|
$this->call("make:model", [ |
36
|
|
|
'name' => $name, |
37
|
|
|
'-m', |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function makeFactory(string $name): void |
42
|
|
|
{ |
43
|
|
|
$this->comment("Creating Factory for {$name}"); |
44
|
|
|
$this->call("make:factory", [ |
45
|
|
|
'name' => "{$name}Factory", |
46
|
|
|
'--model' => $name, |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function makeSeeder(string $name): void |
51
|
|
|
{ |
52
|
|
|
$this->comment("Creating Seeder for {$name}"); |
53
|
|
|
$this->call("make:seeder", [ |
54
|
|
|
'name' => sprintf(config('api-toolkit.seeder_name'), $name), |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function makePolicy(string $name): void |
59
|
|
|
{ |
60
|
|
|
$this->comment("Creating Model Policy for {$name}"); |
61
|
|
|
$this->call('make:policy', [ |
62
|
|
|
'name' => sprintf(config('api-toolkit.policy_name'), $name), |
63
|
|
|
'--model' => $name, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function makeFormRequests(string $name): void |
68
|
|
|
{ |
69
|
|
|
foreach (config('api-toolkit.form_requests') as $request) { |
70
|
|
|
$this->comment("Creating {$request} for model {$name}"); |
71
|
|
|
$this->call('make:request', [ |
72
|
|
|
'name' => "Api\\{$name}\\{$request}", |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function makeApiResource(string $name): void |
78
|
|
|
{ |
79
|
|
|
$this->comment("Creating API Resource for {$name}"); |
80
|
|
|
$this->call('make:resource', [ |
81
|
|
|
'name' => sprintf(config('api-toolkit.resource_name'), $name), |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function makeControllers(string $name): void |
86
|
|
|
{ |
87
|
|
|
foreach (config('api-toolkit.controllers') as $controller) { |
88
|
|
|
$this->comment("Creating {$controller['name']} for model {$name}"); |
89
|
|
|
$this->call('make:controller', [ |
90
|
|
|
'name' => "{$name}\\{$controller['name']}", |
91
|
|
|
implode(' ', $controller['options']), |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|