1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
6
|
|
|
use Nwidart\Modules\Support\Stub; |
7
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
|
11
|
|
|
class ControllerMakeCommand extends GeneratorCommand |
12
|
|
|
{ |
13
|
|
|
use ModuleCommandTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name of argument being used. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $argumentName = 'controller'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command name. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $name = 'module:make-controller'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Generate new restful controller for the specified module.'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get controller name. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
86 |
|
public function getDestinationFilePath() |
42
|
|
|
{ |
43
|
86 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
44
|
|
|
|
45
|
86 |
|
$controllerPath = GenerateConfigReader::read('controller'); |
46
|
|
|
|
47
|
86 |
|
return $path . $controllerPath->getPath() . '/' . $this->getControllerName() . '.php'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
86 |
|
protected function getTemplateContents() |
54
|
|
|
{ |
55
|
86 |
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
56
|
|
|
|
57
|
86 |
|
return (new Stub($this->getStubName(), [ |
58
|
86 |
|
'MODULENAME' => $module->getStudlyName(), |
59
|
86 |
|
'CONTROLLERNAME' => $this->getControllerName(), |
60
|
86 |
|
'NAMESPACE' => $module->getStudlyName(), |
61
|
86 |
|
'CLASS_NAMESPACE' => $this->getClassNamespace($module), |
62
|
86 |
|
'CLASS' => $this->getControllerNameWithoutNamespace(), |
63
|
86 |
|
'LOWER_NAME' => $module->getLowerName(), |
64
|
86 |
|
'MODULE' => $this->getModuleName(), |
65
|
86 |
|
'NAME' => $this->getModuleName(), |
66
|
86 |
|
'STUDLY_NAME' => $module->getStudlyName(), |
67
|
86 |
|
'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'), |
68
|
86 |
|
]))->render(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the console command arguments. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
94 |
|
protected function getArguments() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
94 |
|
['controller', InputArgument::REQUIRED, 'The name of the controller class.'], |
80
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
94 |
|
protected function getOptions() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
94 |
|
['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array|string |
96
|
|
|
*/ |
97
|
86 |
|
protected function getControllerName() |
98
|
|
|
{ |
99
|
86 |
|
$controller = studly_case($this->argument('controller')); |
100
|
|
|
|
101
|
86 |
|
if (str_contains(strtolower($controller), 'controller') === false) { |
102
|
2 |
|
$controller .= 'Controller'; |
103
|
|
|
} |
104
|
|
|
|
105
|
86 |
|
return $controller; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string|array $controllerName |
110
|
|
|
* |
111
|
|
|
* @return array|string |
112
|
|
|
*/ |
113
|
86 |
|
protected function sanitizeControllerName($controllerName) |
114
|
|
|
{ |
115
|
86 |
|
if (!\is_string($controllerName) || strpos($controllerName, '\\') === false) { |
116
|
86 |
|
return $controllerName; |
117
|
|
|
} |
118
|
2 |
|
$start = strrpos($controllerName, '\\') + 1; |
119
|
|
|
|
120
|
2 |
|
return substr($controllerName, $start); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return array|string |
125
|
|
|
*/ |
126
|
86 |
|
private function getControllerNameWithoutNamespace() |
127
|
|
|
{ |
128
|
86 |
|
return $this->sanitizeControllerName($this->getControllerName()); |
129
|
|
|
} |
130
|
|
|
|
131
|
86 |
|
public function getDefaultNamespace() : string |
132
|
|
|
{ |
133
|
86 |
|
return $this->laravel['modules']->config('paths.generator.controller.path', 'Http/Controllers'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the stub file name based on the plain option |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
86 |
|
private function getStubName() |
141
|
|
|
{ |
142
|
86 |
|
if ($this->option('plain') === true) { |
143
|
1 |
|
return '/controller-plain.stub'; |
144
|
|
|
} |
145
|
|
|
|
146
|
86 |
|
return '/controller.stub'; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|