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
|
|
|
* @var CommandData |
12
|
|
|
*/ |
13
|
|
|
private $commandData; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $path; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $routeContents; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $routesTemplate; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $routes; |
34
|
|
|
|
35
|
|
|
public function __construct(CommandData $commandData) |
36
|
|
|
{ |
37
|
|
|
$this->commandData = $commandData; |
38
|
|
|
$this->path = $commandData->config->pathRoutes; |
39
|
|
|
$this->prepareRoutes(); |
40
|
|
|
$this->routeContents = file_get_contents($this->path); |
41
|
|
|
if (false === preg_match('/\/\/ Artomator Routes Start(.*)\/\/ Artomator Routes Stop/sU', $this->routeContents)) { |
42
|
|
|
$this->routeContents .= "\n\n// Artomator Routes Start\n// Artomator Routes Stop"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->routeContents = preg_replace('/(\/\/ Artomator Routes Start)(.*)(\/\/ Artomator Routes Stop)/sU', "$1\n".$this->routes.'$3', $this->routeContents); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function prepareRoutes() |
49
|
|
|
{ |
50
|
|
|
$fileName = $this->path.'.json'; |
51
|
|
|
|
52
|
|
|
if (file_exists($fileName)) { |
53
|
|
|
// Routes json exists: |
54
|
|
|
$fileRoutes = file_get_contents($fileName); |
55
|
|
|
$fileRoutes = json_decode($fileRoutes, true); |
56
|
|
|
} else { |
57
|
|
|
$fileRoutes = []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (empty($this->commandData->config->prefixes['route'])) { |
61
|
|
|
$new = [ |
62
|
|
|
'resources' => [$this->commandData->modelName => $this->commandData->modelName], |
63
|
|
|
'name' => strtolower($this->commandData->modelName), |
64
|
|
|
]; |
65
|
|
|
$routes = [ucfirst($this->commandData->modelName) => $new]; |
66
|
|
|
} else { |
67
|
|
|
$prefixes = explode('.', $this->commandData->config->prefixes['route']); |
68
|
|
|
$routes = []; |
69
|
|
|
foreach (array_reverse($prefixes) as $key => $prefix) { |
70
|
|
|
$new = [ |
71
|
|
|
'prefix' => $prefix, |
72
|
|
|
'name' => strtolower($prefix), |
73
|
|
|
]; |
74
|
|
|
if (0 === $key) { |
75
|
|
|
$new['resources'] = [$this->commandData->modelName => $this->commandData->modelName]; |
76
|
|
|
} else { |
77
|
|
|
$new['group'] = $routes; |
78
|
|
|
} |
79
|
|
|
$routes = [ucfirst($prefix) => $new]; |
80
|
|
|
} |
81
|
|
|
}//end if |
82
|
|
|
$fileRoutes = array_replace_recursive($fileRoutes, $routes); |
83
|
|
|
file_put_contents($fileName, json_encode($fileRoutes, JSON_PRETTY_PRINT)); |
84
|
|
|
$this->commandData->commandComment("\nRoute JSON File saved: "); |
85
|
|
|
$this->commandData->commandInfo($fileName); |
86
|
|
|
$this->routes = $this->buildText($fileRoutes); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function generate() |
90
|
|
|
{ |
91
|
|
|
file_put_contents($this->path, $this->routeContents); |
92
|
|
|
$this->commandData->commandComment("\n".$this->commandData->config->mCamelPlural.' routes added.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function rollback() |
96
|
|
|
{ |
97
|
|
|
if (Str::contains($this->routeContents, $this->routesTemplate)) { |
98
|
|
|
$this->routeContents = str_replace($this->routesTemplate, '', $this->routeContents); |
99
|
|
|
file_put_contents($this->path, $this->routeContents); |
100
|
|
|
$this->commandData->commandComment('scaffold routes deleted'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function buildText($routes, $indent = 0) |
105
|
|
|
{ |
106
|
|
|
$templateContent = ''; |
107
|
|
|
foreach ($routes as $route_key => $route) { |
108
|
|
|
$templateString = ''; |
109
|
|
|
if (isset($route['resources'])) { |
110
|
|
|
$tabs = (isset($route['prefix'])) ? (($indent * 3) + 3) : 0; |
111
|
|
|
foreach ($route['resources'] as $resource_key => $resource) { |
112
|
|
|
$vars = [ |
113
|
|
|
'$ITERATION_MODEL_NAME_PLURAL_CAMEL$' => Str::camel(Str::plural($resource_key)), |
114
|
|
|
'$ITERATION_MODEL_NAME$' => $resource_key, |
115
|
|
|
'$INDENT$' => infy_tabs($tabs), |
116
|
|
|
]; |
117
|
|
|
$templateString .= get_artomator_template('scaffold.routes.prefixed.route'); |
118
|
|
|
$templateString = fill_template($vars, $templateString); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
if ((isset($route['group']))) { |
122
|
|
|
$templateString .= $this->buildText($route['group'], ($indent + 1)); |
123
|
|
|
} |
124
|
|
|
if ((isset($route['prefix']))) { |
125
|
|
|
$vars = [ |
126
|
|
|
'$ITERATION_NAMESPACE_CAMEL$' => ucfirst($route_key), |
127
|
|
|
'$ITERATION_NAMESPACE_LOWER$' => strtolower($route_key), |
128
|
|
|
'$INDENT$' => infy_tabs($indent * 3), |
129
|
|
|
]; |
130
|
|
|
$templateString = get_artomator_template('scaffold.routes.prefixed.namespace').$templateString.get_artomator_template('scaffold.routes.prefixed.closure'); |
131
|
|
|
$templateString = fill_template($vars, $templateString); |
132
|
|
|
} |
133
|
|
|
$templateContent .= $templateString; |
134
|
|
|
}//end foreach |
135
|
|
|
|
136
|
|
|
return $templateContent; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|