Skip to content

Commit

Permalink
Add support for iterating over statement children (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored May 5, 2023
1 parent 032aa5d commit 1b20a79
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 358 deletions.
121 changes: 62 additions & 59 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Blueprint\Models\Statements\SessionStatement;
use Blueprint\Models\Statements\ValidateStatement;
use Blueprint\Tree;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

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

foreach ($statements as $statement) {
if ($statement instanceof SendStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
if ($statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_FACADE) {
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Notification');
$this->addImport($controller, config('blueprint.namespace') . '\\Notification\\' . $statement->mail());
} elseif ($statement->type() === SendStatement::TYPE_MAIL) {
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Mail');
$this->addImport($controller, config('blueprint.namespace') . '\\Mail\\' . $statement->mail());
}
} elseif ($statement instanceof ValidateStatement) {
$using_validation = true;
$class_name = $controller->name() . Str::studly($name) . 'Request';

$fqcn = config('blueprint.namespace') . '\\Http\\Requests\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $class_name;

$method = str_replace('\Illuminate\Http\Request $request', '\\' . $fqcn . ' $request', $method);
$method = str_replace('(Request $request', '(' . $class_name . ' $request', $method);

$this->addImport($controller, $fqcn);
} elseif ($statement instanceof DispatchStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
$this->addImport($controller, config('blueprint.namespace') . '\\Jobs\\' . $statement->job());
} elseif ($statement instanceof FireStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
if (!$statement->isNamedEvent()) {
$this->addImport($controller, config('blueprint.namespace') . '\\Events\\' . $statement->event());
}
} elseif ($statement instanceof RenderStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof ResourceStatement) {
$fqcn = config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name();
$this->addImport($controller, $fqcn);
$body .= self::INDENT . $statement->output() . PHP_EOL;

if ($statement->paginate()) {
if (!Str::contains($body, '::all();')) {
$queryStatement = new QueryStatement('all', [$statement->reference()]);
$body = implode(PHP_EOL, [
self::INDENT . $queryStatement->output($statement->reference()),
PHP_EOL . $body,
]);

$this->addImport($controller, $this->determineModel($controller, $queryStatement->model()));
foreach (Arr::wrap($statement) as $statement) {
if ($statement instanceof SendStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
if ($statement->type() === SendStatement::TYPE_NOTIFICATION_WITH_FACADE) {
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Notification');
$this->addImport($controller, config('blueprint.namespace') . '\\Notification\\' . $statement->mail());
} elseif ($statement->type() === SendStatement::TYPE_MAIL) {
$this->addImport($controller, 'Illuminate\\Support\\Facades\\Mail');
$this->addImport($controller, config('blueprint.namespace') . '\\Mail\\' . $statement->mail());
}

$body = str_replace('::all();', '::paginate();', $body);
} elseif ($statement instanceof ValidateStatement) {
$using_validation = true;
$class_name = $controller->name() . Str::studly($name) . 'Request';

$fqcn = config('blueprint.namespace') . '\\Http\\Requests\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $class_name;

$method = str_replace('\Illuminate\Http\Request $request', '\\' . $fqcn . ' $request', $method);
$method = str_replace('(Request $request', '(' . $class_name . ' $request', $method);

$this->addImport($controller, $fqcn);
} elseif ($statement instanceof DispatchStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
$this->addImport($controller, config('blueprint.namespace') . '\\Jobs\\' . $statement->job());
} elseif ($statement instanceof FireStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
if (!$statement->isNamedEvent()) {
$this->addImport($controller, config('blueprint.namespace') . '\\Events\\' . $statement->event());
}
} elseif ($statement instanceof RenderStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof ResourceStatement) {
$fqcn = config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name();
$this->addImport($controller, $fqcn);
$body .= self::INDENT . $statement->output() . PHP_EOL;

if ($statement->paginate()) {
if (!Str::contains($body, '::all();')) {
$queryStatement = new QueryStatement('all', [$statement->reference()]);
$body = implode(PHP_EOL, [
self::INDENT . $queryStatement->output($statement->reference()),
PHP_EOL . $body,
]);

$this->addImport($controller, $this->determineModel($controller, $queryStatement->model()));
}

$body = str_replace('::all();', '::paginate();', $body);
}
} elseif ($statement instanceof RedirectStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof RespondStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof SessionStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof EloquentStatement) {
$body .= self::INDENT . $statement->output($controller->prefix(), $name, $using_validation) . PHP_EOL;
$this->addImport($controller, $this->determineModel($controller, $statement->reference()));
} elseif ($statement instanceof QueryStatement) {
$body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL;
$this->addImport($controller, $this->determineModel($controller, $statement->model()));
}
} elseif ($statement instanceof RedirectStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof RespondStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof SessionStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof EloquentStatement) {
$body .= self::INDENT . $statement->output($controller->prefix(), $name, $using_validation) . PHP_EOL;
$this->addImport($controller, $this->determineModel($controller, $statement->reference()));
} elseif ($statement instanceof QueryStatement) {
$body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL;
$this->addImport($controller, $this->determineModel($controller, $statement->model()));
}

$body .= PHP_EOL;
$body .= PHP_EOL;
}
}

if (!empty($body)) {
Expand Down
25 changes: 14 additions & 11 deletions src/Generators/Statements/EventGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Blueprint\Generators\StatementGenerator;
use Blueprint\Models\Statements\FireStatement;
use Blueprint\Tree;
use Illuminate\Support\Arr;

class EventGenerator extends StatementGenerator
{
Expand All @@ -21,21 +22,23 @@ public function output(Tree $tree): array
foreach ($tree->controllers() as $controller) {
foreach ($controller->methods() as $method => $statements) {
foreach ($statements as $statement) {
if (!$statement instanceof FireStatement) {
continue;
}
foreach (Arr::wrap($statement) as $statement) {
if (!$statement instanceof FireStatement) {
continue;
}

if ($statement->isNamedEvent()) {
continue;
}
if ($statement->isNamedEvent()) {
continue;
}

$path = $this->getStatementPath($statement->event());
$path = $this->getStatementPath($statement->event());

if ($this->filesystem->exists($path)) {
continue;
}
if ($this->filesystem->exists($path)) {
continue;
}

$this->create($path, $this->populateStub($stub, $statement));
$this->create($path, $this->populateStub($stub, $statement));
}
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/Generators/Statements/JobGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Blueprint\Generators\StatementGenerator;
use Blueprint\Models\Statements\DispatchStatement;
use Blueprint\Tree;
use Illuminate\Support\Arr;

class JobGenerator extends StatementGenerator
{
Expand All @@ -21,17 +22,19 @@ public function output(Tree $tree): array
foreach ($tree->controllers() as $controller) {
foreach ($controller->methods() as $method => $statements) {
foreach ($statements as $statement) {
if (!$statement instanceof DispatchStatement) {
continue;
}
foreach (Arr::wrap($statement) as $statement) {
if (!$statement instanceof DispatchStatement) {
continue;
}

$path = $this->getStatementPath($statement->job());
$path = $this->getStatementPath($statement->job());

if ($this->filesystem->exists($path)) {
continue;
}
if ($this->filesystem->exists($path)) {
continue;
}

$this->create($path, $this->populateStub($stub, $statement));
$this->create($path, $this->populateStub($stub, $statement));
}
}
}
}
Expand Down
35 changes: 19 additions & 16 deletions src/Generators/Statements/MailGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Blueprint\Generators\StatementGenerator;
use Blueprint\Models\Statements\SendStatement;
use Blueprint\Tree;
use Illuminate\Support\Arr;

class MailGenerator extends StatementGenerator
{
Expand All @@ -22,27 +23,29 @@ public function output(Tree $tree): array
foreach ($tree->controllers() as $controller) {
foreach ($controller->methods() as $statements) {
foreach ($statements as $statement) {
if (!$statement instanceof SendStatement) {
continue;
}
foreach (Arr::wrap($statement) as $statement) {
if (!$statement instanceof SendStatement) {
continue;
}

if ($statement->type() !== SendStatement::TYPE_MAIL) {
continue;
}
if ($statement->type() !== SendStatement::TYPE_MAIL) {
continue;
}

$path = $this->getStatementPath($statement->mail());
if ($this->filesystem->exists($path)) {
continue;
}
$path = $this->getStatementPath($statement->mail());
if ($this->filesystem->exists($path)) {
continue;
}

$this->create($path, $this->populateStub($stub, $statement));
$this->create($path, $this->populateStub($stub, $statement));

$path = $this->getViewPath($statement->view());
if ($this->filesystem->exists($path)) {
continue;
}
$path = $this->getViewPath($statement->view());
if ($this->filesystem->exists($path)) {
continue;
}

$this->create($path, $this->populateViewStub($view_stub, $statement));
$this->create($path, $this->populateViewStub($view_stub, $statement));
}
}
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/Generators/Statements/NotificationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Blueprint\Generators\StatementGenerator;
use Blueprint\Models\Statements\SendStatement;
use Blueprint\Tree;
use Illuminate\Support\Arr;

class NotificationGenerator extends StatementGenerator
{
Expand All @@ -21,21 +22,23 @@ public function output(Tree $tree): array
foreach ($tree->controllers() as $controller) {
foreach ($controller->methods() as $method => $statements) {
foreach ($statements as $statement) {
if (!$statement instanceof SendStatement) {
continue;
}
foreach (Arr::wrap($statement) as $statement) {
if (!$statement instanceof SendStatement) {
continue;
}

if (!$statement->isNotification()) {
continue;
}
if (!$statement->isNotification()) {
continue;
}

$path = $this->getStatementPath($statement->mail());
$path = $this->getStatementPath($statement->mail());

if ($this->filesystem->exists($path)) {
continue;
}
if ($this->filesystem->exists($path)) {
continue;
}

$this->create($path, $this->populateStub($stub, $statement));
$this->create($path, $this->populateStub($stub, $statement));
}
}
}
}
Expand Down
Loading

0 comments on commit 1b20a79

Please sign in to comment.