1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Pingpong\Support\Stub; |
6
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
|
9
|
|
|
class ControllerCommand extends GeneratorCommand |
10
|
|
|
{ |
11
|
|
|
use ModuleCommandTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The name of argument being used. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $argumentName = 'controller'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command name. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $name = 'module:make-controller'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Generate new restful controller for the specified module.'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get controller name. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
16 |
|
public function getDestinationFilePath() |
40
|
|
|
{ |
41
|
16 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
42
|
|
|
|
43
|
16 |
|
$controllerPath = $this->laravel['modules']->config('paths.generator.controller'); |
44
|
|
|
|
45
|
16 |
|
return $path.$controllerPath.'/'.$this->getControllerName().'.php'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Stub |
50
|
|
|
*/ |
51
|
16 |
|
protected function getTemplateContents() |
52
|
|
|
{ |
53
|
16 |
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
54
|
|
|
|
55
|
16 |
|
return (new Stub('/controller.stub', [ |
56
|
16 |
|
'MODULENAME' => $module->getStudlyName(), |
57
|
16 |
|
'CONTROLLERNAME' => $this->getControllerName(), |
58
|
16 |
|
'NAMESPACE' => $module->getStudlyName(), |
59
|
16 |
|
'CLASS_NAMESPACE' => $this->getClassNamespace($module), |
60
|
16 |
|
'CLASS' => $this->getClass(), |
61
|
16 |
|
'LOWER_NAME' => $module->getLowerName(), |
62
|
16 |
|
'MODULE' => $this->getModuleName(), |
63
|
16 |
|
'NAME' => $this->getModuleName(), |
64
|
16 |
|
'STUDLY_NAME' => $module->getStudlyName(), |
65
|
16 |
|
'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'), |
66
|
16 |
|
]))->render(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the console command arguments. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
16 |
|
protected function getArguments() |
75
|
|
|
{ |
76
|
|
|
return array( |
77
|
16 |
|
array('controller', InputArgument::REQUIRED, 'The name of the controller class.'), |
78
|
16 |
|
array('module', InputArgument::OPTIONAL, 'The name of module will be used.'), |
79
|
16 |
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array|string |
84
|
|
|
*/ |
85
|
16 |
|
protected function getControllerName() |
86
|
|
|
{ |
87
|
16 |
|
$controller = studly_case($this->argument('controller')); |
|
|
|
|
88
|
|
|
|
89
|
16 |
|
if (!str_contains(strtolower($controller), 'controller')) { |
90
|
|
|
$controller = $controller.'Controller'; |
91
|
|
|
} |
92
|
|
|
|
93
|
16 |
|
return $controller; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get default namespace. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
16 |
|
public function getDefaultNamespace() |
102
|
|
|
{ |
103
|
16 |
|
return 'Http\Controllers'; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.