mirror of
https://github.com/ambieco/scribe.git
synced 2026-03-26 14:25:30 +08:00
Merge pull request #680 from cosmastech/feature/check-for-isinstanceof
Provide ability to extend Header attribute
This commit is contained in:
@@ -41,19 +41,19 @@ abstract class PhpAttributeStrategy extends Strategy
|
||||
protected function getAttributes(ReflectionFunctionAbstract $method, ?ReflectionClass $class = null): array
|
||||
{
|
||||
$attributesOnMethod = collect(static::$attributeNames)
|
||||
->flatMap(fn(string $name) => $method->getAttributes($name))
|
||||
->flatMap(fn(string $name) => $method->getAttributes($name, ReflectionAttribute::IS_INSTANCEOF))
|
||||
->map(fn(ReflectionAttribute $a) => $a->newInstance())->all();
|
||||
|
||||
// If there's a FormRequest, we check there.
|
||||
if ($formRequestClass = $this->getFormRequestReflectionClass($method)) {
|
||||
$attributesOnFormRequest = collect(static::$attributeNames)
|
||||
->flatMap(fn(string $name) => $formRequestClass->getAttributes($name))
|
||||
->flatMap(fn(string $name) => $formRequestClass->getAttributes($name, ReflectionAttribute::IS_INSTANCEOF))
|
||||
->map(fn(ReflectionAttribute $a) => $a->newInstance())->all();
|
||||
}
|
||||
|
||||
if ($class) {
|
||||
$attributesOnController = collect(static::$attributeNames)
|
||||
->flatMap(fn(string $name) => $class->getAttributes($name))
|
||||
->flatMap(fn(string $name) => $class->getAttributes($name, ReflectionAttribute::IS_INSTANCEOF))
|
||||
->map(fn(ReflectionAttribute $a) => $a->newInstance())->all();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Knuckles\Scribe\Tests\Strategies\Headers;
|
||||
|
||||
use Attribute;
|
||||
use Knuckles\Camel\Extraction\ExtractedEndpointData;
|
||||
use Knuckles\Scribe\Attributes\Header;
|
||||
use Knuckles\Scribe\Extracting\Strategies\Headers\GetFromHeaderAttribute;
|
||||
@@ -17,14 +18,7 @@ class GetFromHeaderAttributeTest extends TestCase
|
||||
/** @test */
|
||||
public function can_fetch_from_header_attribute()
|
||||
{
|
||||
$endpoint = new class extends ExtractedEndpointData {
|
||||
public function __construct(array $parameters = []) {}
|
||||
};
|
||||
$endpoint->controller = new ReflectionClass(\Knuckles\Scribe\Tests\Strategies\Headers\HeaderAttributeTestController::class);
|
||||
$endpoint->method = $endpoint->controller->getMethod('methodWithAttributes');
|
||||
|
||||
$strategy = new GetFromHeaderAttribute(new DocumentationConfig([]));
|
||||
$results = $strategy($endpoint);
|
||||
$results = $this->getHeaderFromAttribute('methodWithAttributes');
|
||||
|
||||
$this->assertArraySubset([
|
||||
'Api-Version' => 'v1',
|
||||
@@ -33,6 +27,31 @@ class GetFromHeaderAttributeTest extends TestCase
|
||||
$this->assertNotEmpty($results['Some-Custom']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function can_fetch_child_of_header_attribute()
|
||||
{
|
||||
$results = $this->getHeaderFromAttribute('methodWithCustomHeaderAttribute');
|
||||
|
||||
$this->assertArraySubset([
|
||||
'Api-Version' => 'v1',
|
||||
], $results);
|
||||
$this->assertArrayHasKey('hello', $results);
|
||||
$this->assertEquals('world', $results['hello']);
|
||||
}
|
||||
|
||||
private function getHeaderFromAttribute(string $methodName): array
|
||||
{
|
||||
$endpoint = new class extends ExtractedEndpointData {
|
||||
public function __construct(array $parameters = []) {}
|
||||
};
|
||||
$endpoint->controller = new ReflectionClass(\Knuckles\Scribe\Tests\Strategies\Headers\HeaderAttributeTestController::class);
|
||||
$endpoint->method = $endpoint->controller->getMethod($methodName);
|
||||
|
||||
$strategy = new GetFromHeaderAttribute(new DocumentationConfig([]));
|
||||
|
||||
return $strategy($endpoint);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[Header("Api-Version", "v1")]
|
||||
@@ -43,4 +62,19 @@ class HeaderAttributeTestController
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#[CustomHeaderClass()]
|
||||
public function methodWithCustomHeaderAttribute()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
|
||||
class CustomHeaderClass extends Header
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('hello', 'world');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user