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

Wrap existing Services in Laravel Commands #22

Merged
merged 1 commit into from
Feb 27, 2024
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"require": {
"php": "^8.1.0",
"crell/api-problem": "^3.6.1",
"illuminate/console": "^9.0 || ^10.0",
"illuminate/http": "^9.0 || ^10.0",
"illuminate/support": "^9.0 || ^10.0",
"nyholm/psr7": "^1.8",
Expand Down
74 changes: 74 additions & 0 deletions src/Command/CacheOpenAPIProcessors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Membrane\Laravel\Command;

use Illuminate\Console\Command;
use Symfony\Component\Console\Logger\ConsoleLogger;

final class CacheOpenAPIProcessors extends Command
{
protected $signature = 'membrane:cache-openapi-routes' .
'{openapi : Absolute filepath of OpenAPI}' .
'{--destination= : Cache directory}' .
'{--namespace=Membrane\Cache}' .
'{--skip-requests}' .
'{--skip-responses}';

protected $description = <<<TEXT
Cache routes specified in OpenAPI file.

You may specify the cache --destination
Defaults to "<CWD>/cache/"

You may specify the cache --namespace
Defaults to "Membrane\Cache"

You may avoid caching requests with --skip-requests
You may avoid caching responses with --skip-responses
TEXT;

public function handle(): int
{
$openapi = $this->argument('openapi');

if (!is_string($openapi)) {
$this->error('openapi filepath MUST be a string');
return Command::FAILURE;
}

$destination = $this->option('destination') ??
getcwd() . '/cache';

if (!is_string($destination)) {
$this->error('destination MUST be a string');
return Command::FAILURE;
}

$namespace = $this->option('namespace');
if (!is_string($namespace)) {
$this->error('namespace MUST be a string');
return Command::FAILURE;
}

$buildRequests = !$this->option('skip-requests');
assert(is_bool($buildRequests));
$buildResponses = !$this->option('skip-responses');
assert(is_bool($buildResponses));

$service = new \Membrane\Console\Service\CacheOpenAPIProcessors(
new ConsoleLogger($this->output)
);

return $service->cache(
$openapi,
$destination,
$namespace,
$buildRequests,
$buildResponses,
) ?
Command::SUCCESS :
Command::FAILURE;
}
}
52 changes: 52 additions & 0 deletions src/Command/CacheOpenAPIRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Membrane\Laravel\Command;

use Illuminate\Console\Command;
use Membrane\OpenAPIRouter;
use Symfony\Component\Console\Logger\ConsoleLogger;

final class CacheOpenAPIRoutes extends Command
{
protected $signature = 'membrane:cache-openapi-routes' .
'{openapi : Absolute filepath of OpenAPI}' .
'{--destination= : Cache directory}';

protected $description = <<<TEXT
Cache routes specified in OpenAPI file.

You may specify a --destination
Defaults to "<CWD>/cache/"
TEXT;

public function handle(): int
{
$openapi = $this->argument('openapi');

if (!is_string($openapi)) {
$this->error('openapi filepath MUST be a string');
return Command::FAILURE;
}

$destination = $this->option('destination') ??
getcwd() . '/cache';

if (!is_string($destination)) {
$this->error('destination MUST be a string');
return Command::FAILURE;
}

$service = new OpenAPIRouter\Console\Service\CacheOpenAPIRoutes(
new ConsoleLogger($this->output)
);

return $service->cache(
$openapi,
rtrim($destination, '/') . '/routes.php',
) ?
Command::SUCCESS :
Command::FAILURE;
}
}
6 changes: 2 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace Membrane\Laravel;

use Membrane\Builder\Builder;
use Membrane\Console\Command\CacheOpenAPIProcessors;
use Membrane\Laravel\Middleware\RequestValidation;
use Membrane\Membrane;
use Membrane\OpenAPIRouter\Console\Commands\CacheOpenAPIRoutes;
use Membrane\OpenAPIRouter\Router\Router;
use Membrane\OpenAPIRouter\Router\ValueObject\RouteCollection;

Expand All @@ -27,8 +25,8 @@ public function boot(): void

if ($this->app->runningInConsole()) {
$this->commands([
CacheOpenAPIRoutes::class,
CacheOpenAPIProcessors::class
Command\CacheOpenAPIRoutes::class,
Command\CacheOpenAPIProcessors::class,
]);
}
}
Expand Down