Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for iterating over statement children #619

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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