|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Resta\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
|
|
|
|
|
|
6
|
|
|
use Resta\Exception\FileNotFoundException; |
|
7
|
|
|
|
|
8
|
|
|
class Generator |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var null|array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $classProperties; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var null|string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $data; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var null|string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $file; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var null|object |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $fileSystem; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var null|string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $path; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var null|string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $name; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var null|string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $namespace; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $type = 'class'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var null|string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $stubPath; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $accessibleProperties = array(); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $methodParameters = array(); |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Generator constructor. |
|
67
|
|
|
* @param null|string $path |
|
68
|
|
|
* @param null|string $name |
|
69
|
|
|
* @param null|object $fileSystem |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct($path=null,$name=null,$fileSystem=null) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->path = $path; |
|
74
|
|
|
|
|
75
|
|
|
$this->name = $name; |
|
76
|
|
|
|
|
77
|
|
|
$this->file = $this->path.''.DIRECTORY_SEPARATOR.''.ucfirst($this->name).'.php'; |
|
78
|
|
|
|
|
79
|
|
|
$this->fileSystem = (is_null($fileSystem)) ? files() : $fileSystem; |
|
80
|
|
|
|
|
81
|
|
|
$this->namespace = Utils::getNamespace($this->path); |
|
82
|
|
|
|
|
83
|
|
|
$this->setStubPath(); |
|
84
|
|
|
|
|
85
|
|
|
$this->createPath(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* creates class for generator |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $replacement |
|
92
|
|
|
* @return Generator |
|
93
|
|
|
* |
|
94
|
|
|
* @throws FileNotFoundException |
|
95
|
|
|
*/ |
|
96
|
|
|
public function createClass($replacement=array()) |
|
97
|
|
|
{ |
|
98
|
|
|
if(file_exists($this->path)){ |
|
99
|
|
|
|
|
100
|
|
|
$content = $this->fileSystem->get($this->getStubFile()); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
if($this->fileSystem->put($this->file,$content)!==FALSE){ |
|
103
|
|
|
$this->replaceFileContent($replacement,$this->file); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* creates class property for generator |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $properties |
|
114
|
|
|
* @param bool $loadedMethod |
|
115
|
|
|
* |
|
116
|
|
|
* @throws FileNotFoundException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function createClassProperty($properties=array(),$loadedMethod=false) |
|
119
|
|
|
{ |
|
120
|
|
|
if(is_null($this->classProperties)){ |
|
121
|
|
|
$this->classProperties = $properties; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if($loadedMethod){ |
|
125
|
|
|
|
|
126
|
|
|
foreach ($this->classProperties as $property) { |
|
127
|
|
|
|
|
128
|
|
|
if(preg_match('@class\s.*\n{@',$this->fileSystem->get($this->file),$parse)){ |
|
129
|
|
|
$this->replaceFileContent([ |
|
130
|
|
|
$parse[0] => $parse[0].' |
|
131
|
|
|
'.$property.' |
|
132
|
|
|
' |
|
133
|
|
|
|
|
134
|
|
|
],$this->file,true); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* creates method for generator |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $methods |
|
146
|
|
|
* |
|
147
|
|
|
* @throws FileNotFoundException |
|
148
|
|
|
*/ |
|
149
|
|
|
public function createMethod($methods=array()) |
|
150
|
|
|
{ |
|
151
|
|
|
$list = []; |
|
152
|
|
|
|
|
153
|
|
|
foreach ($methods as $method){ |
|
154
|
|
|
|
|
155
|
|
|
$list[] = ' |
|
156
|
|
|
'.$this->getAccessibleMethodValue($method).' function '.$method.'('.$this->getMethodParameters($method).') |
|
157
|
|
|
{ |
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
'; |
|
161
|
|
|
} |
|
162
|
|
|
if(preg_match('@class\s.*\n{@',$this->fileSystem->get($this->file),$parse)){ |
|
163
|
|
|
$this->replaceFileContent([ |
|
164
|
|
|
$parse[0] => $parse[0].' '.implode('',$list) |
|
165
|
|
|
],$this->file,true); |
|
166
|
|
|
|
|
167
|
|
|
$this->createClassProperty([],true); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* accessible properties method for generator |
|
174
|
|
|
* |
|
175
|
|
|
* @param array $methods |
|
176
|
|
|
* @return void|mixed |
|
177
|
|
|
*/ |
|
178
|
|
|
public function createMethodAccessibleProperty($methods=array()) |
|
179
|
|
|
{ |
|
180
|
|
|
foreach($methods as $method=>$accessibleValue) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->accessibleProperties[$method] = $accessibleValue; |
|
183
|
|
|
|
|
184
|
|
|
$this->replaceFileContent([ |
|
185
|
|
|
|
|
186
|
|
|
'public function '.$method.'' => ''.$this->getAccessibleMethodValue($method).' function '.$method.'' |
|
187
|
|
|
|
|
188
|
|
|
],$this->file,true); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* creates method for generator |
|
194
|
|
|
* |
|
195
|
|
|
* @param array $methods |
|
196
|
|
|
*/ |
|
197
|
|
|
public function createMethodBody($methods=array()) |
|
198
|
|
|
{ |
|
199
|
|
|
foreach ($methods as $method=>$body){ |
|
200
|
|
|
|
|
201
|
|
|
$this->replaceFileContent([ |
|
202
|
|
|
''.$this->getAccessibleMethodValue($method).' function '.$method.'\('.$this->getMethodParameters($method).'\)\n.*{\n.*\n.*}' => ''.$this->getAccessibleMethodValue($method).' function '.$method.'('.$this->getMethodParameters($method).') |
|
203
|
|
|
{ |
|
204
|
|
|
'.$body.' |
|
205
|
|
|
}' |
|
206
|
|
|
],$this->file,true); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* creates method for generator |
|
212
|
|
|
* |
|
213
|
|
|
* @param array $methods |
|
214
|
|
|
*/ |
|
215
|
|
|
public function createMethodDocument($methods=array()) |
|
216
|
|
|
{ |
|
217
|
|
|
foreach ($methods as $method=>$documents){ |
|
218
|
|
|
|
|
219
|
|
|
$documentString = []; |
|
220
|
|
|
$documentString[] = '/**'; |
|
221
|
|
|
|
|
222
|
|
|
foreach ($documents as $document){ |
|
223
|
|
|
$documentString[] = ' |
|
224
|
|
|
* '.$document.''; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$documentString[] = ' |
|
228
|
|
|
*/'; |
|
229
|
|
|
|
|
230
|
|
|
$this->replaceFileContent([ |
|
231
|
|
|
''.$this->getAccessibleMethodValue($method).' function '.$method.'\('.$this->getMethodParameters($method).'\)' => ''.implode('',$documentString).' |
|
232
|
|
|
'.$this->getAccessibleMethodValue($method).' function '.$method.'('.$this->getMethodParameters($method).')' |
|
233
|
|
|
],$this->file,true); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* accessible properties method for generator |
|
239
|
|
|
* |
|
240
|
|
|
* @param array $methods |
|
241
|
|
|
* @return void|mixed |
|
242
|
|
|
*/ |
|
243
|
|
|
public function createMethodParameters($methods=array()) |
|
244
|
|
|
{ |
|
245
|
|
|
foreach($methods as $method=>$parameter) |
|
246
|
|
|
{ |
|
247
|
|
|
$this->methodParameters[$method] = $parameter; |
|
248
|
|
|
|
|
249
|
|
|
$this->replaceFileContent([ |
|
250
|
|
|
|
|
251
|
|
|
''.$this->getAccessibleMethodValue($method).' function '.$method.'\(\)' => ''.$this->getAccessibleMethodValue($method).' function '.$method.'('.$parameter.')' |
|
252
|
|
|
|
|
253
|
|
|
],$this->file,true); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* creates directory for generator |
|
259
|
|
|
* |
|
260
|
|
|
* @return mixed|void |
|
261
|
|
|
*/ |
|
262
|
|
|
public function createPath() |
|
263
|
|
|
{ |
|
264
|
|
|
if(!file_exists($this->path)){ |
|
265
|
|
|
if(!$this->fileSystem->makeDirectory($this->path)){ |
|
266
|
|
|
throw new \Error($this->path.' makeDirectory fail'); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* get accessible method value for generator |
|
273
|
|
|
* |
|
274
|
|
|
* @param $method |
|
275
|
|
|
* @return mixed|string |
|
276
|
|
|
*/ |
|
277
|
|
|
private function getAccessibleMethodValue($method) |
|
278
|
|
|
{ |
|
279
|
|
|
return (isset($this->accessibleProperties[$method])) ? |
|
280
|
|
|
$this->accessibleProperties[$method] |
|
281
|
|
|
: 'public'; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* get parameters method value for generator |
|
286
|
|
|
* |
|
287
|
|
|
* @param $method |
|
288
|
|
|
* @return mixed|string |
|
289
|
|
|
*/ |
|
290
|
|
|
private function getMethodParameters($method) |
|
291
|
|
|
{ |
|
292
|
|
|
return (isset($this->methodParameters[$method])) ? |
|
293
|
|
|
str_replace('$','\$',$this->methodParameters[$method]) |
|
294
|
|
|
: ''; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* get stub file for generator |
|
299
|
|
|
* |
|
300
|
|
|
* @return string |
|
301
|
|
|
*/ |
|
302
|
|
|
public function getStubFile() |
|
303
|
|
|
{ |
|
304
|
|
|
$stubFile = $this->stubPath.''.DIRECTORY_SEPARATOR.''.$this->type.'.stub'; |
|
305
|
|
|
|
|
306
|
|
|
if(!file_exists($stubFile)){ |
|
307
|
|
|
throw new \Error($stubFile.' path is not available'); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
return $stubFile; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* replace with replacement variables content of the given file |
|
315
|
|
|
* |
|
316
|
|
|
* @param $replacement |
|
317
|
|
|
* @param $file |
|
318
|
|
|
* @param bool $default |
|
319
|
|
|
* @return void |
|
320
|
|
|
*/ |
|
321
|
|
|
private function replaceFileContent($replacement,$file,$default=false) |
|
322
|
|
|
{ |
|
323
|
|
|
$replacementVariables = ($default) ? $replacement : $this->replacementVariables($replacement); |
|
324
|
|
|
$content = $this->fileSystem->get($file); |
|
325
|
|
|
|
|
326
|
|
|
foreach ($replacementVariables as $key=>$replacementVariable){ |
|
327
|
|
|
|
|
328
|
|
|
if($default){ |
|
329
|
|
|
$search = '/'.$key.'/'; |
|
330
|
|
|
} |
|
331
|
|
|
else{ |
|
332
|
|
|
$search = '/__'.$key.'__/'; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
$replace = $replacementVariable; |
|
336
|
|
|
$content = preg_replace($search,$replace,$content); |
|
337
|
|
|
} |
|
338
|
|
|
$this->fileSystem->replace($file,$content); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* get replacement variables |
|
343
|
|
|
* |
|
344
|
|
|
* @param array $replacement |
|
345
|
|
|
* @return array |
|
346
|
|
|
*/ |
|
347
|
|
|
private function replacementVariables($replacement=array()) |
|
348
|
|
|
{ |
|
349
|
|
|
$replacement['namespace'] = $this->namespace; |
|
350
|
|
|
$replacement['class'] = $this->name; |
|
351
|
|
|
|
|
352
|
|
|
return array_map(function($item){ |
|
353
|
|
|
return ucfirst($item); |
|
354
|
|
|
},$replacement); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* set stub path |
|
359
|
|
|
* |
|
360
|
|
|
* @param $stubPath |
|
361
|
|
|
*/ |
|
362
|
|
|
public function setStubPath($stubPath=null) |
|
363
|
|
|
{ |
|
364
|
|
|
if(is_null($stubPath)){ |
|
365
|
|
|
$this->stubPath = app()->corePath().'Console/Stubs/generator'; |
|
366
|
|
|
} |
|
367
|
|
|
else{ |
|
368
|
|
|
$this->stubPath = $stubPath; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
} |
|
373
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths