Skip to content
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: 3 additions & 0 deletions config/openapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
'paths' => [
//
],
'components' => [
//
],
],

],
Expand Down
16 changes: 16 additions & 0 deletions docs/middlewares.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# Middlewares

Middlewares are an optional bit of logic to transform the given data at various lifecycle points.

### Path

To add a path middleware create a class that implements `\Vyuldashev\LaravelOpenApi\Contracts\PathMiddleware` then register it by referencing it in the `openapi.collections.default.middlewares.paths` config array like `MyPathMiddleware::class`

Available lifecycle points are:
- `before` - after collecting all `RouteInformation` but before processing them.
- `after` - after the `PathItem` has been built.

### Component

To add a path middleware create a class that implements `\Vyuldashev\LaravelOpenApi\Contracts\ComponentMiddleware` then register it by referencing it in the `openapi.collections.default.middlewares.components` config array like `MyComponentMiddleware::class`

Available lifecycle points are:
- `after` - after the `Components` has been built.
16 changes: 13 additions & 3 deletions src/Builders/ComponentsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ public function __construct(
$this->securitySchemesBuilder = $securitySchemesBuilder;
}

public function build(string $collection = Generator::COLLECTION_DEFAULT): ?Components
{
public function build(
string $collection = Generator::COLLECTION_DEFAULT,
array $middlewares = []
): ?Components {
$callbacks = $this->callbacksBuilder->build($collection);
$requestBodies = $this->requestBodiesBuilder->build($collection);
$responses = $this->responsesBuilder->build($collection);
Expand Down Expand Up @@ -71,6 +73,14 @@ public function build(string $collection = Generator::COLLECTION_DEFAULT): ?Comp
$components = $components->securitySchemes(...$securitySchemes);
}

return $hasAnyObjects ? $components : null;
if (! $hasAnyObjects) {
return null;
}

foreach ($middlewares as $middleware) {
app($middleware)->after($components);
}

return $components;
}
}
10 changes: 10 additions & 0 deletions src/Contracts/ComponentMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Vyuldashev\LaravelOpenApi\Contracts;

use GoldSpecDigital\ObjectOrientedOAS\Objects\Components;

interface ComponentMiddleware
{
public function after(Components $components): void;
}
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function generate(string $collection = self::COLLECTION_DEFAULT): OpenApi
$servers = $this->serversBuilder->build(Arr::get($this->config, 'collections.'.$collection.'.servers', []));
$tags = $this->tagsBuilder->build(Arr::get($this->config, 'collections.'.$collection.'.tags', []));
$paths = $this->pathsBuilder->build($collection, Arr::get($middlewares, 'paths', []));
$components = $this->componentsBuilder->build($collection);
$components = $this->componentsBuilder->build($collection, Arr::get($middlewares, 'components', []));

return OpenApi::create()
->openapi(OpenApi::OPENAPI_3_0_2)
Expand Down