1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scaffolder\Compilers\Core; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
6
|
|
|
use Scaffolder\Compilers\AbstractCompiler; |
7
|
|
|
use Scaffolder\Compilers\Support\FileToCompile; |
8
|
|
|
use Scaffolder\Compilers\Support\PathParser; |
9
|
|
|
use Scaffolder\Support\CamelCase; |
10
|
|
|
|
11
|
|
|
class RouteCompiler extends AbstractCompiler |
12
|
|
|
{ |
13
|
|
|
protected $stubFilename = 'Routes.php' ; |
14
|
|
|
|
15
|
|
|
protected $stubResourceFilename = 'ResourceRoute.php' ; |
16
|
|
|
protected $stubResource ; |
17
|
|
|
|
18
|
|
|
|
19
|
|
View Code Duplication |
public function __construct($scaffolderConfig, $modelData = null) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$this->stubsDirectory = __DIR__ . '/../../../../stubs/Api/'; |
22
|
|
|
parent::__construct($scaffolderConfig, null); |
23
|
|
|
|
24
|
|
|
$this->stubResource = File::get($this->stubsDirectory . $this->stubResourceFilename ); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Replace and store the Stub. |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function replaceAndStore(){} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Compiles a resource. |
36
|
|
|
* |
37
|
|
|
* @param $hash |
38
|
|
|
* @param null $extra |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function compile($extra = null) {} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Compiles a group of routes. |
46
|
|
|
* |
47
|
|
|
* @param $hash |
48
|
|
|
* @param null $extra |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function compileGroup($compiledRoutes) |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
$this->replaceRoutes($compiledRoutes) |
56
|
|
|
->replaceRoutePrefix() |
57
|
|
|
->store(new FileToCompile(null, null)); |
58
|
|
|
|
59
|
|
|
return $this->stub; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get output filename |
65
|
|
|
* |
66
|
|
|
* |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
protected function getOutputFilename() |
70
|
|
|
{ |
71
|
|
|
$folder = PathParser::parse($this->scaffolderConfig->generator->paths->routes); |
72
|
|
|
|
73
|
|
|
return $folder . 'routes.php'; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Replace the resource. |
79
|
|
|
* |
80
|
|
|
* @param $this->modelName |
81
|
|
|
* |
82
|
|
|
* @return string routeStub |
83
|
|
|
*/ |
84
|
|
|
public function replaceResource($modelData) |
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
$routeStub = str_replace('{{resource_lw}}', strtolower($modelData->modelName), $this->stubResource); |
88
|
|
|
$routeStub = str_replace('{{resource}}', $modelData->modelName, $routeStub); |
89
|
|
|
$routeStub = str_replace('{{reverseRelationships}}', $this->replaceReverseRelationships($modelData), $routeStub); |
90
|
|
|
$routeStub = str_replace('{{enum}}', $this->replaceEnum($modelData), $routeStub); |
91
|
|
|
|
92
|
|
|
return $routeStub; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Replace the reverse relationships. |
97
|
|
|
* |
98
|
|
|
* @param $this->modelData |
99
|
|
|
* |
100
|
|
|
* @return string functions |
101
|
|
|
*/ |
102
|
|
|
public function replaceReverseRelationships($modelData) |
103
|
|
|
{ |
104
|
|
|
$functions = ''; |
105
|
|
|
|
106
|
|
|
foreach ($modelData->reverseRelationships as $relationship) |
107
|
|
|
{ |
108
|
|
|
if ($relationship) |
109
|
|
|
{ |
110
|
|
|
$functionName = ''; |
|
|
|
|
111
|
|
|
if ($relationship->type == "hasOne") |
112
|
|
|
$functionName = strtolower($relationship->modelName); |
113
|
|
|
elseif ($relationship->type == "belongsToMany") |
114
|
|
|
$functionName = CamelCase::pluralize(strtolower($relationship->relatedTable)); |
115
|
|
|
else |
116
|
|
|
$functionName = CamelCase::pluralize(strtolower($relationship->modelName)); |
117
|
|
|
|
118
|
|
|
$method = "\tRoute::get('{{resource_lw}}/{id}/{{function_name}}', '{{resource}}Controller@{{function_name}}');\n"; |
119
|
|
|
$method = str_replace('{{resource_lw}}', strtolower($modelData->modelName), $method); |
120
|
|
|
$method = str_replace('{{function_name}}', $functionName, $method); |
121
|
|
|
$method = str_replace('{{resource}}', $modelData->modelName, $method); |
122
|
|
|
|
123
|
|
|
$functions .= $method; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $functions; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Replace the enum. |
132
|
|
|
* |
133
|
|
|
* @param $this->modelData |
134
|
|
|
* |
135
|
|
|
* @return string functions |
136
|
|
|
*/ |
137
|
|
|
public function replaceEnum($modelData) |
138
|
|
|
{ |
139
|
|
|
$functions = ''; |
140
|
|
|
|
141
|
|
|
foreach ($modelData->fields as $field) |
142
|
|
|
{ |
143
|
|
|
if ($field->type->db == "enum") |
144
|
|
|
{ |
145
|
|
|
$method = "\tRoute::get('{{resource_lw}}/{{field_name}}', '{{resource}}Controller@get{{field_name_uc}}Options');\n"; |
146
|
|
|
$method = str_replace('{{resource_lw}}', strtolower($modelData->modelName), $method); |
147
|
|
|
$method = str_replace('{{field_name_uc}}', CamelCase::convertToCamelCase($field->name), $method); |
148
|
|
|
$method = str_replace('{{field_name}}', $field->name, $method); |
149
|
|
|
$method = str_replace('{{resource}}', $modelData->modelName, $method); |
150
|
|
|
|
151
|
|
|
$functions .= $method; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $functions; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Replace compiled routes. |
161
|
|
|
* |
162
|
|
|
* @param $compiledRoutes |
163
|
|
|
* |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
private function replaceRoutes($compiledRoutes) |
167
|
|
|
{ |
168
|
|
|
$this->stub = str_replace('{{routes}}', $compiledRoutes, $this->stub); |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.