Skip to content

Commit

Permalink
Provide Membrane with CachedBuilders
Browse files Browse the repository at this point in the history
  • Loading branch information
charjr committed Sep 26, 2023
1 parent 3ba3ed1 commit 561982d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +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\CacheOpenAPI;
use Membrane\OpenAPIRouter\Router\Router;
use Membrane\OpenAPIRouter\Router\ValueObject\RouteCollection;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
Expand All @@ -18,7 +22,9 @@ 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]);

$this->commands([CacheOpenAPI::class, CacheOpenAPIProcessors::class]);
if ($this->app->runningInConsole()) {
$this->commands([CacheOpenAPI::class, CacheOpenAPIProcessors::class]);
}
}

public function register(): void
Expand All @@ -38,5 +44,22 @@ 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
$routesFile = config('membrane.routes_file');

/** @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'));
}
}

0 comments on commit 561982d

Please sign in to comment.