From 51332b2975c3125e4a5ab8a5c5318885c3b2d49b Mon Sep 17 00:00:00 2001 From: WildEgo Date: Sun, 3 Mar 2024 18:13:34 +0000 Subject: [PATCH] add: Tag behaviour overriding callback. --- src/Scramble.php | 7 +++++++ .../RequestEssentialsExtension.php | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Scramble.php b/src/Scramble.php index 3f562f2d..0d22971d 100755 --- a/src/Scramble.php +++ b/src/Scramble.php @@ -11,6 +11,8 @@ class Scramble public static $routeResolver; + public static $tagResolver; + /** * Update open api document before finally rendering it. */ @@ -24,6 +26,11 @@ public static function routes(callable $routeResolver) static::$routeResolver = $routeResolver; } + public static function tags(callable $tagResolver) + { + static::$tagResolver = $tagResolver; + } + /** * @param array $variables */ diff --git a/src/Support/OperationExtensions/RequestEssentialsExtension.php b/src/Support/OperationExtensions/RequestEssentialsExtension.php index 983306a9..d6bcd7d7 100644 --- a/src/Support/OperationExtensions/RequestEssentialsExtension.php +++ b/src/Support/OperationExtensions/RequestEssentialsExtension.php @@ -4,6 +4,7 @@ use Dedoc\Scramble\Extensions\OperationExtension; use Dedoc\Scramble\Infer; +use Dedoc\Scramble\Scramble; use Dedoc\Scramble\Support\Generator\OpenApi; use Dedoc\Scramble\Support\Generator\Operation; use Dedoc\Scramble\Support\Generator\Parameter; @@ -51,6 +52,13 @@ public function handle(Operation $operation, RouteInfo $routeInfo) { [$pathParams, $pathAliases] = $this->getRoutePathParameters($routeInfo->route, $routeInfo->phpDoc()); + $tagResolver = Scramble::$tagResolver ?? function (RouteInfo $routeInfo) { + return array_unique([ + ...$this->extractTagsForMethod($routeInfo), + Str::of(class_basename($routeInfo->className()))->replace('Controller', ''), + ]); + }; + $operation ->setMethod(strtolower($routeInfo->route->methods()[0])) ->setPath(Str::replace( @@ -58,10 +66,7 @@ public function handle(Operation $operation, RouteInfo $routeInfo) collect($pathAliases)->values()->map(fn ($v) => '{'.$v.'}')->all(), $routeInfo->route->uri, )) - ->setTags(array_unique([ - ...$this->extractTagsForMethod($routeInfo), - Str::of(class_basename($routeInfo->className()))->replace('Controller', ''), - ])) + ->setTags($tagResolver($routeInfo)) ->servers($this->getAlternativeServers($routeInfo->route)) ->addParameters($pathParams);