1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators; |
4
|
|
|
|
5
|
|
|
use Milkmeowo\Framework\Repository\Generators\Migrations\SchemaParser; |
6
|
|
|
|
7
|
|
|
class RepositoryEloquentGenerator extends Generator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Get stub name. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $stub = 'repository/eloquent'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get root namespace. |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public function getRootNamespace() |
22
|
|
|
{ |
23
|
|
|
return parent::getRootNamespace().parent::getConfigGeneratorClassPath($this->getPathConfigNode()); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get generator path config node. |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getPathConfigNode() |
32
|
|
|
{ |
33
|
|
|
return 'repositories'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get destination path for generated file. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getPath() |
42
|
|
|
{ |
43
|
|
|
return $this->getBasePath().'/'.parent::getConfigGeneratorClassPath($this->getPathConfigNode(), |
|
|
|
|
44
|
|
|
true).'/'.$this->getName().'RepositoryEloquent.php'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get base path of destination file. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getBasePath() |
53
|
|
|
{ |
54
|
|
|
return config('repository.generator.basePath', app()->path()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get array replacements. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getReplacements() |
63
|
|
|
{ |
64
|
|
|
$repository = parent::getRootNamespace().parent::getConfigGeneratorClassPath('interfaces').'\\'.$this->name.'Repository;'; |
|
|
|
|
65
|
|
|
$repository = str_replace([ |
66
|
|
|
'\\', |
67
|
|
|
'/', |
68
|
|
|
], '\\', $repository); |
69
|
|
|
|
70
|
|
|
return array_merge(parent::getReplacements(), [ |
71
|
|
|
'fillable' => $this->getFillable(), |
72
|
|
|
'use_validator' => $this->getValidatorUse(), |
73
|
|
|
'validator' => $this->getValidatorMethod(), |
74
|
|
|
'use_presenter' => $this->getPresenterUse(), |
75
|
|
|
'presenter' => $this->getPresenterMethod(), |
76
|
|
|
'criteria' => $this->getCriteria(), |
77
|
|
|
'repository' => $repository, |
78
|
|
|
'model' => isset($this->options['model']) ? $this->options['model'] : '', |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the fillable attributes. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getFillable() |
88
|
|
|
{ |
89
|
|
|
if (! $this->fillable) { |
|
|
|
|
90
|
|
|
return '[]'; |
91
|
|
|
} |
92
|
|
|
$results = '['.PHP_EOL; |
93
|
|
|
|
94
|
|
|
foreach ($this->getSchemaParser()->toArray() as $column => $value) { |
95
|
|
|
$results .= "\t\t'{$column}',".PHP_EOL; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $results."\t".']'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get schema parser. |
103
|
|
|
* |
104
|
|
|
* @return SchemaParser |
105
|
|
|
*/ |
106
|
|
|
public function getSchemaParser() |
107
|
|
|
{ |
108
|
|
|
return new SchemaParser($this->fillable); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getValidatorUse() |
115
|
|
|
{ |
116
|
|
|
$validator = $this->getValidator(); |
117
|
|
|
|
118
|
|
|
return "use {$validator};"; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function getValidator() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$validatorGenerator = new ValidatorGenerator([ |
127
|
|
|
'name' => $this->name, |
|
|
|
|
128
|
|
|
'rules' => $this->rules, |
|
|
|
|
129
|
|
|
'force' => $this->force, |
|
|
|
|
130
|
|
|
]); |
131
|
|
|
|
132
|
|
|
$validator = $validatorGenerator->getRootNamespace().'\\'.$validatorGenerator->getName(); |
133
|
|
|
|
134
|
|
|
return str_replace([ |
135
|
|
|
'\\', |
136
|
|
|
'/', |
137
|
|
|
], '\\', $validator).'Validator'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function getValidatorMethod() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
if ($this->validator != 'yes') { |
|
|
|
|
146
|
|
|
return ''; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$class = $this->getClass(); |
150
|
|
|
|
151
|
|
|
return '/**'.PHP_EOL.' * Specify Validator class name.'.PHP_EOL.' *'.PHP_EOL.' * @return mixed'.PHP_EOL.' */'.PHP_EOL.' public function validator()'.PHP_EOL.' {'.PHP_EOL.' return '.$class.'Validator::class;'.PHP_EOL.' }'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getPresenterUse() |
158
|
|
|
{ |
159
|
|
|
$presenter = $this->getPresenter(); |
160
|
|
|
|
161
|
|
|
return "use {$presenter};"; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public function getPresenter() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$presenterGenerator = new PresenterGenerator([ |
170
|
|
|
'name' => $this->name, |
|
|
|
|
171
|
|
|
'rules' => $this->rules, |
|
|
|
|
172
|
|
|
'force' => $this->force, |
|
|
|
|
173
|
|
|
]); |
174
|
|
|
$presenter = $presenterGenerator->getRootNamespace().'\\'.$presenterGenerator->getName(); |
175
|
|
|
|
176
|
|
|
return str_replace([ |
177
|
|
|
'\\', |
178
|
|
|
'/', |
179
|
|
|
], '\\', $presenter).'Presenter'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function getPresenterMethod() |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
if ($this->presenter != 'yes') { |
|
|
|
|
188
|
|
|
return ''; |
189
|
|
|
} |
190
|
|
|
$class = $this->getClass(); |
191
|
|
|
|
192
|
|
|
return '/**'.PHP_EOL.' * Specify Presenter class name.'.PHP_EOL.' *'.PHP_EOL.' * @return mixed'.PHP_EOL.' */'.PHP_EOL.' public function presenter()'.PHP_EOL.' {'.PHP_EOL.' return '.$class.'Presenter::class;'.PHP_EOL.' }'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function getCriteria() |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$criteriaGenerator = new CriteriaGenerator([ |
201
|
|
|
'name' => $this->name, |
|
|
|
|
202
|
|
|
'force' => $this->force, |
|
|
|
|
203
|
|
|
]); |
204
|
|
|
|
205
|
|
|
$criteria = $criteriaGenerator->getRootNamespace().'\\'.$criteriaGenerator->getName(); |
206
|
|
|
|
207
|
|
|
return str_replace([ |
208
|
|
|
'\\', |
209
|
|
|
'/', |
210
|
|
|
], '\\', $criteria).'Criteria'; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
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.