4
4
5
5
use Blueprint \Contracts \Generator ;
6
6
use Blueprint \Models \Controller ;
7
+ use Blueprint \Models \Statements \DispatchStatement ;
8
+ use Blueprint \Models \Statements \EloquentStatement ;
9
+ use Blueprint \Models \Statements \FireStatement ;
10
+ use Blueprint \Models \Statements \QueryStatement ;
11
+ use Blueprint \Models \Statements \RedirectStatement ;
12
+ use Blueprint \Models \Statements \RenderStatement ;
13
+ use Blueprint \Models \Statements \SendStatement ;
14
+ use Blueprint \Models \Statements \SessionStatement ;
15
+ use Blueprint \Models \Statements \ValidateStatement ;
7
16
use Illuminate \Support \Str ;
8
17
9
18
class ControllerGenerator implements Generator
10
19
{
20
+ const INDENT = ' ' ;
21
+
11
22
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
12
23
private $ files ;
13
24
25
+ private $ imports = [];
26
+
14
27
public function __construct ($ files )
15
28
{
16
29
$ this ->files = $ files ;
@@ -24,6 +37,7 @@ public function output(array $tree): array
24
37
25
38
/** @var \Blueprint\Models\Controller $controller */
26
39
foreach ($ tree ['controllers ' ] as $ controller ) {
40
+ $ this ->addImport ($ controller , 'Illuminate \\Http \\Request ' );
27
41
$ path = $ this ->getPath ($ controller );
28
42
$ this ->files ->put (
29
43
$ path ,
@@ -41,7 +55,7 @@ protected function populateStub(string $stub, Controller $controller)
41
55
$ stub = str_replace ('DummyNamespace ' , 'App \\Http \\Controllers ' , $ stub );
42
56
$ stub = str_replace ('DummyClass ' , $ this ->className ($ controller ), $ stub );
43
57
$ stub = str_replace ('// methods... ' , $ this ->buildMethods ($ controller ), $ stub );
44
- $ stub = $ this ->addImports ($ controller , $ stub );
58
+ $ stub = str_replace ( ' // imports... ' , $ this ->buildImports ($ controller) , $ stub );
45
59
46
60
return $ stub ;
47
61
}
@@ -52,13 +66,54 @@ private function buildMethods(Controller $controller)
52
66
53
67
$ methods = '' ;
54
68
55
- foreach ($ controller ->methods () as $ name => $ body ) {
56
- $ methods .= PHP_EOL . str_replace ('DummyMethod ' , $ name , $ template );
57
- // TODO:
58
- // foreach statements
59
- // output their resulting code
60
- // validate => replace Request injection (addImport)
61
- // queue => output Job::dispatch($data) (addImport)
69
+ foreach ($ controller ->methods () as $ name => $ statements ) {
70
+ $ method = str_replace ('DummyMethod ' , $ name , $ template );
71
+
72
+ // TODO: if resourceful action, do implicit model binding
73
+
74
+ $ body = '' ;
75
+ foreach ($ statements as $ statement ) {
76
+ if ($ statement instanceof SendStatement) {
77
+ $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
78
+ $ this ->addImport ($ controller , 'Illuminate \\Support \\Facades \\Mail ' );
79
+ $ this ->addImport ($ controller , 'App \\Mail \\' . $ statement ->mail ());
80
+ } elseif ($ statement instanceof ValidateStatement) {
81
+ $ class = $ controller ->name () . Str::studly ($ name ) . 'Request ' ;
82
+
83
+ $ method = str_replace ('\Illuminate\Http\Request $request ' , '\\App \\Http \\Requests \\' . $ class . ' $request ' , $ method );
84
+ $ method = str_replace ('(Request $request) ' , '( ' . $ class . ' $request) ' , $ method );
85
+
86
+ $ this ->addImport ($ controller , 'App \\Http \\Requests \\' . $ class );
87
+ } elseif ($ statement instanceof DispatchStatement) {
88
+ $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
89
+ $ this ->addImport ($ controller , 'App \\Jobs \\' . $ statement ->job ());
90
+ } elseif ($ statement instanceof FireStatement) {
91
+ $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
92
+ if (!$ statement ->isNamedEvent ()) {
93
+ $ this ->addImport ($ controller , 'App \\Events \\' . $ statement ->event ());
94
+ }
95
+ } elseif ($ statement instanceof RenderStatement) {
96
+ $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
97
+ } elseif ($ statement instanceof RedirectStatement) {
98
+ $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
99
+ } elseif ($ statement instanceof SessionStatement) {
100
+ $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
101
+ } elseif ($ statement instanceof EloquentStatement) {
102
+ // TODO: pass controller method for context..
103
+ $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
104
+ $ this ->addImport ($ controller , 'App \\' . Str::studly ($ statement ->reference ()));
105
+ } elseif ($ statement instanceof QueryStatement) {
106
+ $ body .= self ::INDENT . $ statement ->output () . PHP_EOL ;
107
+ }
108
+
109
+ $ body .= PHP_EOL ;
110
+ }
111
+
112
+ if (!empty ($ body )) {
113
+ $ method = str_replace ('// ' , trim ($ body ), $ method );
114
+ }
115
+
116
+ $ methods .= PHP_EOL . $ method ;
62
117
}
63
118
64
119
return trim ($ methods );
@@ -80,13 +135,23 @@ private function methodStub()
80
135
return $ stub ;
81
136
}
82
137
83
- private function addImports (Controller $ controller, $ stub )
138
+ protected function className (Controller $ controller): string
84
139
{
85
- return $ stub ;
140
+ return $ controller -> name () . (Str:: endsWith ( $ controller -> name (), ' Controller ' ) ? '' : ' Controller ' ) ;
86
141
}
87
142
88
- protected function className (Controller $ controller): string
143
+ private function addImport (Controller $ controller, $ class )
89
144
{
90
- return $ controller ->name () . (Str::endsWith ($ controller ->name (), 'Controller ' ) ? '' : 'Controller ' );
145
+ $ this ->imports [$ controller ->name ()][] = $ class ;
146
+ }
147
+
148
+ private function buildImports (Controller $ controller )
149
+ {
150
+ $ imports = array_unique ($ this ->imports [$ controller ->name ()]);
151
+ sort ($ imports );
152
+
153
+ return implode (PHP_EOL , array_map (function ($ class ) {
154
+ return 'use ' . $ class . '; ' ;
155
+ }, $ imports ));
91
156
}
92
157
}
0 commit comments