Skip to content

Commit

Permalink
Merge pull request #22 from membrane-php/laravel-commands
Browse files Browse the repository at this point in the history
Wrap existing Services in Laravel Commands
  • Loading branch information
carnage authored Feb 27, 2024
2 parents ede6935 + d6f8849 commit c13fb31
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
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

0 comments on commit c13fb31

Please sign in to comment.