2
2
3
3
namespace Blueprint \Generators ;
4
4
5
+ use Blueprint \Blueprint ;
5
6
use Blueprint \Contracts \Generator ;
6
7
use Blueprint \Models \Controller ;
7
8
use Blueprint \Models \Statements \DispatchStatement ;
@@ -33,7 +34,7 @@ public function output(array $tree): array
33
34
{
34
35
$ output = [];
35
36
36
- $ stub = $ this ->files ->get ( STUBS_PATH . ' / controller/class.stub ' );
37
+ $ stub = $ this ->files ->stub ( ' controller/class.stub ' );
37
38
38
39
/** @var \Blueprint\Models\Controller $controller */
39
40
foreach ($ tree ['controllers ' ] as $ controller ) {
@@ -52,7 +53,7 @@ public function output(array $tree): array
52
53
53
54
protected function populateStub (string $ stub , Controller $ controller )
54
55
{
55
- $ stub = str_replace ('DummyNamespace ' , ' App \\ Http \\ Controllers ' , $ stub );
56
+ $ stub = str_replace ('DummyNamespace ' , $ controller -> fullyQualifiedNamespace () , $ stub );
56
57
$ stub = str_replace ('DummyClass ' , $ controller ->className (), $ stub );
57
58
$ stub = str_replace ('// methods... ' , $ this ->buildMethods ($ controller ), $ stub );
58
59
$ stub = str_replace ('// imports... ' , $ this ->buildImports ($ controller ), $ stub );
@@ -62,7 +63,7 @@ protected function populateStub(string $stub, Controller $controller)
62
63
63
64
private function buildMethods (Controller $ controller )
64
65
{
65
- $ template = $ this ->methodStub ( );
66
+ $ template = $ this ->files -> stub ( ' controller/method.stub ' );
66
67
67
68
$ methods = '' ;
68
69
@@ -71,7 +72,7 @@ private function buildMethods(Controller $controller)
71
72
72
73
if (in_array ($ name , ['edit ' , 'update ' , 'show ' , 'destroy ' ])) {
73
74
$ context = Str::singular ($ controller ->prefix ());
74
- $ reference = ' App \\' . $ context ;
75
+ $ reference = config ( ' blueprint.namespace ' ) . ' \\' . $ context ;
75
76
$ variable = '$ ' . Str::camel ($ context );
76
77
77
78
// TODO: verify controller prefix references a model
@@ -88,21 +89,23 @@ private function buildMethods(Controller $controller)
88
89
if ($ statement instanceof SendStatement) {
89
90
$ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
90
91
$ 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 ());
92
93
} elseif ($ statement instanceof ValidateStatement) {
93
- $ class = $ controller ->name () . Str::studly ($ name ) . 'Request ' ;
94
+ $ class_name = $ controller ->name () . Str::studly ($ name ) . 'Request ' ;
94
95
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 ;
97
97
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 );
99
102
} elseif ($ statement instanceof DispatchStatement) {
100
103
$ 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 ());
102
105
} elseif ($ statement instanceof FireStatement) {
103
106
$ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
104
107
if (!$ statement ->isNamedEvent ()) {
105
- $ this ->addImport ($ controller , ' App \\Events \\' . $ statement ->event ());
108
+ $ this ->addImport ($ controller , config ( ' blueprint.namespace ' ) . ' \\Events \\' . $ statement ->event ());
106
109
}
107
110
} elseif ($ statement instanceof RenderStatement) {
108
111
$ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
@@ -112,10 +115,10 @@ private function buildMethods(Controller $controller)
112
115
$ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
113
116
} elseif ($ statement instanceof EloquentStatement) {
114
117
$ 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 ()));
116
119
} elseif ($ statement instanceof QueryStatement) {
117
120
$ 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 ()));
119
122
}
120
123
121
124
$ body .= PHP_EOL ;
@@ -133,18 +136,9 @@ private function buildMethods(Controller $controller)
133
136
134
137
protected function getPath (Controller $ controller )
135
138
{
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 ()));
142
140
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 ' ;
148
142
}
149
143
150
144
private function addImport (Controller $ controller , $ class )
@@ -162,10 +156,10 @@ private function buildImports(Controller $controller)
162
156
}, $ imports ));
163
157
}
164
158
165
- private function determineModel (string $ prefix , ?string $ reference )
159
+ private function determineModel (Controller $ controller , ?string $ reference )
166
160
{
167
161
if (empty ($ reference ) || $ reference === 'id ' ) {
168
- return Str::studly (Str::singular ($ prefix ));
162
+ return Str::studly (Str::singular ($ controller -> prefix () ));
169
163
}
170
164
171
165
if (Str::contains ($ reference , '. ' )) {
0 commit comments