|
3 | 3 | namespace Blueprint\Generators;
|
4 | 4 |
|
5 | 5 | use Blueprint\Blueprint;
|
| 6 | +use Blueprint\Concerns\HandlesImports; |
| 7 | +use Blueprint\Concerns\HandlesTraits; |
6 | 8 | use Blueprint\Contracts\Generator;
|
7 | 9 | use Blueprint\Models\Controller;
|
| 10 | +use Blueprint\Models\Model; |
8 | 11 | use Blueprint\Models\Statements\DispatchStatement;
|
9 | 12 | use Blueprint\Models\Statements\EloquentStatement;
|
10 | 13 | use Blueprint\Models\Statements\FireStatement;
|
|
17 | 20 | use Blueprint\Models\Statements\SessionStatement;
|
18 | 21 | use Blueprint\Models\Statements\ValidateStatement;
|
19 | 22 | use Blueprint\Tree;
|
20 |
| -use Illuminate\Filesystem\Filesystem; |
21 | 23 | use Illuminate\Support\Str;
|
22 | 24 |
|
23 |
| -class ControllerGenerator implements Generator |
| 25 | +class ControllerGenerator extends AbstractClassGenerator implements Generator |
24 | 26 | {
|
25 |
| - const INDENT = ' '; |
| 27 | + use HandlesImports, HandlesTraits; |
26 | 28 |
|
27 |
| - /** |
28 |
| - * @var Filesystem |
29 |
| - */ |
30 |
| - protected $filesystem; |
31 |
| - |
32 |
| - private $imports = []; |
33 |
| - |
34 |
| - /** |
35 |
| - * @var Tree |
36 |
| - */ |
37 |
| - private $tree; |
38 |
| - |
39 |
| - public function __construct(Filesystem $filesystem) |
40 |
| - { |
41 |
| - $this->filesystem = $filesystem; |
42 |
| - } |
| 29 | + protected $types = ['controllers']; |
43 | 30 |
|
44 | 31 | public function output(Tree $tree): array
|
45 | 32 | {
|
46 | 33 | $this->tree = $tree;
|
47 | 34 |
|
48 |
| - $output = []; |
49 |
| - |
50 | 35 | $stub = $this->filesystem->stub('controller.class.stub');
|
51 | 36 |
|
52 | 37 | /** @var \Blueprint\Models\Controller $controller */
|
53 | 38 | foreach ($tree->controllers() as $controller) {
|
54 | 39 | $this->addImport($controller, 'Illuminate\\Http\\Request');
|
55 |
| - |
56 | 40 | if ($controller->fullyQualifiedNamespace() !== 'App\\Http\\Controllers') {
|
57 | 41 | $this->addImport($controller, 'App\\Http\\Controllers\\Controller');
|
58 | 42 | }
|
59 |
| - |
60 | 43 | $path = $this->getPath($controller);
|
61 | 44 |
|
62 |
| - if (!$this->filesystem->exists(dirname($path))) { |
63 |
| - $this->filesystem->makeDirectory(dirname($path), 0755, true); |
64 |
| - } |
65 |
| - |
66 |
| - $this->filesystem->put($path, $this->populateStub($stub, $controller)); |
67 |
| - |
68 |
| - $output['created'][] = $path; |
| 45 | + $this->create($path, $this->populateStub($stub, $controller)); |
69 | 46 | }
|
70 | 47 |
|
71 |
| - return $output; |
72 |
| - } |
73 |
| - |
74 |
| - public function types(): array |
75 |
| - { |
76 |
| - return ['controllers']; |
| 48 | + return $this->output; |
77 | 49 | }
|
78 | 50 |
|
79 | 51 | protected function populateStub(string $stub, Controller $controller)
|
@@ -149,7 +121,7 @@ protected function buildMethods(Controller $controller)
|
149 | 121 | $body .= self::INDENT . $statement->output() . PHP_EOL;
|
150 | 122 |
|
151 | 123 | if ($statement->paginate()) {
|
152 |
| - if (! Str::contains($body, '::all();')) { |
| 124 | + if (!Str::contains($body, '::all();')) { |
153 | 125 | $queryStatement = new QueryStatement('all', [$statement->reference()]);
|
154 | 126 | $body = implode(PHP_EOL, [
|
155 | 127 | self::INDENT . $queryStatement->output($statement->reference()),
|
@@ -196,34 +168,6 @@ protected function buildMethods(Controller $controller)
|
196 | 168 | return trim($methods);
|
197 | 169 | }
|
198 | 170 |
|
199 |
| - protected function getPath(Controller $controller) |
200 |
| - { |
201 |
| - $path = str_replace('\\', '/', Blueprint::relativeNamespace($controller->fullyQualifiedClassName())); |
202 |
| - |
203 |
| - return Blueprint::appPath() . '/' . $path . '.php'; |
204 |
| - } |
205 |
| - |
206 |
| - protected function buildImports(Controller $controller) |
207 |
| - { |
208 |
| - $imports = array_unique($this->imports[$controller->name()]); |
209 |
| - sort($imports); |
210 |
| - |
211 |
| - return implode( |
212 |
| - PHP_EOL, |
213 |
| - array_map( |
214 |
| - function ($class) { |
215 |
| - return 'use ' . $class . ';'; |
216 |
| - }, |
217 |
| - $imports |
218 |
| - ) |
219 |
| - ); |
220 |
| - } |
221 |
| - |
222 |
| - private function addImport(Controller $controller, $class) |
223 |
| - { |
224 |
| - $this->imports[$controller->name()][] = $class; |
225 |
| - } |
226 |
| - |
227 | 171 | private function determineModel(Controller $controller, ?string $reference)
|
228 | 172 | {
|
229 | 173 | if (empty($reference) || $reference === 'id') {
|
|
0 commit comments