|
18 | 18 | use Blueprint\Models\Statements\SessionStatement;
|
19 | 19 | use Blueprint\Models\Statements\ValidateStatement;
|
20 | 20 | use Blueprint\Tree;
|
| 21 | +use Illuminate\Support\Arr; |
21 | 22 | use Illuminate\Support\Str;
|
22 | 23 |
|
23 | 24 | class ControllerGenerator extends AbstractClassGenerator implements Generator
|
@@ -79,68 +80,70 @@ protected function buildMethods(Controller $controller)
|
79 | 80 | $using_validation = false;
|
80 | 81 |
|
81 | 82 | foreach ($statements as $statement) {
|
82 |
| - if ($statement instanceof SendStatement) { |
83 |
| - $body .= self::INDENT . $statement->output() . PHP_EOL; |
84 |
| - if ($statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_FACADE) { |
85 |
| - $this->addImport($controller, 'Illuminate\\Support\\Facades\\Notification'); |
86 |
| - $this->addImport($controller, config('blueprint.namespace') . '\\Notification\\' . $statement->mail()); |
87 |
| - } elseif ($statement->type() === SendStatement::TYPE_MAIL) { |
88 |
| - $this->addImport($controller, 'Illuminate\\Support\\Facades\\Mail'); |
89 |
| - $this->addImport($controller, config('blueprint.namespace') . '\\Mail\\' . $statement->mail()); |
90 |
| - } |
91 |
| - } elseif ($statement instanceof ValidateStatement) { |
92 |
| - $using_validation = true; |
93 |
| - $class_name = $controller->name() . Str::studly($name) . 'Request'; |
94 |
| - |
95 |
| - $fqcn = config('blueprint.namespace') . '\\Http\\Requests\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $class_name; |
96 |
| - |
97 |
| - $method = str_replace('\Illuminate\Http\Request $request', '\\' . $fqcn . ' $request', $method); |
98 |
| - $method = str_replace('(Request $request', '(' . $class_name . ' $request', $method); |
99 |
| - |
100 |
| - $this->addImport($controller, $fqcn); |
101 |
| - } elseif ($statement instanceof DispatchStatement) { |
102 |
| - $body .= self::INDENT . $statement->output() . PHP_EOL; |
103 |
| - $this->addImport($controller, config('blueprint.namespace') . '\\Jobs\\' . $statement->job()); |
104 |
| - } elseif ($statement instanceof FireStatement) { |
105 |
| - $body .= self::INDENT . $statement->output() . PHP_EOL; |
106 |
| - if (!$statement->isNamedEvent()) { |
107 |
| - $this->addImport($controller, config('blueprint.namespace') . '\\Events\\' . $statement->event()); |
108 |
| - } |
109 |
| - } elseif ($statement instanceof RenderStatement) { |
110 |
| - $body .= self::INDENT . $statement->output() . PHP_EOL; |
111 |
| - } elseif ($statement instanceof ResourceStatement) { |
112 |
| - $fqcn = config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name(); |
113 |
| - $this->addImport($controller, $fqcn); |
114 |
| - $body .= self::INDENT . $statement->output() . PHP_EOL; |
115 |
| - |
116 |
| - if ($statement->paginate()) { |
117 |
| - if (!Str::contains($body, '::all();')) { |
118 |
| - $queryStatement = new QueryStatement('all', [$statement->reference()]); |
119 |
| - $body = implode(PHP_EOL, [ |
120 |
| - self::INDENT . $queryStatement->output($statement->reference()), |
121 |
| - PHP_EOL . $body, |
122 |
| - ]); |
123 |
| - |
124 |
| - $this->addImport($controller, $this->determineModel($controller, $queryStatement->model())); |
| 83 | + foreach (Arr::wrap($statement) as $statement) { |
| 84 | + if ($statement instanceof SendStatement) { |
| 85 | + $body .= self::INDENT . $statement->output() . PHP_EOL; |
| 86 | + if ($statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_FACADE) { |
| 87 | + $this->addImport($controller, 'Illuminate\\Support\\Facades\\Notification'); |
| 88 | + $this->addImport($controller, config('blueprint.namespace') . '\\Notification\\' . $statement->mail()); |
| 89 | + } elseif ($statement->type() === SendStatement::TYPE_MAIL) { |
| 90 | + $this->addImport($controller, 'Illuminate\\Support\\Facades\\Mail'); |
| 91 | + $this->addImport($controller, config('blueprint.namespace') . '\\Mail\\' . $statement->mail()); |
125 | 92 | }
|
126 |
| - |
127 |
| - $body = str_replace('::all();', '::paginate();', $body); |
| 93 | + } elseif ($statement instanceof ValidateStatement) { |
| 94 | + $using_validation = true; |
| 95 | + $class_name = $controller->name() . Str::studly($name) . 'Request'; |
| 96 | + |
| 97 | + $fqcn = config('blueprint.namespace') . '\\Http\\Requests\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $class_name; |
| 98 | + |
| 99 | + $method = str_replace('\Illuminate\Http\Request $request', '\\' . $fqcn . ' $request', $method); |
| 100 | + $method = str_replace('(Request $request', '(' . $class_name . ' $request', $method); |
| 101 | + |
| 102 | + $this->addImport($controller, $fqcn); |
| 103 | + } elseif ($statement instanceof DispatchStatement) { |
| 104 | + $body .= self::INDENT . $statement->output() . PHP_EOL; |
| 105 | + $this->addImport($controller, config('blueprint.namespace') . '\\Jobs\\' . $statement->job()); |
| 106 | + } elseif ($statement instanceof FireStatement) { |
| 107 | + $body .= self::INDENT . $statement->output() . PHP_EOL; |
| 108 | + if (!$statement->isNamedEvent()) { |
| 109 | + $this->addImport($controller, config('blueprint.namespace') . '\\Events\\' . $statement->event()); |
| 110 | + } |
| 111 | + } elseif ($statement instanceof RenderStatement) { |
| 112 | + $body .= self::INDENT . $statement->output() . PHP_EOL; |
| 113 | + } elseif ($statement instanceof ResourceStatement) { |
| 114 | + $fqcn = config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name(); |
| 115 | + $this->addImport($controller, $fqcn); |
| 116 | + $body .= self::INDENT . $statement->output() . PHP_EOL; |
| 117 | + |
| 118 | + if ($statement->paginate()) { |
| 119 | + if (!Str::contains($body, '::all();')) { |
| 120 | + $queryStatement = new QueryStatement('all', [$statement->reference()]); |
| 121 | + $body = implode(PHP_EOL, [ |
| 122 | + self::INDENT . $queryStatement->output($statement->reference()), |
| 123 | + PHP_EOL . $body, |
| 124 | + ]); |
| 125 | + |
| 126 | + $this->addImport($controller, $this->determineModel($controller, $queryStatement->model())); |
| 127 | + } |
| 128 | + |
| 129 | + $body = str_replace('::all();', '::paginate();', $body); |
| 130 | + } |
| 131 | + } elseif ($statement instanceof RedirectStatement) { |
| 132 | + $body .= self::INDENT . $statement->output() . PHP_EOL; |
| 133 | + } elseif ($statement instanceof RespondStatement) { |
| 134 | + $body .= self::INDENT . $statement->output() . PHP_EOL; |
| 135 | + } elseif ($statement instanceof SessionStatement) { |
| 136 | + $body .= self::INDENT . $statement->output() . PHP_EOL; |
| 137 | + } elseif ($statement instanceof EloquentStatement) { |
| 138 | + $body .= self::INDENT . $statement->output($controller->prefix(), $name, $using_validation) . PHP_EOL; |
| 139 | + $this->addImport($controller, $this->determineModel($controller, $statement->reference())); |
| 140 | + } elseif ($statement instanceof QueryStatement) { |
| 141 | + $body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL; |
| 142 | + $this->addImport($controller, $this->determineModel($controller, $statement->model())); |
128 | 143 | }
|
129 |
| - } elseif ($statement instanceof RedirectStatement) { |
130 |
| - $body .= self::INDENT . $statement->output() . PHP_EOL; |
131 |
| - } elseif ($statement instanceof RespondStatement) { |
132 |
| - $body .= self::INDENT . $statement->output() . PHP_EOL; |
133 |
| - } elseif ($statement instanceof SessionStatement) { |
134 |
| - $body .= self::INDENT . $statement->output() . PHP_EOL; |
135 |
| - } elseif ($statement instanceof EloquentStatement) { |
136 |
| - $body .= self::INDENT . $statement->output($controller->prefix(), $name, $using_validation) . PHP_EOL; |
137 |
| - $this->addImport($controller, $this->determineModel($controller, $statement->reference())); |
138 |
| - } elseif ($statement instanceof QueryStatement) { |
139 |
| - $body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL; |
140 |
| - $this->addImport($controller, $this->determineModel($controller, $statement->model())); |
141 |
| - } |
142 | 144 |
|
143 |
| - $body .= PHP_EOL; |
| 145 | + $body .= PHP_EOL; |
| 146 | + } |
144 | 147 | }
|
145 | 148 |
|
146 | 149 | if (!empty($body)) {
|
|
0 commit comments