22
33namespace Blueprint \Generators ;
44
5+ use Blueprint \Blueprint ;
56use Blueprint \Contracts \Generator ;
67use Blueprint \Models \Controller ;
78use Blueprint \Models \Statements \DispatchStatement ;
@@ -33,7 +34,7 @@ public function output(array $tree): array
3334 {
3435 $ output = [];
3536
36- $ stub = $ this ->files ->get ( STUBS_PATH . ' / controller/class.stub ' );
37+ $ stub = $ this ->files ->stub ( ' controller/class.stub ' );
3738
3839 /** @var \Blueprint\Models\Controller $controller */
3940 foreach ($ tree ['controllers ' ] as $ controller ) {
@@ -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 );
@@ -62,7 +63,7 @@ protected function populateStub(string $stub, Controller $controller)
6263
6364 private function buildMethods (Controller $ controller )
6465 {
65- $ template = $ this ->methodStub ( );
66+ $ template = $ this ->files -> stub ( ' controller/method.stub ' );
6667
6768 $ methods = '' ;
6869
@@ -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,18 +136,9 @@ private function buildMethods(Controller $controller)
133136
134137 protected function getPath (Controller $ controller )
135138 {
136- return 'app/Http/Controllers/ ' . $ controller ->className () . '.php ' ;
137- }
138-
139- private function methodStub ()
140- {
141- static $ stub = '' ;
139+ $ path = str_replace ('\\' , '/ ' , Blueprint::relativeNamespace ($ controller ->fullyQualifiedClassName ()));
142140
143- if (empty ($ stub )) {
144- $ stub = $ this ->files ->get (STUBS_PATH . '/controller/method.stub ' );
145- }
146-
147- return $ stub ;
141+ return config ('blueprint.app_path ' ) . '/ ' . $ path . '.php ' ;
148142 }
149143
150144 private function addImport (Controller $ controller , $ class )
@@ -162,10 +156,10 @@ private function buildImports(Controller $controller)
162156 }, $ imports ));
163157 }
164158
165- private function determineModel (string $ prefix , ?string $ reference )
159+ private function determineModel (Controller $ controller , ?string $ reference )
166160 {
167161 if (empty ($ reference ) || $ reference === 'id ' ) {
168- return Str::studly (Str::singular ($ prefix ));
162+ return Str::studly (Str::singular ($ controller -> prefix () ));
169163 }
170164
171165 if (Str::contains ($ reference , '. ' )) {
0 commit comments