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