22
33namespace Blueprint \Generators ;
44
5+ use Blueprint \Blueprint ;
56use Blueprint \Contracts \Generator ;
67use Blueprint \Models \Controller ;
78use Blueprint \Models \Statements \DispatchStatement ;
@@ -52,7 +53,7 @@ public function output(array $tree): array
5253
5354 protected function populateStub (string $ stub , Controller $ controller )
5455 {
55- $ stub = str_replace ('DummyNamespace ' , ' App \\ Http \\ Controllers ' , $ stub );
56+ $ stub = str_replace ('DummyNamespace ' , $ controller -> fullyQualifiedNamespace () , $ stub );
5657 $ stub = str_replace ('DummyClass ' , $ controller ->className (), $ stub );
5758 $ stub = str_replace ('// methods... ' , $ this ->buildMethods ($ controller ), $ stub );
5859 $ stub = str_replace ('// imports... ' , $ this ->buildImports ($ controller ), $ stub );
@@ -71,7 +72,7 @@ private function buildMethods(Controller $controller)
7172
7273 if (in_array ($ name , ['edit ' , 'update ' , 'show ' , 'destroy ' ])) {
7374 $ context = Str::singular ($ controller ->prefix ());
74- $ reference = ' App \\' . $ context ;
75+ $ reference = config ( ' blueprint.namespace ' ) . ' \\' . $ context ;
7576 $ variable = '$ ' . Str::camel ($ context );
7677
7778 // TODO: verify controller prefix references a model
@@ -88,21 +89,23 @@ private function buildMethods(Controller $controller)
8889 if ($ statement instanceof SendStatement) {
8990 $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
9091 $ this ->addImport ($ controller , 'Illuminate \\Support \\Facades \\Mail ' );
91- $ this ->addImport ($ controller , ' App \\Mail \\' . $ statement ->mail ());
92+ $ this ->addImport ($ controller , config ( ' blueprint.namespace ' ) . ' \\Mail \\' . $ statement ->mail ());
9293 } elseif ($ statement instanceof ValidateStatement) {
93- $ class = $ controller ->name () . Str::studly ($ name ) . 'Request ' ;
94+ $ class_name = $ controller ->name () . Str::studly ($ name ) . 'Request ' ;
9495
95- $ method = str_replace ('\Illuminate\Http\Request $request ' , '\\App \\Http \\Requests \\' . $ class . ' $request ' , $ method );
96- $ method = str_replace ('(Request $request ' , '( ' . $ class . ' $request ' , $ method );
96+ $ fqcn = config ('blueprint.namespace ' ) . '\\Http \\Requests \\' . ($ controller ->namespace () ? $ controller ->namespace () . '\\' : '' ) . $ class_name ;
9797
98- $ this ->addImport ($ controller , 'App \\Http \\Requests \\' . $ class );
98+ $ method = str_replace ('\Illuminate\Http\Request $request ' , '\\' . $ fqcn . ' $request ' , $ method );
99+ $ method = str_replace ('(Request $request ' , '( ' . $ class_name . ' $request ' , $ method );
100+
101+ $ this ->addImport ($ controller , $ fqcn );
99102 } elseif ($ statement instanceof DispatchStatement) {
100103 $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
101- $ this ->addImport ($ controller , ' App \\Jobs \\' . $ statement ->job ());
104+ $ this ->addImport ($ controller , config ( ' blueprint.namespace ' ) . ' \\Jobs \\' . $ statement ->job ());
102105 } elseif ($ statement instanceof FireStatement) {
103106 $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
104107 if (!$ statement ->isNamedEvent ()) {
105- $ this ->addImport ($ controller , ' App \\Events \\' . $ statement ->event ());
108+ $ this ->addImport ($ controller , config ( ' blueprint.namespace ' ) . ' \\Events \\' . $ statement ->event ());
106109 }
107110 } elseif ($ statement instanceof RenderStatement) {
108111 $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
@@ -112,10 +115,10 @@ private function buildMethods(Controller $controller)
112115 $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
113116 } elseif ($ statement instanceof EloquentStatement) {
114117 $ body .= self ::INDENT . $ statement ->output ($ controller ->prefix (), $ name ) . PHP_EOL ;
115- $ this ->addImport ($ controller , ' App \\' . $ this -> determineModel ( $ controller ->prefix () , $ statement ->reference ()));
118+ $ this ->addImport ($ controller , config ( ' blueprint.namespace ' ) . ' \\' . ( $ controller -> namespace () ? $ controller ->namespace () . '\\' : '' ) . $ this -> determineModel ( $ controller , $ statement ->reference ()));
116119 } elseif ($ statement instanceof QueryStatement) {
117120 $ body .= self ::INDENT . $ statement ->output ($ controller ->prefix ()) . PHP_EOL ;
118- $ this ->addImport ($ controller , ' App \\' . $ this -> determineModel ( $ controller ->prefix () , $ statement ->model ()));
121+ $ this ->addImport ($ controller , config ( ' blueprint.namespace ' ) . ' \\' . ( $ controller -> namespace () ? $ controller ->namespace () . '\\' : '' ) . $ this -> determineModel ( $ controller , $ statement ->model ()));
119122 }
120123
121124 $ body .= PHP_EOL ;
@@ -133,7 +136,9 @@ private function buildMethods(Controller $controller)
133136
134137 protected function getPath (Controller $ controller )
135138 {
136- return 'app/Http/Controllers/ ' . $ controller ->className () . '.php ' ;
139+ $ path = str_replace ('\\' , '/ ' , Blueprint::relativeNamespace ($ controller ->fullyQualifiedClassName ()));
140+
141+ return config ('blueprint.app_path ' ) . '/ ' . $ path . '.php ' ;
137142 }
138143
139144 private function methodStub ()
@@ -162,10 +167,10 @@ private function buildImports(Controller $controller)
162167 }, $ imports ));
163168 }
164169
165- private function determineModel (string $ prefix , ?string $ reference )
170+ private function determineModel (Controller $ controller , ?string $ reference )
166171 {
167172 if (empty ($ reference ) || $ reference === 'id ' ) {
168- return Str::studly (Str::singular ($ prefix ));
173+ return Str::studly (Str::singular ($ controller -> prefix () ));
169174 }
170175
171176 if (Str::contains ($ reference , '. ' )) {
0 commit comments