rawilk /
laravel-modules
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Rawilk\LaravelModules\Commands\Generators; |
||
| 4 | |||
| 5 | use Illuminate\Support\Str; |
||
| 6 | use Rawilk\LaravelModules\Support\Config\GenerateConfigReader; |
||
| 7 | use Rawilk\LaravelModules\Support\Stub; |
||
| 8 | use Rawilk\LaravelModules\Traits\ModuleCommands; |
||
| 9 | |||
| 10 | class ResourceMakeCommand extends GeneratorCommand |
||
| 11 | { |
||
| 12 | use ModuleCommands; |
||
| 13 | |||
| 14 | /** @var string */ |
||
| 15 | protected $argumentName = 'name'; |
||
| 16 | |||
| 17 | /** @var string */ |
||
| 18 | protected $signature = 'module:make-resource |
||
| 19 | {name : The name of the resource class} |
||
| 20 | {module? : The name of the module to create the resource for} |
||
| 21 | {--c|collection : Indicates a resource collection should be created}'; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | protected $description = 'Create a new resource class for the specified module.'; |
||
| 25 | |||
| 26 | protected function getDefaultNamespace(): string |
||
| 27 | { |
||
| 28 | /** @var \Rawilk\LaravelModules\Contracts\Repository $module */ |
||
| 29 | $module = $this->laravel['modules']; |
||
| 30 | |||
| 31 | return $module->config('paths.generator.resource.namespace') ?: $module->config('paths.generator.resource.path', 'Transformers'); |
||
| 32 | } |
||
| 33 | |||
| 34 | protected function getDestinationFilePath(): string |
||
| 35 | { |
||
| 36 | $path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
||
| 37 | |||
| 38 | $resourcePath = GenerateConfigReader::read('resource'); |
||
| 39 | |||
| 40 | return $path . $resourcePath->getPath() . '/' . $this->getFileName() . '.php'; |
||
| 41 | } |
||
| 42 | |||
| 43 | protected function getTemplateContents(): string |
||
| 44 | { |
||
| 45 | /** @var \Rawilk\LaravelModules\Module $module */ |
||
| 46 | $module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
||
| 47 | |||
| 48 | return (new Stub($this->getStubName(), [ |
||
| 49 | 'NAMESPACE' => $this->getClassNamespace($module), |
||
| 50 | 'CLASS' => $this->getClass(), |
||
| 51 | ]))->render(); |
||
| 52 | } |
||
| 53 | |||
| 54 | private function collection(): bool |
||
| 55 | { |
||
| 56 | return $this->option('collection') |
||
| 57 | || Str::endsWith($this->argument('name'), 'Collection'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 58 | } |
||
| 59 | |||
| 60 | private function getStubName(): string |
||
| 61 | { |
||
| 62 | if ($this->collection()) { |
||
| 63 | return '/resource-collection.stub'; |
||
| 64 | } |
||
| 65 | |||
| 66 | return '/resource.stub'; |
||
| 67 | } |
||
| 68 | } |
||
| 69 |