1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
7
|
|
|
use Nwidart\Modules\Support\Stub; |
8
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
|
12
|
|
|
class ResourceMakeCommand extends GeneratorCommand |
13
|
|
|
{ |
14
|
|
|
use ModuleCommandTrait; |
15
|
|
|
|
16
|
|
|
protected $argumentName = 'name'; |
17
|
|
|
protected $name = 'module:make-resource'; |
18
|
|
|
protected $description = 'Create a new resource class for the specified module.'; |
19
|
|
|
|
20
|
|
|
public function getDefaultNamespace() : string |
21
|
|
|
{ |
22
|
|
|
return $this->laravel['modules']->config('paths.generator.resource.path', 'Transformers'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get the console command arguments. |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
protected function getArguments() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
['name', InputArgument::REQUIRED, 'The name of the resource class.'], |
34
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function getOptions() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
['collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection.'], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
protected function getTemplateContents() |
49
|
|
|
{ |
50
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
51
|
|
|
|
52
|
|
|
return (new Stub($this->getStubName(), [ |
53
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
54
|
|
|
'CLASS' => $this->getClass(), |
55
|
|
|
]))->render(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
protected function getDestinationFilePath() |
62
|
|
|
{ |
63
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
64
|
|
|
|
65
|
|
|
$resourcePath = GenerateConfigReader::read('resource'); |
66
|
|
|
|
67
|
|
|
return $path . $resourcePath->getPath() . '/' . $this->getFileName() . '.php'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
private function getFileName() |
74
|
|
|
{ |
75
|
|
|
return Str::studly($this->argument('name')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Determine if the command is generating a resource collection. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
protected function collection() : bool |
84
|
|
|
{ |
85
|
|
|
return $this->option('collection') || |
86
|
|
|
Str::endsWith($this->argument('name'), 'Collection'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function getStubName(): string |
93
|
|
|
{ |
94
|
|
|
if ($this->collection()) { |
95
|
|
|
return '/resource-collection.stub'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return '/resource.stub'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|