1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators; |
4
|
|
|
|
5
|
|
|
class ControllerGenerator extends Generator |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Get stub name. |
9
|
|
|
* |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
protected $stub = 'controller/controller'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Get root namespace. |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public function getRootNamespace() |
20
|
|
|
{ |
21
|
|
|
return str_replace('/', '\\', parent::getRootNamespace().parent::getConfigGeneratorClassPath($this->getPathConfigNode())); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get generator path config node. |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function getPathConfigNode() |
30
|
|
|
{ |
31
|
|
|
return 'controllers'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get destination path for generated file. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getPath() |
40
|
|
|
{ |
41
|
|
|
return $this->getBasePath().'/'.parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true).'/'.$this->getControllerName().'Controller.php'; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get base path of destination file. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getBasePath() |
50
|
|
|
{ |
51
|
|
|
return config('repository.generator.basePath', app()->path()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets controller name based on model. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getControllerName() |
60
|
|
|
{ |
61
|
|
|
return ucfirst($this->getPluralName()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets plural name based on model. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getPluralName() |
70
|
|
|
{ |
71
|
|
|
return str_plural(lcfirst(ucwords($this->getClass()))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get array replacements. |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getReplacements() |
80
|
|
|
{ |
81
|
|
|
return array_merge(parent::getReplacements(), [ |
82
|
|
|
'controller' => $this->getControllerName(), |
83
|
|
|
'plural' => $this->getPluralName(), |
84
|
|
|
'singular' => $this->getSingularName(), |
85
|
|
|
'validator' => $this->getValidator(), |
86
|
|
|
'repository' => $this->getRepository(), |
87
|
|
|
'appname' => $this->getAppNamespace(), |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Gets singular name based on model. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getSingularName() |
97
|
|
|
{ |
98
|
|
|
return str_singular(lcfirst(ucwords($this->getClass()))); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets validator full class name. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getValidator() |
107
|
|
|
{ |
108
|
|
|
$validatorGenerator = new ValidatorGenerator([ |
109
|
|
|
'name' => $this->name, |
|
|
|
|
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$validator = $validatorGenerator->getRootNamespace().'\\'.$validatorGenerator->getName(); |
113
|
|
|
|
114
|
|
|
return 'use '.str_replace([ |
115
|
|
|
'\\', |
116
|
|
|
'/', |
117
|
|
|
], '\\', $validator).'Validator;'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets repository full class name. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getRepository() |
126
|
|
|
{ |
127
|
|
|
$repositoryGenerator = new RepositoryInterfaceGenerator([ |
128
|
|
|
'name' => $this->name, |
|
|
|
|
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
$repository = $repositoryGenerator->getRootNamespace().'\\'.$repositoryGenerator->getName(); |
132
|
|
|
|
133
|
|
|
return 'use '.str_replace([ |
134
|
|
|
'\\', |
135
|
|
|
'/', |
136
|
|
|
], '\\', $repository).'Repository;'; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.