Skip to content

Commit 40e0b0a

Browse files
authored
Merge pull request #55 from nandi95/feature/middlewares
Add components middleware
2 parents 263da65 + 4c24436 commit 40e0b0a

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
# Middlewares
22

3+
Middlewares are an optional bit of logic to transform the given data at various lifecycle points.
4+
5+
### Path
6+
7+
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`
8+
9+
Available lifecycle points are:
10+
- `before` - after collecting all `RouteInformation` but before processing them.
11+
- `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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public function __construct(
3232
$this->securitySchemesBuilder = $securitySchemesBuilder;
3333
}
3434

35-
public function build(string $collection = Generator::COLLECTION_DEFAULT): ?Components
36-
{
35+
public function build(
36+
string $collection = Generator::COLLECTION_DEFAULT,
37+
array $middlewares = []
38+
): ?Components {
3739
$callbacks = $this->callbacksBuilder->build($collection);
3840
$requestBodies = $this->requestBodiesBuilder->build($collection);
3941
$responses = $this->responsesBuilder->build($collection);
@@ -71,6 +73,14 @@ public function build(string $collection = Generator::COLLECTION_DEFAULT): ?Comp
7173
$components = $components->securitySchemes(...$securitySchemes);
7274
}
7375

74-
return $hasAnyObjects ? $components : null;
76+
if (! $hasAnyObjects) {
77+
return null;
78+
}
79+
80+
foreach ($middlewares as $middleware) {
81+
app($middleware)->after($components);
82+
}
83+
84+
return $components;
7585
}
7686
}
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)