diff --git a/composer.json b/composer.json index ae78177..76ff92f 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,12 @@ "illuminate/support": "^9.0 || ^10.0", "nyholm/psr7": "^1.8", "membrane/membrane": "^0.6.0", + "membrane/openapi-router": "0.2.1", "symfony/psr-http-message-bridge": "^2.1" }, "require-dev": { "phpunit/phpunit": "^10.3", - "phpstan/phpstan": "^1.8", + "phpstan/phpstan": "^1.10.56", "squizlabs/php_codesniffer": "^3.7" }, "extra": { diff --git a/config/membrane.php b/config/membrane.php index 032732f..ab241ff 100644 --- a/config/membrane.php +++ b/config/membrane.php @@ -26,4 +26,25 @@ 'validation_error_response_type' => 'about:blank', 'api_problem_response_types' => [], + /* + |-------------------------------------------------------------------------- + | + |-------------------------------------------------------------------------- + | + | + | + | + */ + 'routes_file' => base_path() . '/cache/routes.php', + + /* + |-------------------------------------------------------------------------- + | + |-------------------------------------------------------------------------- + | + | + | + | + */ + 'additional_builders' => [], ]; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 900a248..469bc9b 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,7 +4,13 @@ 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; class ServiceProvider extends \Illuminate\Support\ServiceProvider { @@ -15,6 +21,10 @@ public function boot(): void { /** @phpstan-ignore-next-line */ // config_path is a laravel framework helper method $this->publishes([self::CONFIG_PATH => config_path('membrane.php')], [self::CONFIG_NAME]); + + if ($this->app->runningInConsole()) { + $this->commands([CacheOpenAPIRoutes::class, CacheOpenAPIProcessors::class]); + } } public function register(): void @@ -34,5 +44,19 @@ public function register(): void $this->app->when(ApiProblemBuilder::class) ->needs('$apiProblemTypes') ->giveConfig('membrane.api_problem_response_types'); + + $this->app->when(Membrane::class) + ->needs('$builders') + ->give($this->instantiateBuilders()); + } + + /** @return Builder[] */ + private function instantiateBuilders(): array + { + /** @phpstan-ignore-next-line */ // config is a laravel framework helper method + $router = new Router(new RouteCollection(include config('membrane.routes_file'))); + + /** @phpstan-ignore-next-line */ // config is a laravel framework helper method + return array_map(fn($className) => new $className($router), config('membrane.additional_builders')); } }