-
Notifications
You must be signed in to change notification settings - Fork 937
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
PathFilter
processor to filter paths (#1613)
- Loading branch information
1 parent
5eb7a90
commit b308ec0
Showing
8 changed files
with
235 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Processors\Concerns; | ||
|
||
use OpenApi\Annotations as OA; | ||
|
||
trait AnnotationTrait | ||
{ | ||
/** | ||
* Collects a (complete) list of all nested/referenced annotations starting from the given root. | ||
* | ||
* @param string|array|iterable|OA\AbstractAnnotation $root | ||
*/ | ||
public function collectAnnotations($root): \SplObjectStorage | ||
{ | ||
$storage = new \SplObjectStorage(); | ||
|
||
$this->traverseAnnotations($root, function ($item) use (&$storage) { | ||
if ($item instanceof OA\AbstractAnnotation && !$storage->contains($item)) { | ||
$storage->attach($item); | ||
} | ||
}); | ||
|
||
return $storage; | ||
} | ||
|
||
/** | ||
* Remove all annotations that are part of the `$annotation` tree. | ||
*/ | ||
public function removeAnnotation(iterable $root, OA\AbstractAnnotation $annotation): void | ||
{ | ||
$remove = $this->collectAnnotations($annotation); | ||
$this->traverseAnnotations($root, function ($item) use ($remove) { | ||
if ($item instanceof \SplObjectStorage) { | ||
foreach ($remove as $annotation) { | ||
$item->detach($annotation); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param string|array|iterable|OA\AbstractAnnotation $root | ||
*/ | ||
public function traverseAnnotations($root, callable $callable): void | ||
{ | ||
$callable($root); | ||
|
||
if (is_iterable($root)) { | ||
foreach ($root as $value) { | ||
$this->traverseAnnotations($value, $callable); | ||
} | ||
} elseif ($root instanceof OA\AbstractAnnotation) { | ||
foreach (array_merge($root::$_nested, ['allOf', 'anyOf', 'oneOf', 'callbacks']) as $properties) { | ||
foreach ((array) $properties as $property) { | ||
if (isset($root->{$property})) { | ||
$this->traverseAnnotations($root->{$property}, $callable); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Processors; | ||
|
||
use OpenApi\Analysis; | ||
use OpenApi\Generator; | ||
use OpenApi\Processors\Concerns\AnnotationTrait; | ||
|
||
/** | ||
* Allows to filter endpoints based on tags and/or path. | ||
* | ||
* If no `tags` or `paths` filters are set, no filtering is performed. | ||
* All filter (regular) expressions must be enclosed within delimiter characters as they are used as-is. | ||
*/ | ||
class PathFilter implements ProcessorInterface | ||
{ | ||
use AnnotationTrait; | ||
|
||
/** @var array */ | ||
protected $tags = []; | ||
|
||
/** @var array */ | ||
protected $paths = []; | ||
|
||
public function __construct(array $tags = [], array $paths = []) | ||
{ | ||
$this->tags = $tags; | ||
$this->paths = $paths; | ||
} | ||
|
||
public function getTags(): array | ||
{ | ||
return $this->tags; | ||
} | ||
|
||
/** | ||
* A list of regular expressions to match <code>tags</code> to include. | ||
* | ||
* @param array<string> $tags | ||
*/ | ||
public function setTags(array $tags): PathFilter | ||
{ | ||
$this->tags = $tags; | ||
|
||
return $this; | ||
} | ||
|
||
public function getPaths(): array | ||
{ | ||
return $this->paths; | ||
} | ||
|
||
/** | ||
* A list of regular expressions to match <code>paths</code> to include. | ||
* | ||
* @param array<string> $paths | ||
*/ | ||
public function setPaths(array $paths): PathFilter | ||
{ | ||
$this->paths = $paths; | ||
|
||
return $this; | ||
} | ||
|
||
public function __invoke(Analysis $analysis) | ||
{ | ||
if (($this->tags || $this->paths) && !Generator::isDefault($analysis->openapi->paths)) { | ||
$filtered = []; | ||
foreach ($analysis->openapi->paths as $pathItem) { | ||
$matched = null; | ||
foreach ($this->tags as $pattern) { | ||
foreach ($pathItem->operations() as $operation) { | ||
if (!Generator::isDefault($operation->tags)) { | ||
foreach ($operation->tags as $tag) { | ||
if (preg_match($pattern, $tag)) { | ||
$matched = $pathItem; | ||
break 3; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
foreach ($this->paths as $pattern) { | ||
if (preg_match($pattern, $pathItem->path)) { | ||
$matched = $pathItem; | ||
break; | ||
} | ||
} | ||
|
||
if ($matched) { | ||
$filtered[] = $matched; | ||
} else { | ||
$this->removeAnnotation($analysis->annotations, $pathItem); | ||
} | ||
} | ||
|
||
$analysis->openapi->paths = $filtered; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters