1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Generator; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
use Recca0120\Generator\Fixers\UseSortFixer; |
9
|
|
|
|
10
|
|
|
class Generator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* $filesystem. |
14
|
|
|
* |
15
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
16
|
|
|
*/ |
17
|
|
|
protected $filesystem; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* $useSortFixer. |
21
|
|
|
* |
22
|
|
|
* @var \Recca0120\Generator\Fixers\UseSortFixer |
23
|
|
|
*/ |
24
|
|
|
protected $useSortFixer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* $attributes. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $attributes = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* __construct. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
37
|
|
|
* @param \Recca0120\Generator\Fixers\UseSortFixer $useSortFixer |
38
|
|
|
*/ |
39
|
9 |
|
public function __construct(Filesystem $filesystem = null, UseSortFixer $useSortFixer = null) |
40
|
|
|
{ |
41
|
9 |
|
$this->filesystem = $filesystem ?: new Filesystem(); |
42
|
9 |
|
$this->useSortFixer = $useSortFixer ?: new UseSortFixer(); |
43
|
9 |
|
$this->useSortFixer->setSortType(UseSortFixer::SORT_TYPE_LENGTH); |
44
|
9 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* parseAttribute. |
48
|
|
|
* |
49
|
|
|
* @param string $value |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
9 |
|
protected function parseAttribute($value) |
53
|
|
|
{ |
54
|
9 |
|
$alias = array_map('trim', explode(' as ', $value)); |
55
|
9 |
|
$className = $alias[0]; |
56
|
|
|
|
57
|
9 |
|
$dummyClass = class_basename(isset($alias[1]) === true ? $alias[1] : $className); |
58
|
9 |
|
$dummyNamespace = $this->getNamespace($className); |
59
|
|
|
|
60
|
9 |
|
$singular = Str::camel(Str::singular( |
61
|
9 |
|
preg_replace('/(Controller|Repository)$/', '', $dummyClass) |
62
|
9 |
|
)); |
63
|
9 |
|
$plural = Str::plural($singular); |
64
|
|
|
|
65
|
9 |
|
$dummyModel = $singular; |
66
|
9 |
|
$dummyRepository = $plural; |
67
|
9 |
|
$dummyCollection = $singular === $plural ? $singular.'Collection' : $plural; |
68
|
9 |
|
$dummyView = Str::snake($plural); |
69
|
9 |
|
$dummyRoute = Str::snake($plural); |
70
|
|
|
|
71
|
|
|
return [ |
72
|
9 |
|
'DummyNamespace' => $dummyNamespace, |
73
|
9 |
|
'DummyClass' => $dummyClass, |
74
|
9 |
|
'dummyModel' => $dummyModel, |
75
|
9 |
|
'dummyRepository' => $dummyRepository, |
76
|
9 |
|
'dummyCollection' => $dummyCollection, |
77
|
9 |
|
'dummyView' => $dummyView, |
78
|
9 |
|
'dummyRoute' => $dummyRoute, |
79
|
9 |
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* setFullBaseClass. |
84
|
|
|
* |
85
|
|
|
* @param string $value |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
7 |
|
public function setFullBaseClass($value) |
89
|
|
|
{ |
90
|
7 |
|
$attributes = $this->parseAttribute($value); |
91
|
7 |
|
$this->set('DummyFullBaseClass', $value) |
92
|
7 |
|
->set('DummyBaseClass', $attributes['DummyClass']); |
93
|
7 |
|
if ($this->get('DummyNamespace') === $this->getNamespace($value)) { |
94
|
2 |
|
$this->remove('DummyFullBaseClass'); |
95
|
2 |
|
} |
96
|
|
|
|
97
|
7 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* setFullRepositoryInterface. |
102
|
|
|
* |
103
|
|
|
* @param string $value |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
3 |
View Code Duplication |
public function setFullRepositoryInterface($value) |
107
|
|
|
{ |
108
|
3 |
|
$attributes = $this->parseAttribute($value); |
109
|
|
|
|
110
|
3 |
|
return $this->set('DummyFullRepositoryInterface', $value) |
111
|
3 |
|
->set('DummyNamespace', $attributes['DummyNamespace'], false) |
112
|
3 |
|
->set('DummyClass', $attributes['DummyClass'], false) |
113
|
3 |
|
->set('DummyRepositoryInterface', $attributes['DummyClass'], false); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* setFullRepositoryClass. |
118
|
|
|
* |
119
|
|
|
* @param string $value |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
3 |
|
public function setFullRepositoryClass($value) |
123
|
|
|
{ |
124
|
3 |
|
$attributes = $this->parseAttribute($value); |
125
|
|
|
|
126
|
3 |
|
return $this->set('DummyFullRepositoryClass', $value) |
127
|
3 |
|
->set('DummyNamespace', $attributes['DummyNamespace'], false) |
128
|
3 |
|
->set('DummyClass', $attributes['DummyClass'], false) |
129
|
3 |
|
->set('DummyFullRepositoryInterface', $attributes['DummyNamespace'].'\Contracts\\'.$attributes['DummyClass'], false) |
130
|
3 |
|
->set('DummyRepositoryClass', $attributes['DummyClass']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* setFullModelClass. |
135
|
|
|
* |
136
|
|
|
* @param string $value |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
4 |
|
public function setFullModelClass($value) |
140
|
|
|
{ |
141
|
4 |
|
$attributes = $this->parseAttribute($value); |
142
|
|
|
|
143
|
4 |
|
return $this->set('DummyFullModelClass', $value) |
144
|
4 |
|
->set('DummyNamespace', $attributes['DummyNamespace'], false) |
145
|
4 |
|
->set('DummyClass', $attributes['DummyClass'], false) |
146
|
4 |
|
->set('dummyModel', $attributes['dummyModel'], false) |
147
|
4 |
|
->set('DummyFullPresenterClass', $attributes['DummyNamespace'].'\Presenters\\'.$attributes['DummyClass'].'Presenter', false) |
148
|
4 |
|
->set('DummyPresenterClass', $attributes['DummyClass'].'Presenter', false) |
149
|
4 |
|
->set('DummyModelClass', $attributes['DummyClass']); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* setFullPresenterClass. |
154
|
|
|
* |
155
|
|
|
* @param string $value |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
1 |
View Code Duplication |
public function setFullPresenterClass($value) |
159
|
|
|
{ |
160
|
1 |
|
$attributes = $this->parseAttribute($value); |
161
|
|
|
|
162
|
1 |
|
return $this->set('DummyFullPresenterClass', $value) |
163
|
1 |
|
->set('DummyNamespace', $attributes['DummyNamespace'], false) |
164
|
1 |
|
->set('DummyClass', $attributes['DummyClass'], false) |
165
|
1 |
|
->set('DummyPresenterClass', $attributes['DummyClass']); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* setFullRequestClass. |
170
|
|
|
* |
171
|
|
|
* @param string $value |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
3 |
View Code Duplication |
public function setFullRequestClass($value) |
175
|
|
|
{ |
176
|
3 |
|
$attributes = $this->parseAttribute($value); |
177
|
|
|
|
178
|
3 |
|
return $this->set('DummyFullRequestClass', $value) |
179
|
3 |
|
->set('DummyNamespace', $attributes['DummyNamespace'], false) |
180
|
3 |
|
->set('DummyClass', $attributes['DummyClass'], false) |
181
|
3 |
|
->set('DummyRequestClass', $attributes['DummyClass']); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* setFullControllerClass. |
186
|
|
|
* |
187
|
|
|
* @param string $value |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
2 |
|
public function setFullControllerClass($value) |
191
|
|
|
{ |
192
|
2 |
|
$attributes = $this->parseAttribute($value); |
193
|
|
|
|
194
|
2 |
|
return $this->set('DummyFullControllerClass', $value) |
195
|
2 |
|
->set('DummyNamespace', $attributes['DummyNamespace'], false) |
196
|
2 |
|
->set('DummyClass', $attributes['DummyClass'], false) |
197
|
2 |
|
->set('dummyRepository', $attributes['dummyRepository'], false) |
198
|
2 |
|
->set('dummyCollection', $attributes['dummyCollection'], false) |
199
|
2 |
|
->set('dummyModel', $attributes['dummyModel'], false) |
200
|
2 |
|
->set('dummyView', $attributes['dummyView'], false) |
201
|
2 |
|
->set('dummyRoute', $attributes['dummyRoute'], false) |
202
|
2 |
|
->set('DummyControllerClass', $attributes['DummyClass']); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* set. |
207
|
|
|
* |
208
|
|
|
* @param string $value |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
9 |
|
public function set($key, $value = null, $replace = true) |
212
|
|
|
{ |
213
|
9 |
|
if ($replace === false && isset($this->attributes[$key]) === true) { |
214
|
5 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
9 |
|
$this->attributes[$key] = $value; |
218
|
|
|
|
219
|
9 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* get. |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
7 |
|
public function get($key) |
228
|
|
|
{ |
229
|
7 |
|
return Arr::get($this->attributes, $key); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* remove. |
234
|
|
|
* |
235
|
|
|
* @param string $key |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
2 |
|
public function remove($key) |
239
|
|
|
{ |
240
|
2 |
|
return Arr::forget($this->attributes, $key); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* render. |
245
|
|
|
* |
246
|
|
|
* @param string $stub |
247
|
|
|
* @param bool $orderedUse |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
8 |
|
public function render($stub, $orderedUse = true) |
251
|
|
|
{ |
252
|
8 |
|
$content = strtr( |
253
|
8 |
|
strtr($this->filesystem->get($stub), $this->attributes), [ |
254
|
8 |
|
' extends DummyBaseClass' => '', |
255
|
8 |
|
'use DummyFullBaseClass;' => '', |
256
|
8 |
|
]); |
257
|
|
|
|
258
|
8 |
|
return $this->format($content, $orderedUse); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* renderServiceProvider. |
263
|
|
|
* |
264
|
|
|
* @param string $content |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
1 |
|
public function renderServiceProvider($content) |
268
|
|
|
{ |
269
|
1 |
|
if (strpos($content, '$this->registerRepositories') === false) { |
270
|
1 |
|
$content = preg_replace_callback('/public function register\(.+\n\s+{/', function ($m) { |
271
|
1 |
|
return $m[0]."\n". |
272
|
1 |
|
str_repeat(' ', 8). |
273
|
1 |
|
'$this->registerRepositories();'; |
274
|
1 |
|
}, $content); |
275
|
1 |
|
} |
276
|
|
|
|
277
|
1 |
|
if (strpos($content, 'protected function registerRepositories()') === false) { |
278
|
1 |
|
$content = substr($content, 0, strrpos($content, '}')). |
279
|
1 |
|
"\n".str_repeat(' ', 4). |
280
|
1 |
|
'protected function registerRepositories()'. |
281
|
1 |
|
"\n".str_repeat(' ', 4).'{'. |
282
|
1 |
|
"\n".str_repeat(' ', 4).'}'. |
283
|
1 |
|
"\n}\n"; |
284
|
1 |
|
} |
285
|
|
|
|
286
|
1 |
View Code Duplication |
if (strpos($content, sprintf('use %s;', $this->get('DummyFullRepositoryClass'))) === false) { |
287
|
1 |
|
$content = preg_replace_callback( |
288
|
1 |
|
'/namespace.+/', |
289
|
1 |
|
[$this, 'replaceServieProviderCallback'], |
290
|
|
|
$content |
291
|
1 |
|
); |
292
|
1 |
|
} |
293
|
|
|
|
294
|
1 |
View Code Duplication |
if (strpos($content, sprintf('$this->app->singleton(%sContract::class, %s::class);', $this->get('DummyClass'), $this->get('DummyClass'))) === false) { |
295
|
1 |
|
$content = preg_replace_callback( |
296
|
1 |
|
'/protected function registerRepositories.+\n\s+{/', |
297
|
1 |
|
[$this, 'replaceServieProviderCallback'], |
298
|
|
|
$content |
299
|
1 |
|
); |
300
|
1 |
|
} |
301
|
|
|
|
302
|
1 |
|
return $this->format($content); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* replaceServieProviderCallback. |
307
|
|
|
* |
308
|
|
|
* @param array $match |
309
|
|
|
* @return string |
310
|
|
|
*/ |
311
|
1 |
|
protected function replaceServieProviderCallback($match) |
312
|
|
|
{ |
313
|
1 |
|
$fullRepositoryClass = $this->get('DummyFullRepositoryClass'); |
314
|
1 |
|
$fullRepositoryInterface = $this->get('DummyFullRepositoryInterface'); |
315
|
1 |
|
$DummyClass = $this->get('DummyClass'); |
316
|
|
|
|
317
|
1 |
|
if (Str::startsWith($match[0], 'namespace') === true) { |
318
|
1 |
|
return $match[0]."\n\n". |
319
|
1 |
|
sprintf("use %s as %sContract;\n", $fullRepositoryInterface, $DummyClass). |
320
|
1 |
|
sprintf("use %s;\n", $fullRepositoryClass); |
321
|
|
|
} else { |
322
|
1 |
|
return $match[0]."\n". |
323
|
1 |
|
str_repeat(' ', 8). |
324
|
1 |
|
sprintf('$this->app->singleton(%sContract::class, %s::class);', $DummyClass, $DummyClass); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* getNamespace. |
330
|
|
|
* |
331
|
|
|
* @param string $name |
332
|
|
|
* @return string |
333
|
|
|
*/ |
334
|
9 |
|
protected function getNamespace($name) |
335
|
|
|
{ |
336
|
9 |
|
return rtrim(preg_replace('/'.class_basename($name).'$/', '', $name), '\\'); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* format. |
341
|
|
|
* |
342
|
|
|
* @param string $content |
343
|
|
|
* @param bool $orderedUse |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
9 |
|
protected function format($content, $orderedUse = true) |
347
|
|
|
{ |
348
|
9 |
|
if ($orderedUse === true && ($ordered = $this->useSortFixer->fix($content)) !== false) { |
349
|
7 |
|
$content = $ordered; |
350
|
7 |
|
} |
351
|
|
|
|
352
|
9 |
|
return strtr($content, ["\r\n" => "\n"]); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|