4
4
5
5
use Blueprint \Blueprint ;
6
6
use Blueprint \Contracts \Generator ;
7
+ use Blueprint \Models \Model ;
7
8
use Blueprint \Models \Controller ;
8
9
use Blueprint \Models \Statements \DispatchStatement ;
9
10
use Blueprint \Models \Statements \EloquentStatement ;
@@ -27,6 +28,8 @@ class ControllerGenerator implements Generator
27
28
28
29
private $ imports = [];
29
30
31
+ private $ models = [];
32
+
30
33
public function __construct ($ files )
31
34
{
32
35
$ this ->files = $ files ;
@@ -38,6 +41,8 @@ public function output(array $tree): array
38
41
39
42
$ stub = $ this ->files ->stub ('controller/class.stub ' );
40
43
44
+ $ this ->registerModels ($ tree );
45
+
41
46
/** @var \Blueprint\Models\Controller $controller */
42
47
foreach ($ tree ['controllers ' ] as $ controller ) {
43
48
$ this ->addImport ($ controller , 'Illuminate \\Http \\Request ' );
@@ -198,6 +203,34 @@ private function fullyQualifyModelReference(string $sub_namespace, string $model
198
203
// TODO: get model_name from tree.
199
204
// If not found, assume parallel namespace as controller.
200
205
// Use respond-statement.php as test case.
206
+
207
+ /** @var \Blueprint\Models\Model $model */
208
+ $ model = $ this ->modelForContext ($ model_name );
209
+
210
+ if (isset ($ this ->models [Str::studly ($ model_name )])) {
211
+ return $ model ->fullyQualifiedClassName ();
212
+ }
213
+
201
214
return config ('blueprint.namespace ' ) . '\\' . ($ sub_namespace ? $ sub_namespace . '\\' : '' ) . $ model_name ;
202
215
}
216
+
217
+ private function modelForContext (string $ context )
218
+ {
219
+ if (isset ($ this ->models [Str::studly ($ context )])) {
220
+ return $ this ->models [Str::studly ($ context )];
221
+ }
222
+
223
+ $ matches = array_filter (array_keys ($ this ->models ), function ($ key ) use ($ context ) {
224
+ return Str::endsWith ($ key , '/ ' .Str::studly ($ context ));
225
+ });
226
+
227
+ if (count ($ matches ) === 1 ) {
228
+ return $ this ->models [$ matches [0 ]];
229
+ }
230
+ }
231
+
232
+ private function registerModels (array $ tree )
233
+ {
234
+ $ this ->models = array_merge ($ tree ['cache ' ] ?? [], $ tree ['models ' ] ?? []);
235
+ }
203
236
}
0 commit comments