Skip to content

Commit 52469ae

Browse files
committed
Add components middleware
1 parent 12d1514 commit 52469ae

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

config/openapi.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
'paths' => [
4747
//
4848
],
49+
'components' => [
50+
//
51+
]
4952
],
5053

5154
],

docs/middlewares.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ To add a path middleware create a class that implements `\Vyuldashev\LaravelOpen
99
Available lifecycle points are:
1010
- `before` - after collecting all `RouteInformation` but before processing them.
1111
- `after` - after the `PathItem` has been built.
12+
13+
### Component
14+
15+
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`
16+
17+
Available lifecycle points are:
18+
- `after` - after the `Components` has been built.

src/Builders/ComponentsBuilder.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public function __construct(
3232
$this->securitySchemesBuilder = $securitySchemesBuilder;
3333
}
3434

35-
public function build(string $collection = Generator::COLLECTION_DEFAULT): ?Components
35+
public function build(
36+
string $collection = Generator::COLLECTION_DEFAULT,
37+
array $middlewares = []
38+
): ?Components
3639
{
3740
$callbacks = $this->callbacksBuilder->build($collection);
3841
$requestBodies = $this->requestBodiesBuilder->build($collection);
@@ -71,6 +74,14 @@ public function build(string $collection = Generator::COLLECTION_DEFAULT): ?Comp
7174
$components = $components->securitySchemes(...$securitySchemes);
7275
}
7376

74-
return $hasAnyObjects ? $components : null;
77+
if (!$hasAnyObjects) {
78+
return null;
79+
}
80+
81+
foreach ($middlewares as $middleware) {
82+
app($middleware)->after($components);
83+
}
84+
85+
return $components;
7586
}
7687
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Vyuldashev\LaravelOpenApi\Contracts;
4+
5+
use GoldSpecDigital\ObjectOrientedOAS\Objects\Components;
6+
7+
interface ComponentMiddleware
8+
{
9+
public function after(Components $components): void;
10+
}

src/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function generate(string $collection = self::COLLECTION_DEFAULT): OpenApi
4747
$servers = $this->serversBuilder->build(Arr::get($this->config, 'collections.'.$collection.'.servers', []));
4848
$tags = $this->tagsBuilder->build(Arr::get($this->config, 'collections.'.$collection.'.tags', []));
4949
$paths = $this->pathsBuilder->build($collection, Arr::get($middlewares, 'paths', []));
50-
$components = $this->componentsBuilder->build($collection);
50+
$components = $this->componentsBuilder->build($collection, Arr::get($middlewares, 'components', []));
5151

5252
return OpenApi::create()
5353
->openapi(OpenApi::OPENAPI_3_0_2)

0 commit comments

Comments
 (0)