Skip to content

Commit 1b20a79

Browse files
author
Brian Faust
authored
Add support for iterating over statement children (#619)
1 parent 032aa5d commit 1b20a79

File tree

6 files changed

+376
-358
lines changed

6 files changed

+376
-358
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Blueprint\Models\Statements\SessionStatement;
1919
use Blueprint\Models\Statements\ValidateStatement;
2020
use Blueprint\Tree;
21+
use Illuminate\Support\Arr;
2122
use Illuminate\Support\Str;
2223

2324
class ControllerGenerator extends AbstractClassGenerator implements Generator
@@ -79,68 +80,70 @@ protected function buildMethods(Controller $controller)
7980
$using_validation = false;
8081

8182
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());
12592
}
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()));
128143
}
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-
}
142144

143-
$body .= PHP_EOL;
145+
$body .= PHP_EOL;
146+
}
144147
}
145148

146149
if (!empty($body)) {

src/Generators/Statements/EventGenerator.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Blueprint\Generators\StatementGenerator;
77
use Blueprint\Models\Statements\FireStatement;
88
use Blueprint\Tree;
9+
use Illuminate\Support\Arr;
910

1011
class EventGenerator extends StatementGenerator
1112
{
@@ -21,21 +22,23 @@ public function output(Tree $tree): array
2122
foreach ($tree->controllers() as $controller) {
2223
foreach ($controller->methods() as $method => $statements) {
2324
foreach ($statements as $statement) {
24-
if (!$statement instanceof FireStatement) {
25-
continue;
26-
}
25+
foreach (Arr::wrap($statement) as $statement) {
26+
if (!$statement instanceof FireStatement) {
27+
continue;
28+
}
2729

28-
if ($statement->isNamedEvent()) {
29-
continue;
30-
}
30+
if ($statement->isNamedEvent()) {
31+
continue;
32+
}
3133

32-
$path = $this->getStatementPath($statement->event());
34+
$path = $this->getStatementPath($statement->event());
3335

34-
if ($this->filesystem->exists($path)) {
35-
continue;
36-
}
36+
if ($this->filesystem->exists($path)) {
37+
continue;
38+
}
3739

38-
$this->create($path, $this->populateStub($stub, $statement));
40+
$this->create($path, $this->populateStub($stub, $statement));
41+
}
3942
}
4043
}
4144
}

src/Generators/Statements/JobGenerator.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Blueprint\Generators\StatementGenerator;
77
use Blueprint\Models\Statements\DispatchStatement;
88
use Blueprint\Tree;
9+
use Illuminate\Support\Arr;
910

1011
class JobGenerator extends StatementGenerator
1112
{
@@ -21,17 +22,19 @@ public function output(Tree $tree): array
2122
foreach ($tree->controllers() as $controller) {
2223
foreach ($controller->methods() as $method => $statements) {
2324
foreach ($statements as $statement) {
24-
if (!$statement instanceof DispatchStatement) {
25-
continue;
26-
}
25+
foreach (Arr::wrap($statement) as $statement) {
26+
if (!$statement instanceof DispatchStatement) {
27+
continue;
28+
}
2729

28-
$path = $this->getStatementPath($statement->job());
30+
$path = $this->getStatementPath($statement->job());
2931

30-
if ($this->filesystem->exists($path)) {
31-
continue;
32-
}
32+
if ($this->filesystem->exists($path)) {
33+
continue;
34+
}
3335

34-
$this->create($path, $this->populateStub($stub, $statement));
36+
$this->create($path, $this->populateStub($stub, $statement));
37+
}
3538
}
3639
}
3740
}

src/Generators/Statements/MailGenerator.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Blueprint\Generators\StatementGenerator;
77
use Blueprint\Models\Statements\SendStatement;
88
use Blueprint\Tree;
9+
use Illuminate\Support\Arr;
910

1011
class MailGenerator extends StatementGenerator
1112
{
@@ -22,27 +23,29 @@ public function output(Tree $tree): array
2223
foreach ($tree->controllers() as $controller) {
2324
foreach ($controller->methods() as $statements) {
2425
foreach ($statements as $statement) {
25-
if (!$statement instanceof SendStatement) {
26-
continue;
27-
}
26+
foreach (Arr::wrap($statement) as $statement) {
27+
if (!$statement instanceof SendStatement) {
28+
continue;
29+
}
2830

29-
if ($statement->type() !== SendStatement::TYPE_MAIL) {
30-
continue;
31-
}
31+
if ($statement->type() !== SendStatement::TYPE_MAIL) {
32+
continue;
33+
}
3234

33-
$path = $this->getStatementPath($statement->mail());
34-
if ($this->filesystem->exists($path)) {
35-
continue;
36-
}
35+
$path = $this->getStatementPath($statement->mail());
36+
if ($this->filesystem->exists($path)) {
37+
continue;
38+
}
3739

38-
$this->create($path, $this->populateStub($stub, $statement));
40+
$this->create($path, $this->populateStub($stub, $statement));
3941

40-
$path = $this->getViewPath($statement->view());
41-
if ($this->filesystem->exists($path)) {
42-
continue;
43-
}
42+
$path = $this->getViewPath($statement->view());
43+
if ($this->filesystem->exists($path)) {
44+
continue;
45+
}
4446

45-
$this->create($path, $this->populateViewStub($view_stub, $statement));
47+
$this->create($path, $this->populateViewStub($view_stub, $statement));
48+
}
4649
}
4750
}
4851
}

src/Generators/Statements/NotificationGenerator.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Blueprint\Generators\StatementGenerator;
77
use Blueprint\Models\Statements\SendStatement;
88
use Blueprint\Tree;
9+
use Illuminate\Support\Arr;
910

1011
class NotificationGenerator extends StatementGenerator
1112
{
@@ -21,21 +22,23 @@ public function output(Tree $tree): array
2122
foreach ($tree->controllers() as $controller) {
2223
foreach ($controller->methods() as $method => $statements) {
2324
foreach ($statements as $statement) {
24-
if (!$statement instanceof SendStatement) {
25-
continue;
26-
}
25+
foreach (Arr::wrap($statement) as $statement) {
26+
if (!$statement instanceof SendStatement) {
27+
continue;
28+
}
2729

28-
if (!$statement->isNotification()) {
29-
continue;
30-
}
30+
if (!$statement->isNotification()) {
31+
continue;
32+
}
3133

32-
$path = $this->getStatementPath($statement->mail());
34+
$path = $this->getStatementPath($statement->mail());
3335

34-
if ($this->filesystem->exists($path)) {
35-
continue;
36-
}
36+
if ($this->filesystem->exists($path)) {
37+
continue;
38+
}
3739

38-
$this->create($path, $this->populateStub($stub, $statement));
40+
$this->create($path, $this->populateStub($stub, $statement));
41+
}
3942
}
4043
}
4144
}

0 commit comments

Comments
 (0)