1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consigliere\Components\Commands; |
4
|
|
|
|
5
|
|
|
use Consigliere\Components\Support\Stub; |
6
|
|
|
use Consigliere\Components\Traits\ComponentCommandTrait; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class MakeControllerCommand extends Command |
11
|
|
|
{ |
12
|
|
|
use ComponentCommandTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The name of argument being used. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $argumentName = 'controller'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name = 'component:make-controller'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Generate new restful controller for the specified component.'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get controller name. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getDestinationFilePath() |
41
|
|
|
{ |
42
|
|
|
$path = $this->laravel['components']->getComponentPath($this->getComponentName()); |
43
|
|
|
|
44
|
|
|
$controllerPath = $this->laravel['components']->config('paths.generator.controller'); |
45
|
|
|
|
46
|
|
|
return $path . $controllerPath . '/' . $this->getControllerName() . '.php'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
protected function getTemplateContents() |
53
|
|
|
{ |
54
|
|
|
$component = $this->laravel['components']->findOrFail($this->getComponentName()); |
55
|
|
|
|
56
|
|
|
return (new Stub($this->getStubName(), [ |
57
|
|
|
'COMPONENTNAME' => $component->getStudlyName(), |
58
|
|
|
'CONTROLLERNAME' => $this->getControllerName(), |
59
|
|
|
'NAMESPACE' => $component->getStudlyName(), |
60
|
|
|
'CLASS_NAMESPACE' => $this->getClassNamespace($component), |
61
|
|
|
'CLASS' => $this->getControllerName(), |
62
|
|
|
'LOWER_NAME' => $component->getLowerName(), |
63
|
|
|
'COMPONENT' => $this->getComponentName(), |
64
|
|
|
'NAME' => $this->getComponentName(), |
65
|
|
|
'STUDLY_NAME' => $component->getStudlyName(), |
66
|
|
|
'COMPONENT_NAMESPACE' => $this->laravel['components']->config('namespace'), |
67
|
|
|
]))->render(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the console command arguments. |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function getArguments() |
76
|
|
|
{ |
77
|
|
|
return [ |
78
|
|
|
['controller', InputArgument::REQUIRED, 'The name of the controller class.'], |
79
|
|
|
['component', InputArgument::OPTIONAL, 'The name of component will be used.'], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
protected function getOptions() |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain controller', null], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array|string |
95
|
|
|
*/ |
96
|
|
|
protected function getControllerName() |
97
|
|
|
{ |
98
|
|
|
$controller = studly_case($this->argument('controller')); |
|
|
|
|
99
|
|
|
|
100
|
|
|
if (str_contains(strtolower($controller), 'controller') === false) { |
101
|
|
|
$controller .= 'Controller'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $controller; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get default namespace. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getDefaultNamespace() |
113
|
|
|
{ |
114
|
|
|
return 'Http\Controllers'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the stub file name based on the plain option |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
private function getStubName() |
122
|
|
|
{ |
123
|
|
|
if ($this->option('plain') === true) { |
124
|
|
|
return '/controller-plain.stub'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return '/controller.stub'; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.