|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PWWEB\Artomator\Generators\Scaffold; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use PWWEB\Artomator\Common\CommandData; |
|
8
|
|
|
|
|
9
|
|
|
class RoutesGenerator |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Command data instance. |
|
13
|
|
|
* |
|
14
|
|
|
* @var CommandData |
|
15
|
|
|
*/ |
|
16
|
|
|
private $commandData; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Path variable. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $path; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Route Contents. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $routeContents; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Route template. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $routesTemplate; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Routes array. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $routes; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Clasess array. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $classes; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Classes array. |
|
55
|
|
|
* |
|
56
|
|
|
* @var string[] |
|
57
|
|
|
*/ |
|
58
|
|
|
private $classNames; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Constructor. |
|
62
|
|
|
* |
|
63
|
|
|
* @param CommandData $commandData Command data passed in from above. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(CommandData $commandData) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->commandData = $commandData; |
|
68
|
|
|
$this->path = $commandData->config->pathRoutes; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Prepare the routes array. |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public function prepareRoutes() |
|
77
|
|
|
{ |
|
78
|
|
|
$fileName = $this->path.'.json'; |
|
79
|
|
|
|
|
80
|
|
|
if (true === file_exists($fileName)) { |
|
81
|
|
|
// Routes json exists: |
|
82
|
|
|
$fileRoutes = file_get_contents($fileName); |
|
83
|
|
|
$fileRoutes = json_decode($fileRoutes, true); |
|
84
|
|
|
} else { |
|
85
|
|
|
$fileRoutes = []; |
|
86
|
|
|
} |
|
87
|
|
|
$nsControllerName = $this->commandData->config->nsController.'\\'.$this->commandData->modelName.'Controller'; |
|
88
|
|
|
if (true === empty($this->commandData->config->prefixes['route'])) { |
|
89
|
|
|
$new = [ |
|
90
|
|
|
'resources' => [ |
|
91
|
|
|
$this->commandData->modelName => [ |
|
92
|
|
|
'only' => '*', |
|
93
|
|
|
'controller' => $nsControllerName, |
|
94
|
|
|
'as' => $this->commandData->modelName.'Controller', |
|
95
|
|
|
], |
|
96
|
|
|
], |
|
97
|
|
|
'name' => strtolower($this->commandData->modelName), |
|
98
|
|
|
]; |
|
99
|
|
|
$routes = [ucfirst($this->commandData->modelName) => $new]; |
|
100
|
|
|
} else { |
|
101
|
|
|
$prefixes = explode('.', $this->commandData->config->prefixes['route']); |
|
102
|
|
|
$routes = []; |
|
103
|
|
|
foreach (array_reverse($prefixes) as $key => $prefix) { |
|
104
|
|
|
$new = [ |
|
105
|
|
|
'prefix' => $prefix, |
|
106
|
|
|
'name' => strtolower($prefix), |
|
107
|
|
|
]; |
|
108
|
|
|
if (0 === $key) { |
|
109
|
|
|
$new['resources'] = [ |
|
110
|
|
|
$this->commandData->modelName => [ |
|
111
|
|
|
'only' => '*', |
|
112
|
|
|
'controller' => $nsControllerName, |
|
113
|
|
|
'as' => $this->commandData->modelName.'Controller', |
|
114
|
|
|
], |
|
115
|
|
|
]; |
|
116
|
|
|
} else { |
|
117
|
|
|
$new['group'] = $routes; |
|
118
|
|
|
} |
|
119
|
|
|
$routes = [ucfirst($prefix) => $new]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
$fileRoutes = array_replace_recursive($fileRoutes, $routes); |
|
123
|
|
|
file_put_contents($fileName, json_encode($fileRoutes, JSON_PRETTY_PRINT)); |
|
124
|
|
|
$this->commandData->commandComment("\nRoute JSON File saved: "); |
|
125
|
|
|
$this->commandData->commandInfo($fileName); |
|
126
|
|
|
$this->routes = $this->buildText($fileRoutes); |
|
127
|
|
|
$this->classes = $this->buildClasses(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Generator function. |
|
132
|
|
|
* |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
|
|
public function generate() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->prepareRoutes(); |
|
138
|
|
|
$this->routeContents = file_get_contents($this->path); |
|
139
|
|
|
if (1 !== preg_match('/\/\/ Artomator Routes Start(.*)\/\/ Artomator Routes Stop/sU', $this->routeContents)) { |
|
140
|
|
|
$this->routeContents .= "\n\n// Artomator Routes Start\n// Artomator Routes Stop"; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$this->routeContents = preg_replace( |
|
144
|
|
|
'/(\/\/ Artomator Routes Start)(.*)(\/\/ Artomator Routes Stop)/sU', |
|
145
|
|
|
"$1\n".$this->routes.'$3', |
|
146
|
|
|
$this->routeContents |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
if (1 !== preg_match( |
|
150
|
|
|
'/\/\/ Artomator Class References Start(.*)\/\/ Artomator Class References Stop/sU', |
|
151
|
|
|
$this->routeContents |
|
152
|
|
|
) |
|
153
|
|
|
) { |
|
154
|
|
|
$this->routeContents = preg_replace( |
|
155
|
|
|
'/(<\?php)/sU', |
|
156
|
|
|
"<?php\n\n// Artomator Class References Start\n// Artomator Class References Stop", |
|
157
|
|
|
$this->routeContents |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$this->routeContents = preg_replace( |
|
162
|
|
|
'/(\/\/ Artomator Class References Start)(.*)(\/\/ Artomator Class References Stop)/sU', |
|
163
|
|
|
"$1\n".$this->classes.'$3', |
|
164
|
|
|
$this->routeContents |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
file_put_contents($this->path, $this->routeContents); |
|
168
|
|
|
$this->commandData->commandComment("\n".$this->commandData->config->mCamelPlural.' routes added.'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Re-generator the routes function. |
|
173
|
|
|
* |
|
174
|
|
|
* @return void |
|
175
|
|
|
*/ |
|
176
|
|
|
public function regenerate() |
|
177
|
|
|
{ |
|
178
|
|
|
$fileName = $this->path.'.json'; |
|
179
|
|
|
|
|
180
|
|
|
if (true === file_exists($fileName)) { |
|
181
|
|
|
// Routes json exists: |
|
182
|
|
|
$fileRoutes = file_get_contents($fileName); |
|
183
|
|
|
$fileRoutes = json_decode($fileRoutes, true); |
|
184
|
|
|
} else { |
|
185
|
|
|
$fileRoutes = []; |
|
186
|
|
|
} |
|
187
|
|
|
$this->routes = $this->buildText($fileRoutes); |
|
188
|
|
|
$this->classes = $this->buildClasses(); |
|
189
|
|
|
$this->routeContents = file_get_contents($this->path); |
|
190
|
|
|
if (1 !== preg_match('/\/\/ Artomator Routes Start(.*)\/\/ Artomator Routes Stop/sU', $this->routeContents)) { |
|
191
|
|
|
$this->routeContents .= "\n\n// Artomator Routes Start\n// Artomator Routes Stop"; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$this->routeContents = preg_replace( |
|
195
|
|
|
'/(\/\/ Artomator Routes Start)(.*)(\/\/ Artomator Routes Stop)/sU', |
|
196
|
|
|
"$1\n".$this->routes.'$3', |
|
197
|
|
|
$this->routeContents |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
|
|
if (1 !== preg_match( |
|
201
|
|
|
'/\/\/ Artomator Class References Start(.*)\/\/ Artomator Class References Stop/sU', |
|
202
|
|
|
$this->routeContents |
|
203
|
|
|
) |
|
204
|
|
|
) { |
|
205
|
|
|
$this->routeContents = preg_replace( |
|
206
|
|
|
'/(<\?php)/sU', |
|
207
|
|
|
"<?php\n\n// Artomator Class References Start\n// Artomator Class References Stop", |
|
208
|
|
|
$this->routeContents |
|
209
|
|
|
); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$this->routeContents = preg_replace( |
|
213
|
|
|
'/(\/\/ Artomator Class References Start)(.*)(\/\/ Artomator Class References Stop)/sU', |
|
214
|
|
|
"$1\n".$this->classes.'$3', |
|
215
|
|
|
$this->routeContents |
|
216
|
|
|
); |
|
217
|
|
|
|
|
218
|
|
|
file_put_contents($this->path, $this->routeContents); |
|
219
|
|
|
$this->commandData->commandComment("\nRoutes regenerated."); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Rollback function. |
|
224
|
|
|
* |
|
225
|
|
|
* @return void |
|
226
|
|
|
*/ |
|
227
|
|
|
public function rollback() |
|
228
|
|
|
{ |
|
229
|
|
|
if (true === Str::contains($this->routeContents, $this->routesTemplate)) { |
|
230
|
|
|
$this->routeContents = str_replace($this->routesTemplate, '', $this->routeContents); |
|
231
|
|
|
file_put_contents($this->path, $this->routeContents); |
|
232
|
|
|
$this->commandData->commandComment('scaffold routes deleted'); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Template text builder function. |
|
238
|
|
|
* |
|
239
|
|
|
* @param array $routes Routes array to process |
|
240
|
|
|
* @param int $indent Indent counter |
|
241
|
|
|
* @param string $parent Parent prefix for fallback route |
|
242
|
|
|
* |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
|
|
private function buildText(array $routes, int $indent = 0, string $parent = '') |
|
246
|
|
|
{ |
|
247
|
|
|
$templateContent = ''; |
|
248
|
|
|
foreach ($routes as $route_key => $route) { |
|
249
|
|
|
if ('' !== $parent) { |
|
250
|
|
|
$parent .= '.'; |
|
251
|
|
|
} |
|
252
|
|
|
$parent .= (true === isset($route['prefix'])) ? $route['prefix'] : ''; |
|
253
|
|
|
$templateString = ''; |
|
254
|
|
|
$tabs = (true === isset($route['prefix'])) ? (($indent * 3) + 3) : 0; |
|
255
|
|
|
if (true === isset($route['custom'])) { |
|
256
|
|
|
foreach ($route['custom'] as $custom) { |
|
257
|
|
|
$vars = [ |
|
258
|
|
|
'$ITERATION_CUSTOM_METHOD$' => $custom['method'], |
|
259
|
|
|
'$ITERATION_CUSTOM_ENDPOINT$' => $custom['endpoint'], |
|
260
|
|
|
'$ITERATION_CUSTOM_CONTROLLER$' => $custom['as'], |
|
261
|
|
|
'$ITERATION_CUSTOM_FUNCTION$' => $custom['function'], |
|
262
|
|
|
'$ITERATION_CUSTOM_NAME$' => $custom['name'], |
|
263
|
|
|
'$INDENT$' => infy_tabs($tabs), |
|
264
|
|
|
]; |
|
265
|
|
|
$this->classNames[$custom['controller']] = $custom['as']; |
|
266
|
|
|
$templateString .= get_artomator_template('scaffold.routes.prefixed.custom'); |
|
267
|
|
|
$templateString = fill_template($vars, $templateString); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
if (true === isset($route['resources'])) { |
|
271
|
|
|
$tabs = (true === isset($route['prefix'])) ? (($indent * 3) + 3) : 0; |
|
272
|
|
|
foreach ($route['resources'] as $resource_key => $resource) { |
|
273
|
|
|
if (true === isset($resource['only']) && '*' !== $resource['only']) { |
|
274
|
|
|
$only = '->only([\''.implode('\', \'', explode(',', $resource['only'])).'\'])'; |
|
275
|
|
|
} else { |
|
276
|
|
|
$only = ''; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
$this->classNames[$resource['controller']] = $resource['as']; |
|
280
|
|
|
|
|
281
|
|
|
$vars = [ |
|
282
|
|
|
'$ITERATION_MODEL_NAME_PLURAL_CAMEL$' => Str::camel(Str::plural($resource_key)), |
|
283
|
|
|
'$ITERATION_CONTROLLER_NAME_AS$' => $resource['as'], |
|
284
|
|
|
'$ITERATION_ONLY$' => $only, |
|
285
|
|
|
'$INDENT$' => infy_tabs($tabs), |
|
286
|
|
|
]; |
|
287
|
|
|
$templateString .= get_artomator_template('scaffold.routes.prefixed.route'); |
|
288
|
|
|
$templateString = fill_template($vars, $templateString); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
if (true === (isset($route['group']))) { |
|
292
|
|
|
$templateString .= $this->buildText($route['group'], ($indent + 1), $parent); |
|
293
|
|
|
} |
|
294
|
|
|
if (true === (isset($route['prefix']))) { |
|
295
|
|
|
if (true === isset($route['fallback'])) { |
|
296
|
|
|
$fallback = $route['fallback']; |
|
297
|
|
|
} else { |
|
298
|
|
|
$fallback = ''; |
|
299
|
|
|
} |
|
300
|
|
|
$vars = [ |
|
301
|
|
|
'$ITERATION_NAMESPACE_CAMEL$' => ucfirst($route_key), |
|
302
|
|
|
'$ITERATION_NAMESPACE_LOWER$' => strtolower($route_key), |
|
303
|
|
|
'$FALLBACK_ROUTE$' => $fallback, |
|
304
|
|
|
'$INDENT$' => infy_tabs($indent * 3), |
|
305
|
|
|
]; |
|
306
|
|
|
if ('' !== $fallback) { |
|
307
|
|
|
$fallback_tmp = get_artomator_template('scaffold.routes.prefixed.fallback'); |
|
308
|
|
|
} else { |
|
309
|
|
|
$fallback_tmp = ''; |
|
310
|
|
|
} |
|
311
|
|
|
$templateString = get_artomator_template('scaffold.routes.prefixed.namespace') |
|
312
|
|
|
.$templateString |
|
313
|
|
|
.$fallback_tmp |
|
314
|
|
|
.get_artomator_template('scaffold.routes.prefixed.closure'); |
|
315
|
|
|
|
|
316
|
|
|
$templateString = fill_template($vars, $templateString); |
|
317
|
|
|
} |
|
318
|
|
|
$templateContent .= $templateString; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
return $templateContent; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Build the class names to be used in the refences. |
|
326
|
|
|
* |
|
327
|
|
|
* @return string The string of classnames in the form. |
|
328
|
|
|
*/ |
|
329
|
|
|
private function buildClasses() |
|
330
|
|
|
{ |
|
331
|
|
|
// $this->classNames = Arr::sort($this->classNames); |
|
332
|
|
|
|
|
333
|
|
|
$classContent = ''; |
|
334
|
|
|
foreach ($this->classNames as $controller => $as) { |
|
335
|
|
|
if ('' !== $as) { |
|
336
|
|
|
$as = ' as '.$as; |
|
337
|
|
|
} |
|
338
|
|
|
$var = [ |
|
339
|
|
|
'$ITERATION_NAMESPACE_CONTROLLER_NAME$' => $controller, |
|
340
|
|
|
'$ITERATION_NAMESPACE_CONTROLLER_AS$' => $as, |
|
341
|
|
|
]; |
|
342
|
|
|
|
|
343
|
|
|
$classContent .= get_artomator_template('scaffold.routes.prefixed.reference'); |
|
344
|
|
|
|
|
345
|
|
|
$classContent = fill_template($var, $classContent); |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
return $classContent; |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|