Skip to content

Commit e6f9cb1

Browse files
author
Brian Faust
authored
Support multiple dispatch, fire, notify, and send statements (#623)
1 parent 4ae5492 commit e6f9cb1

File tree

10 files changed

+473
-380
lines changed

10 files changed

+473
-380
lines changed

src/Blueprint.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function parse($content, $strip_dashes = true)
3838
$content = preg_replace('/^(\s*)-\s*/m', '\1', $content);
3939
}
4040

41+
$content = $this->transformDuplicatePropertyKeys($content, ['dispatch', 'fire', 'notify', 'send']);
42+
4143
$content = preg_replace_callback(
4244
'/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi',
4345
fn ($matches) => $matches[1] . strtolower($matches[2]) . ': ' . $matches[2],
@@ -136,4 +138,44 @@ protected function shouldGenerate(array $types, array $only, array $skip): bool
136138

137139
return true;
138140
}
141+
142+
private function transformDuplicatePropertyKeys(string $content, array $properties): string
143+
{
144+
$contentArray = explode("\n", $content);
145+
146+
foreach ($properties as $property) {
147+
if (!str_contains($content, $property)) {
148+
continue;
149+
}
150+
151+
preg_match_all(
152+
sprintf('/[^\S\r\n]*%s: .*/', $property),
153+
$content,
154+
$lines,
155+
PREG_OFFSET_CAPTURE
156+
);
157+
158+
if (count($lines) === 0) {
159+
return $content;
160+
}
161+
162+
if (count($lines[0]) <= 1) {
163+
return $content;
164+
}
165+
166+
foreach ($lines[0] as $line) {
167+
$lineNumber = count(explode("\n", mb_substr($content, 0, $line[1])));
168+
169+
$replacement = str_replace(
170+
$property . ':',
171+
sprintf('"%s-%s":', $property, $lineNumber, trim($line[0])),
172+
$line[0]
173+
);
174+
175+
$contentArray[$lineNumber - 1] = $replacement;
176+
}
177+
}
178+
179+
return implode("\n", $contentArray);
180+
}
139181
}

src/Generators/ControllerGenerator.php

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

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

8281
foreach ($statements as $statement) {
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());
92-
}
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()));
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());
143108
}
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()));
125+
}
144126

145-
$body .= PHP_EOL;
127+
$body = str_replace('::all();', '::paginate();', $body);
128+
}
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()));
146141
}
142+
143+
$body .= PHP_EOL;
147144
}
148145

