Skip to content

Commit b7246d9

Browse files
Pridat moznost vyfiltrovat endpointy, ktore sa zobrazia v open api (#158)
1 parent f040a9a commit b7246d9

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
All notable changes to this project will be documented in this file.
33
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
44

5+
## [Unreleased][unreleased]
6+
### Added
7+
* Allowed endpoints filter for OpenApiHandler
8+
59
## 3.3.0
610

7-
## [Unreleased][unreleased]
811
### Changed
912
* update minimum PHP to version 8.2 (currently still supported version) + update league/fractal library [BC]
1013
* change Inject from PHPDoc format to native PHP attribute

src/Handlers/OpenApiHandler.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,28 @@ class OpenApiHandler extends BaseHandler
4646
/** @var array<string,mixed> */
4747
private $definitions = [];
4848

49+
/** @var array<string|int, mixed> */
50+
private $allowedEndpoints = [];
51+
4952
/**
5053
* OpenApiHandler constructor.
5154
* @param array<string,mixed> $initData - structured data for initialization response
55+
* @param array<string|int, mixed> $allowedEndpoints - list of endpoint (path and methods) which should be included in output, empty means all, wildcard * is supported for path
56+
* Example: ['user/*' => ['get', 'post', 'delete'], 'user/list' => 'post', 'user/list' => [], 'user/list']
5257
*/
5358
public function __construct(
5459
ApiDecider $apiDecider,
5560
ApiLink $apiLink,
5661
Request $request,
57-
array $initData = []
62+
array $initData = [],
63+
array $allowedEndpoints = [],
5864
) {
5965
parent::__construct();
6066
$this->apiDecider = $apiDecider;
6167
$this->apiLink = $apiLink;
6268
$this->request = $request;
6369
$this->initData = $initData;
70+
$this->allowedEndpoints = $allowedEndpoints;
6471
}
6572

6673
/**
@@ -259,6 +266,10 @@ private function getPaths(array $versionApis, string $baseUrl, string $basePath)
259266
$handler = $api->getHandler();
260267
$path = str_replace([$baseUrl, $basePath], '', $this->apiLink->link($api->getEndpoint()));
261268
$responses = [];
269+
$endpointPath = str_replace('v' . $api->getEndpoint()->getVersion() . '/', '', $api->getEndpoint()->getUrl());
270+
if (!$this->isPathAllowed($endpointPath, $api->getEndpoint()->getMethod())) {
271+
continue;
272+
}
262273

263274
$settings = [
264275
'summary' => $handler->summary(),
@@ -412,6 +423,39 @@ private function getPaths(array $versionApis, string $baseUrl, string $basePath)
412423
return $list;
413424
}
414425

426+
private function isPathAllowed(string $path, string $method): bool
427+
{
428+
if (empty($this->allowedEndpoints)) {
429+
return true;
430+
}
431+
432+
foreach ($this->allowedEndpoints as $allowedPath => $allowedMethods) {
433+
if (is_int($allowedPath)) { // ['user/list'] format
434+
$pattern = str_replace('*', '.*?', preg_quote(strtolower($allowedMethods)));
435+
if (preg_match("#$pattern#", strtolower($path))) {
436+
return true;
437+
}
438+
439+
continue;
440+
}
441+
442+
// ['user/*' => ['get', 'post'], 'user/list' => 'post', 'user/list' => [], 'user/list' => '*'] format
443+
$pattern = str_replace('*', '.*?', preg_quote(strtolower($allowedPath)));
444+
if (preg_match("#$pattern#", strtolower($path))) {
445+
if (empty($allowedMethods)) {
446+
return true;
447+
}
448+
if (is_string($allowedMethods) && strtolower($allowedMethods) === strtolower($method)) {
449+
return true;
450+
}
451+
if (is_array($allowedMethods) && in_array(strtolower($method), array_map('strtolower', $allowedMethods), true)) {
452+
return true;
453+
}
454+
}
455+
}
456+
return false;
457+
}
458+
415459
/**
416460
* @param Api[] $apis
417461
*/

0 commit comments

Comments
 (0)