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 CachedBuilders and RoutesFile to config #8

Merged
merged 1 commit into from
Jan 16, 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
21 changes: 21 additions & 0 deletions config/membrane.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,25 @@
'validation_error_response_type' => 'about:blank',
'api_problem_response_types' => [],

/*
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
|
|
|
*/
'routes_file' => base_path() . '/cache/routes.php',

/*
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
|
|
|
*/
'additional_builders' => [],
];
24 changes: 24 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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'));
}
}