Skip to content

Commit

Permalink
add: Tag behaviour overriding callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
WildEgo committed Mar 3, 2024
1 parent 407ab08 commit 51332b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Scramble.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Scramble

public static $routeResolver;

public static $tagResolver;

/**
* Update open api document before finally rendering it.
*/
Expand All @@ -24,6 +26,11 @@ public static function routes(callable $routeResolver)
static::$routeResolver = $routeResolver;
}

public static function tags(callable $tagResolver)
{
static::$tagResolver = $tagResolver;
}

/**
* @param array<string, ServerVariable> $variables
*/
Expand Down
13 changes: 9 additions & 4 deletions src/Support/OperationExtensions/RequestEssentialsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,17 +52,21 @@ 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(
collect($pathAliases)->keys()->map(fn ($k) => '{'.$k.'}')->all(),
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);

Expand Down

0 comments on commit 51332b2

Please sign in to comment.