Last active
March 1, 2025 20:57
-
-
Save willvincent/ecc18941a0aeb70623924642959514a1 to your computer and use it in GitHub Desktop.
Laravel Scramble - Arbitrary Request Headers
This file contains hidden or 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
<?php | |
namespace ...; | |
use Dedoc\Scramble\Extensions\OperationExtension; | |
use Dedoc\Scramble\Support\Generator\Operation; | |
use Dedoc\Scramble\Support\Generator\Parameter; | |
use Dedoc\Scramble\Support\Generator\Schema; | |
use Dedoc\Scramble\Support\Generator\Types\StringType; | |
use Dedoc\Scramble\Support\RouteInfo; | |
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | |
class PhpDocHeaderExtension extends OperationExtension | |
{ | |
public function handle(Operation $operation, RouteInfo $routeInfo): void | |
{ | |
$headers = collect($routeInfo->phpDoc()?->children) | |
->filter(fn ($item) => $item instanceof PhpDocTagNode && $item->name === '@header') | |
->map(fn($item) => (string) $item->value) | |
->values() | |
->toArray(); | |
foreach ($headers as $head) { | |
$header = [ 'name' => '', 'description' => '', 'type' => new StringType, 'required' => false ]; | |
$header = array_merge( | |
$header, | |
collect(explode("|", $head)) | |
->mapWithKeys(function ($i, $k) { | |
$item = trim($i); | |
$key = $k == 2 ? 'description' : 'name'; | |
if ($k == 3) { | |
$key = 'required'; | |
$item = strtolower(trim($i)) == 'true' ? true : false; | |
} | |
if ($k == 1) { | |
$key = 'type'; | |
if ( | |
strtolower($item) !== 'string' && | |
in_array(strtolower($item), [ | |
'number', 'boolean', 'null', 'array', 'object', 'integer', 'unknown', 'mixed' | |
]) | |
) { | |
$type = | |
'Dedoc\\Scramble\\Support\\Generator\\Types\\' . | |
ucfirst(strtolower($item)) . | |
'Type'; | |
$item = new $type; | |
} | |
else { | |
$item = new StringType; | |
} | |
} | |
return [$key => $item]; | |
}) | |
->toArray() | |
); | |
if ($header['name']) { | |
$operation->addParameters([ | |
Parameter::make(trim($header['name']), 'header') | |
->setSchema(Schema::fromType($header['type'])) | |
->description($header['description']) | |
->required($header['required']), | |
]); | |
} | |
} | |
} | |
} |
This file contains hidden or 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
<?php | |
/** | |
* Abitrary header values can easily be added with this extension | |
* properties of the header config must be in this order, separated by pipes: | |
* | |
* - header name | |
* | |
* - property type (one of: string, number, integer, boolean, null, array, object, mixed, unknown) | |
* defaults to string | |
* | |
* - description - default to empty string | |
* | |
* - required - defults to false | |
* | |
* @header X-Api-Key | string | API Key for this request | true | |
*/ | |
public function __invoke() | |
{ | |
return []; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment