5
5
use Blueprint \Blueprint ;
6
6
use Blueprint \Contracts \Generator ;
7
7
use Blueprint \Models \Controller ;
8
- use Blueprint \Models \Model ;
9
8
use Blueprint \Models \Statements \DispatchStatement ;
10
9
use Blueprint \Models \Statements \EloquentStatement ;
11
10
use Blueprint \Models \Statements \FireStatement ;
17
16
use Blueprint \Models \Statements \SendStatement ;
18
17
use Blueprint \Models \Statements \SessionStatement ;
19
18
use Blueprint \Models \Statements \ValidateStatement ;
19
+ use Blueprint \Tree ;
20
20
use Illuminate \Support \Str ;
21
21
22
22
class ControllerGenerator implements Generator
@@ -28,23 +28,24 @@ class ControllerGenerator implements Generator
28
28
29
29
private $ imports = [];
30
30
31
- private $ models = [];
31
+ /** @var Tree */
32
+ private $ tree ;
32
33
33
34
public function __construct ($ files )
34
35
{
35
36
$ this ->files = $ files ;
36
37
}
37
38
38
- public function output (array $ tree ): array
39
+ public function output (Tree $ tree ): array
39
40
{
41
+ $ this ->tree = $ tree ;
42
+
40
43
$ output = [];
41
44
42
45
$ stub = $ this ->files ->stub ('controller/class.stub ' );
43
46
44
- $ this ->registerModels ($ tree );
45
-
46
47
/** @var \Blueprint\Models\Controller $controller */
47
- foreach ($ tree[ ' controllers ' ] as $ controller ) {
48
+ foreach ($ tree-> controllers () as $ controller ) {
48
49
$ this ->addImport ($ controller , 'Illuminate \\Http \\Request ' );
49
50
50
51
if ($ controller ->fullyQualifiedNamespace () !== 'App \\Http \\Controllers ' ) {
@@ -53,7 +54,7 @@ public function output(array $tree): array
53
54
54
55
$ path = $ this ->getPath ($ controller );
55
56
56
- if (! $ this ->files ->exists (dirname ($ path ))) {
57
+ if (!$ this ->files ->exists (dirname ($ path ))) {
57
58
$ this ->files ->makeDirectory (dirname ($ path ), 0755 , true );
58
59
}
59
60
@@ -131,7 +132,7 @@ protected function buildMethods(Controller $controller)
131
132
$ this ->addImport ($ controller , config ('blueprint.namespace ' ).'\\Jobs \\' .$ statement ->job ());
132
133
} elseif ($ statement instanceof FireStatement) {
133
134
$ body .= self ::INDENT .$ statement ->output ().PHP_EOL ;
134
- if (! $ statement ->isNamedEvent ()) {
135
+ if (!$ statement ->isNamedEvent ()) {
135
136
$ this ->addImport ($ controller , config ('blueprint.namespace ' ).'\\Events \\' .$ statement ->event ());
136
137
}
137
138
} elseif ($ statement instanceof RenderStatement) {
@@ -142,7 +143,7 @@ protected function buildMethods(Controller $controller)
142
143
$ method = str_replace ('* @return \\Illuminate \\Http \\Response ' , '* @return \\' .$ fqcn , $ method );
143
144
144
145
$ import = $ fqcn ;
145
- if (! $ statement ->collection ()) {
146
+ if (!$ statement ->collection ()) {
146
147
$ import .= ' as ' .$ statement ->name ().'Resource ' ;
147
148
}
148
149
@@ -166,7 +167,7 @@ protected function buildMethods(Controller $controller)
166
167
$ body .= PHP_EOL ;
167
168
}
168
169
169
- if (! empty ($ body )) {
170
+ if (!empty ($ body )) {
170
171
$ method = str_replace ('// ' , trim ($ body ), $ method );
171
172
}
172
173
@@ -218,32 +219,12 @@ private function fullyQualifyModelReference(string $sub_namespace, string $model
218
219
// Use respond-statement.php as test case.
219
220
220
221
/** @var \Blueprint\Models\Model $model */
221
- $ model = $ this ->modelForContext ($ model_name );
222
+ $ model = $ this ->tree -> modelForContext ($ model_name );
222
223
223
224
if (isset ($ model )) {
224
225
return $ model ->fullyQualifiedClassName ();
225
226
}
226
227
227
228
return config ('blueprint.namespace ' ).'\\' .($ sub_namespace ? $ sub_namespace .'\\' : '' ).$ model_name ;
228
229
}
229
-
230
- private function modelForContext (string $ context )
231
- {
232
- if (isset ($ this ->models [Str::studly ($ context )])) {
233
- return $ this ->models [Str::studly ($ context )];
234
- }
235
-
236
- $ matches = array_filter (array_keys ($ this ->models ), function ($ key ) use ($ context ) {
237
- return Str::endsWith ($ key , '/ ' .Str::studly ($ context ));
238
- });
239
-
240
- if (count ($ matches ) === 1 ) {
241
- return $ this ->models [$ matches [0 ]];
242
- }
243
- }
244
-
245
- private function registerModels (array $ tree )
246
- {
247
- $ this ->models = array_merge ($ tree ['cache ' ] ?? [], $ tree ['models ' ] ?? []);
248
- }
249
230
}
0 commit comments