1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelPlus\Extension\Generators\Commands; |
4
|
|
|
|
5
|
|
|
use Jumilla\Generators\Laravel\OneFileGeneratorCommand as BaseCommand; |
6
|
|
|
use Jumilla\Generators\FileGenerator; |
7
|
|
|
use LaravelPlus\Extension\Addons\Addon; |
8
|
|
|
use LaravelPlus\Extension\Generators\GeneratorCommandTrait; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
class ControllerMakeCommand extends BaseCommand |
12
|
|
|
{ |
13
|
|
|
use GeneratorCommandTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The console command singature. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'make:controller |
21
|
|
|
{name : The name of the class} |
22
|
|
|
{--a|addon= : The name of the addon} |
23
|
|
|
{--m|model= : Generate a resource controller for the given model} |
24
|
|
|
{--r|resource : Generate a resource controller class} |
25
|
|
|
'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = '[+] Create a new controller class'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The type of class being generated. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $type = 'Controller'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The constructor. |
43
|
|
|
*/ |
44
|
9 |
|
public function __construct() |
45
|
|
|
{ |
46
|
9 |
|
parent::__construct(); |
47
|
|
|
|
48
|
9 |
|
$this->setStubDirectory(__DIR__.'/../stubs'); |
49
|
9 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the default namespace for the class. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
6 |
|
protected function getDefaultNamespace() |
57
|
|
|
{ |
58
|
6 |
|
return $this->getRootNamespace().'\\'.($this->onAddon() ? 'Controllers' : 'Http\\Controllers'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the stub file for the generator. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
6 |
|
protected function getStub() |
67
|
|
|
{ |
68
|
6 |
|
if ($this->option('model')) { |
69
|
2 |
|
return 'controller-model.stub'; |
70
|
|
|
} |
71
|
4 |
|
return $this->option('resource') ? 'controller-resource.stub' : 'controller-plain.stub'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Generate file. |
76
|
|
|
* |
77
|
|
|
* @param \Jumilla\Generators\FileGenerator $generator |
78
|
|
|
* @param string $path |
79
|
|
|
* @param string $fqcn |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
6 |
|
protected function generateFile(FileGenerator $generator, $path, $fqcn) |
84
|
|
|
{ |
85
|
6 |
|
list($namespace, $class) = $this->splitFullQualifyClassName($fqcn); |
86
|
6 |
|
list($model_namespace, $model_class) = $this->splitFullQualifyClassName($this->parseModel($this->option('model', ''))); |
|
|
|
|
87
|
|
|
|
88
|
6 |
|
return $generator->file($path)->template($this->getStub(), [ |
89
|
6 |
|
'namespace' => $namespace, |
90
|
6 |
|
'root_namespace' => $this->getAppNamespace(), // use App\Http\Controllers\Controller |
91
|
6 |
|
'class' => $class, |
92
|
6 |
|
'model_namespace' => $model_namespace ? $model_namespace.'\\' : '', |
93
|
6 |
|
'model_class' => $model_class, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the fully-qualified model class name. |
99
|
|
|
* |
100
|
|
|
* @param string $model |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
6 |
|
protected function parseModel($model) |
104
|
|
|
{ |
105
|
6 |
|
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
106
|
|
|
throw new InvalidArgumentException('Model name contains invalid characters.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
$model = trim(str_replace('/', '\\', $model), '\\'); |
110
|
|
|
|
111
|
6 |
|
return $model; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.