1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PWWEB\Artomator\Generators\Scaffold; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use PWWEB\Artomator\Common\CommandData; |
7
|
|
|
|
8
|
|
|
class RoutesGenerator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Command data instance. |
12
|
|
|
* |
13
|
|
|
* @var CommandData |
14
|
|
|
*/ |
15
|
|
|
private $commandData; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Path variable. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $path; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Route Contents. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $routeContents; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Route template. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $routesTemplate; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Routes array. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $routes; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param CommandData $commandData Command data passed in from above. |
49
|
|
|
*/ |
50
|
|
|
public function __construct(CommandData $commandData) |
51
|
|
|
{ |
52
|
|
|
$this->commandData = $commandData; |
53
|
|
|
$this->path = $commandData->config->pathRoutes; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Prepare the routes array. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function prepareRoutes() |
62
|
|
|
{ |
63
|
|
|
$fileName = $this->path.'.json'; |
64
|
|
|
|
65
|
|
|
if (file_exists($fileName) === true) { |
66
|
|
|
// Routes json exists: |
67
|
|
|
$fileRoutes = file_get_contents($fileName); |
68
|
|
|
$fileRoutes = json_decode($fileRoutes, true); |
69
|
|
|
} else { |
70
|
|
|
$fileRoutes = []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (empty($this->commandData->config->prefixes['route']) === true) { |
74
|
|
|
$new = [ |
75
|
|
|
'resources' => [$this->commandData->modelName => $this->commandData->modelName], |
76
|
|
|
'name' => strtolower($this->commandData->modelName), |
77
|
|
|
]; |
78
|
|
|
$routes = [ucfirst($this->commandData->modelName) => $new]; |
79
|
|
|
} else { |
80
|
|
|
$prefixes = explode('.', $this->commandData->config->prefixes['route']); |
81
|
|
|
$routes = []; |
82
|
|
|
foreach (array_reverse($prefixes) as $key => $prefix) { |
83
|
|
|
$new = [ |
84
|
|
|
'prefix' => $prefix, |
85
|
|
|
'name' => strtolower($prefix), |
86
|
|
|
]; |
87
|
|
|
if (0 === $key) { |
88
|
|
|
$new['resources'] = [$this->commandData->modelName => $this->commandData->modelName]; |
89
|
|
|
} else { |
90
|
|
|
$new['group'] = $routes; |
91
|
|
|
} |
92
|
|
|
$routes = [ucfirst($prefix) => $new]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
$fileRoutes = array_replace_recursive($fileRoutes, $routes); |
96
|
|
|
file_put_contents($fileName, json_encode($fileRoutes, JSON_PRETTY_PRINT)); |
97
|
|
|
$this->commandData->commandComment("\nRoute JSON File saved: "); |
98
|
|
|
$this->commandData->commandInfo($fileName); |
99
|
|
|
$this->routes = $this->buildText($fileRoutes); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Generator function. |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function generate() |
108
|
|
|
{ |
109
|
|
|
$this->prepareRoutes(); |
110
|
|
|
$this->routeContents = file_get_contents($this->path); |
111
|
|
|
if (1 !== preg_match('/\/\/ Artomator Routes Start(.*)\/\/ Artomator Routes Stop/sU', $this->routeContents)) { |
112
|
|
|
$this->routeContents .= "\n\n// Artomator Routes Start\n// Artomator Routes Stop"; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->routeContents = preg_replace('/(\/\/ Artomator Routes Start)(.*)(\/\/ Artomator Routes Stop)/sU', "$1\n".$this->routes.'$3', $this->routeContents); |
|
|
|
|
116
|
|
|
|
117
|
|
|
file_put_contents($this->path, $this->routeContents); |
118
|
|
|
$this->commandData->commandComment("\n".$this->commandData->config->mCamelPlural.' routes added.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Re-generator the routes function. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function regenerate() |
127
|
|
|
{ |
128
|
|
|
$fileName = $this->path.'.json'; |
129
|
|
|
|
130
|
|
|
if (file_exists($fileName) === true) { |
131
|
|
|
// Routes json exists: |
132
|
|
|
$fileRoutes = file_get_contents($fileName); |
133
|
|
|
$fileRoutes = json_decode($fileRoutes, true); |
134
|
|
|
} else { |
135
|
|
|
$fileRoutes = []; |
136
|
|
|
} |
137
|
|
|
$this->routes = $this->buildText($fileRoutes); |
|
|
|
|
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('/(\/\/ Artomator Routes Start)(.*)(\/\/ Artomator Routes Stop)/sU', "$1\n".$this->routes.'$3', $this->routeContents); |
|
|
|
|
144
|
|
|
|
145
|
|
|
file_put_contents($this->path, $this->routeContents); |
146
|
|
|
$this->commandData->commandComment("\nRoutes regenerated."); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Rollback function. |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function rollback() |
155
|
|
|
{ |
156
|
|
|
if (Str::contains($this->routeContents, $this->routesTemplate) === true) { |
157
|
|
|
$this->routeContents = str_replace($this->routesTemplate, '', $this->routeContents); |
158
|
|
|
file_put_contents($this->path, $this->routeContents); |
159
|
|
|
$this->commandData->commandComment('scaffold routes deleted'); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Template text builder function. |
165
|
|
|
* |
166
|
|
|
* @param array $routes Routes array to process |
167
|
|
|
* @param int $indent Indent counter |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
private function buildText(array $routes, int $indent = 0) |
172
|
|
|
{ |
173
|
|
|
$templateContent = ''; |
174
|
|
|
foreach ($routes as $route_key => $route) { |
175
|
|
|
$templateString = ''; |
176
|
|
|
$tabs = (isset($route['prefix']) === true) ? (($indent * 3) + 3) : 0; |
177
|
|
|
if (isset($route['custom']) === true) { |
178
|
|
|
foreach ($route['custom'] as $custom_key => $custom) { |
179
|
|
|
if (true === isset($custom['function']) && $custom['function'] !== '') { |
180
|
|
|
$custom['function'] = '@'.$custom['function']; |
181
|
|
|
} |
182
|
|
|
$vars = [ |
183
|
|
|
'$ITERATION_CUSTOM_METHOD$' => $custom['method'], |
184
|
|
|
'$ITERATION_CUSTOM_ENDPOINT$' => $custom['endpoint'], |
185
|
|
|
'$ITERATION_CUSTOM_CONTROLLER$' => $custom['controller'], |
186
|
|
|
'$ITERATION_CUSTOM_FUNCTION$' => $custom['function'], |
187
|
|
|
'$ITERATION_CUSTOM_NAME$' => $custom['name'], |
188
|
|
|
'$INDENT$' => infy_tabs($tabs), |
189
|
|
|
]; |
190
|
|
|
$templateString .= get_artomator_template('scaffold.routes.prefixed.custom'); |
191
|
|
|
$templateString = fill_template($vars, $templateString); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
if (isset($route['resources'])) { |
195
|
|
|
$tabs = (isset($route['prefix'])) ? (($indent * 3) + 3) : 0; |
196
|
|
|
foreach (array_keys($route['resources']) as $resource_key) { |
197
|
|
|
$vars = [ |
198
|
|
|
'$ITERATION_MODEL_NAME_PLURAL_CAMEL$' => Str::camel(Str::plural($resource_key)), |
199
|
|
|
'$ITERATION_MODEL_NAME$' => $resource_key, |
200
|
|
|
'$INDENT$' => infy_tabs($tabs), |
201
|
|
|
]; |
202
|
|
|
$templateString .= get_artomator_template('scaffold.routes.prefixed.route'); |
203
|
|
|
$templateString = fill_template($vars, $templateString); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
if ((isset($route['group'])) === true) { |
207
|
|
|
$templateString .= $this->buildText($route['group'], ($indent + 1)); |
|
|
|
|
208
|
|
|
} |
209
|
|
|
if ((isset($route['prefix'])) === true) { |
210
|
|
|
$vars = [ |
211
|
|
|
'$ITERATION_NAMESPACE_CAMEL$' => ucfirst($route_key), |
212
|
|
|
'$ITERATION_NAMESPACE_LOWER$' => strtolower($route_key), |
213
|
|
|
'$INDENT$' => infy_tabs($indent * 3), |
214
|
|
|
]; |
215
|
|
|
$templateString = get_artomator_template('scaffold.routes.prefixed.namespace').$templateString.get_artomator_template('scaffold.routes.prefixed.closure'); |
216
|
|
|
$templateString = fill_template($vars, $templateString); |
217
|
|
|
} |
218
|
|
|
$templateContent .= $templateString; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $templateContent; |
|
|
|
|
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.