149146
if (!empty($body)) {

src/Generators/Statements/EventGenerator.php

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

1110
class EventGenerator extends StatementGenerator
1211
{
@@ -22,23 +21,21 @@ public function output(Tree $tree): array
2221
foreach ($tree->controllers() as $controller) {
2322
foreach ($controller->methods() as $method => $statements) {
2423
foreach ($statements as $statement) {
25-
foreach (Arr::wrap($statement) as $statement) {
26-
if (!$statement instanceof FireStatement) {
27-
continue;
28-
}
29-
30-
if ($statement->isNamedEvent()) {
31-
continue;
32-
}
24+
if (!$statement instanceof FireStatement) {
25+
continue;
26+
}
3327

34-
$path = $this->getStatementPath($statement->event());
28+
if ($statement->isNamedEvent()) {
29+
continue;
30+
}
3531

36-
if ($this->filesystem->exists($path)) {
37-
continue;
38-
}
32+
$path = $this->getStatementPath($statement->event());
3933

40-
$this->create($path, $this->populateStub($stub, $statement));
34+
if ($this->filesystem->exists($path)) {
35+
continue;
4136
}
37+
38+
$this->create($path, $this->populateStub($stub, $statement));
4239
}
4340
}
4441
}

src/Generators/Statements/JobGenerator.php

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

1110
class JobGenerator extends StatementGenerator
1211
{
@@ -22,19 +21,17 @@ public function output(Tree $tree): array
2221
foreach ($tree->controllers() as $controller) {
2322
foreach ($controller->methods() as $method => $statements) {
2423
foreach ($statements as $statement) {
25-
foreach (Arr::wrap($statement) as $statement) {
26-
if (!$statement instanceof DispatchStatement) {
27-
continue;
28-
}
29-
30-
$path = $this->getStatementPath($statement->job());
24+
if (!$statement instanceof DispatchStatement) {
25+
continue;
26+
}
3127

32-
if ($this->filesystem->exists($path)) {
33-
continue;
34-
}
28+
$path = $this->getStatementPath($statement->job());
3529

36-
$this->create($path, $this->populateStub($stub, $statement));
30+
if ($this->filesystem->exists($path)) {
31+
continue;
3732
}
33+
34+
$this->create($path, $this->populateStub($stub, $statement));
3835
}
3936
}
4037
}

src/Generators/Statements/MailGenerator.php

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

1110
class MailGenerator extends StatementGenerator
1211
{
@@ -23,29 +22,27 @@ public function output(Tree $tree): array
2322
foreach ($tree->controllers() as $controller) {
2423
foreach ($controller->methods() as $statements) {
2524
foreach ($statements as $statement) {
26-
foreach (Arr::wrap($statement) as $statement) {
27-
if (!$statement instanceof SendStatement) {
28-
continue;
29-
}
30-
31-
if ($statement->type() !== SendStatement::TYPE_MAIL) {
32-
continue;
33-
}
25+
if (!$statement instanceof SendStatement) {
26+
continue;
27+
}
3428

35-
$path = $this->getStatementPath($statement->mail());
36-
if ($this->filesystem->exists($path)) {
37-
continue;
38-
}
29+
if ($statement->type() !== SendStatement::TYPE_MAIL) {
30+
continue;
31+
}
3932

40-
$this->create($path, $this->populateStub($stub, $statement));
33+
$path = $this->getStatementPath($statement->mail());
34+
if ($this->filesystem->exists($path)) {
35+
continue;
36+
}
4137

42-
$path = $this->getViewPath($statement->view());
43-
if ($this->filesystem->exists($path)) {
44-
continue;
45-
}
38+
$this->create($path, $this->populateStub($stub, $statement));
4639

47-
$this->create($path, $this->populateViewStub($view_stub, $statement));
40+
$path = $this->getViewPath($statement->view());
41+
if ($this->filesystem->exists($path)) {
42+
continue;
4843
}
44+
45+
$this->create($path, $this->populateViewStub($view_stub, $statement));
4946
}
5047
}
5148
}

src/Generators/Statements/NotificationGenerator.php

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

1110
class NotificationGenerator extends StatementGenerator
1211
{
@@ -22,23 +21,21 @@ public function output(Tree $tree): array
2221
foreach ($tree->controllers() as $controller) {
2322
foreach ($controller->methods() as $method => $statements) {
2423
foreach ($statements as $statement) {
25-
foreach (Arr::wrap($statement) as $statement) {
26-
if (!$statement instanceof SendStatement) {
27-
continue;
28-
}
29-
30-
if (!$statement->isNotification()) {
31-
continue;
32-
}
24+
if (!$statement instanceof SendStatement) {
25+
continue;
26+
}
3327

34-
$path = $this->getStatementPath($statement->mail());
28+
if (!$statement->isNotification()) {
29+
continue;
30+
}
3531

36-
if ($this->filesystem->exists($path)) {
37-
continue;
38-
}
32+
$path = $this->getStatementPath($statement->mail());
3933

40-
$this->create($path, $this->populateStub($stub, $statement));
34+
if ($this->filesystem->exists($path)) {
35+
continue;
4136
}
37+
38+
$this->create($path, $this->populateStub($stub, $statement));
4239
}
4340
}
4441
}

0 commit comments

Comments
 (0)