1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larafast\Fastapi\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Larafast\Fastapi\Console\Contracts\GeneratorCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
class ResourceMakeCommand extends GeneratorCommand |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The console command name. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name = 'fastApi:resource'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Create a new form resource class'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The type of class being generated. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $type = 'Resource'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the stub file for the generator. |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
protected function getStub() |
38
|
|
|
{ |
39
|
|
|
return config('fastApi.stubs_dir') . '/resource.stub'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function buildClass($name) |
43
|
|
|
{ |
44
|
|
|
$replace = []; |
45
|
|
|
if ($model = $this->option('model')) { |
|
|
|
|
46
|
|
|
$model = Str::studly(class_basename($this->option('model'))); |
|
|
|
|
47
|
|
|
$slug = Str::slug(str_to_words($model), '_'); |
48
|
|
|
$replace['$modelSlug$'] = $slug; |
49
|
|
|
$replace['$modelTable$'] = Str::plural($slug, 2); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return str_replace( |
53
|
|
|
array_keys($replace), array_values($replace), parent::buildClass($name) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the default namespace for the class. |
59
|
|
|
* |
60
|
|
|
* @param string $rootNamespace |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
protected function getDefaultNamespace($rootNamespace) |
65
|
|
|
{ |
66
|
|
|
return $rootNamespace . '\Http\Resources'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the console command options. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
protected function getOptions() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
['model', 'm', InputOption::VALUE_REQUIRED, 'The given model.'], |
78
|
|
|
['type', 't', InputOption::VALUE_OPTIONAL, 'Type of resource. Values can be store or update'], |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|