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 ControllerMakeCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
use ModuleCommands; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $argumentName = 'controller'; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $signature = 'module:make-controller |
19
|
|
|
{controller : The name of the controller class} |
20
|
|
|
{module? : The name of the module to create the controller for} |
21
|
|
|
{--p|plain : Generate a plain controller} |
22
|
|
|
{--base_class= : Override the default base controller class (from config)}'; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $description = 'Generate a new controller for the specified module.'; |
26
|
|
|
|
27
|
|
|
protected function getDefaultNamespace(): string |
28
|
|
|
{ |
29
|
|
|
/** @var \Rawilk\LaravelModules\Contracts\Repository $module */ |
30
|
|
|
$module = $this->laravel['modules']; |
31
|
|
|
|
32
|
|
|
return $module->config('paths.generator.controller.namespace') ?: $module->config('paths.generator.controller.path', 'Http/Controllers'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function getDestinationFilePath(): string |
36
|
|
|
{ |
37
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
38
|
|
|
|
39
|
|
|
$controllerPath = GenerateConfigReader::read('controller'); |
40
|
|
|
|
41
|
|
|
return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function getTemplateContents(): string |
45
|
|
|
{ |
46
|
|
|
/** @var \Rawilk\LaravelModules\Module $module */ |
47
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
48
|
|
|
|
49
|
|
|
return (new Stub($this->getStubName(), [ |
50
|
|
|
'MODULENAME' => $module->getStudlyName(), |
51
|
|
|
'CONTROLLERNAME' => $this->getControllerName(), |
52
|
|
|
'NAMESPACE' => $module->getStudlyName(), |
53
|
|
|
'CLASS_NAMESPACE' => $this->getClassNamespace($module), |
54
|
|
|
'CLASS' => $this->getControllerNameWithoutNamespace(), |
55
|
|
|
'LOWER_NAME' => $module->getLowerName(), |
56
|
|
|
'MODULE' => $this->getModuleName(), |
57
|
|
|
'NAME' => $this->getModuleName(), |
58
|
|
|
'STUDLY_NAME' => $module->getStudlyName(), |
59
|
|
|
'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'), |
60
|
|
|
'BASE_CLASS' => $this->getBaseClass('controller'), |
61
|
|
|
'BASE_CLASS_SHORT' => $this->getBaseClass('controller', true), |
62
|
|
|
]))->render(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function getControllerName(): string |
66
|
|
|
{ |
67
|
|
|
$controller = Str::studly($this->argument('controller')); |
|
|
|
|
68
|
|
|
|
69
|
|
|
if (! Str::contains(strtolower($controller), 'controller')) { |
70
|
|
|
$controller .= 'Controller'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $controller; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function getControllerNameWithoutNamespace(): string |
77
|
|
|
{ |
78
|
|
|
return class_basename($this->getControllerName()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getStubName(): string |
82
|
|
|
{ |
83
|
|
|
if ($this->option('plain') === true) { |
|
|
|
|
84
|
|
|
return '/controller-plain.stub'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return '/controller.stub'; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|