-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
1 deletion.
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
73 changes: 73 additions & 0 deletions
73
src/Extracting/Strategies/RequestHeaders/GetFromHeaderTag.php
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,73 @@ | ||
<?php | ||
|
||
namespace Mpociot\ApiDoc\Extracting\Strategies\RequestHeaders; | ||
|
||
use Dingo\Api\Http\FormRequest as DingoFormRequest; | ||
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest; | ||
use Illuminate\Routing\Route; | ||
use Illuminate\Support\Str; | ||
use Mpociot\ApiDoc\Extracting\ParamHelpers; | ||
use Mpociot\ApiDoc\Extracting\RouteDocBlocker; | ||
use Mpociot\ApiDoc\Extracting\Strategies\Strategy; | ||
use Mpociot\Reflection\DocBlock; | ||
use Mpociot\Reflection\DocBlock\Tag; | ||
use ReflectionClass; | ||
use ReflectionMethod; | ||
|
||
class GetFromHeaderTag extends Strategy | ||
{ | ||
use ParamHelpers; | ||
|
||
public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = []) | ||
{ | ||
foreach ($method->getParameters() as $param) { | ||
$paramType = $param->getType(); | ||
if ($paramType === null) { | ||
continue; | ||
} | ||
|
||
$parameterClassName = $paramType->getName(); | ||
|
||
try { | ||
$parameterClass = new ReflectionClass($parameterClassName); | ||
} catch (\ReflectionException $e) { | ||
continue; | ||
} | ||
|
||
// If there's a FormRequest, we check there for @urlParam tags. | ||
if (class_exists(LaravelFormRequest::class) && $parameterClass->isSubclassOf(LaravelFormRequest::class) | ||
|| class_exists(DingoFormRequest::class) && $parameterClass->isSubclassOf(DingoFormRequest::class)) { | ||
$formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); | ||
$headersFromDocBlock = $this->getHeadersFromDocBlock($formRequestDocBlock->getTags()); | ||
|
||
if (count($headersFromDocBlock)) { | ||
return $headersFromDocBlock; | ||
} | ||
} | ||
} | ||
|
||
/** @var DocBlock $methodDocBlock */ | ||
$methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method']; | ||
|
||
return $this->getHeadersFromDocBlock($methodDocBlock->getTags()); | ||
} | ||
|
||
private function getHeadersFromDocBlock($tags) | ||
{ | ||
$parameters = collect($tags) | ||
->filter(function ($tag) { | ||
return $tag instanceof Tag && $tag->getName() === 'header'; | ||
}) | ||
->mapWithKeys(function (Tag $tag) { | ||
// Format: | ||
// @header <content> | ||
// Examples: | ||
// @header Cookie foo Example: crumbs | ||
preg_match('/(.+?)\s+(.*)/', $tag->getContent(), $content); | ||
list($content, $name, $value) = $content; | ||
return [$name => $value]; | ||
})->toArray(); | ||
|
||
return $parameters; | ||
} | ||
} |
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