Skip to content

Commit a527504

Browse files
yobrxsoyuka
andauthored
feat(metadata): use a custom Attribute extends ApiResource (#5076) (#5175)
Co-authored-by: Antoine Bluchet <[email protected]>
1 parent 5bc84ce commit a527504

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
8484
$operationPriority = 0;
8585

8686
foreach ($attributes as $attribute) {
87-
if (ApiResource::class === $attribute->getName()) {
87+
if (is_a($attribute->getName(), ApiResource::class, true)) {
8888
$resource = $this->getResourceWithDefaults($resourceClass, $shortName, $attribute->newInstance());
8989
$operations = [];
9090
foreach ($resource->getOperations() ?? new Operations() as $operation) {
@@ -165,7 +165,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
165165
private function hasResourceAttributes(\ReflectionClass $reflectionClass): bool
166166
{
167167
foreach ($reflectionClass->getAttributes() as $attribute) {
168-
if (ApiResource::class === $attribute->getName() || is_subclass_of($attribute->getName(), HttpOperation::class) || is_subclass_of($attribute->getName(), GraphQlOperation::class)) {
168+
if (is_a($attribute->getName(), ApiResource::class, true) || is_subclass_of($attribute->getName(), HttpOperation::class) || is_subclass_of($attribute->getName(), GraphQlOperation::class)) {
169169
return true;
170170
}
171171
}

src/Metadata/Resource/Factory/AttributesResourceNameCollectionFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function create(): ResourceNameCollection
5757

5858
private function isResource(\ReflectionClass $reflectionClass): bool
5959
{
60-
if ($reflectionClass->getAttributes(ApiResource::class)) {
60+
if ($reflectionClass->getAttributes(ApiResource::class, \ReflectionAttribute::IS_INSTANCEOF)) {
6161
return true;
6262
}
6363

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Attributes;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
18+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
19+
class RestfulApi extends ApiResource
20+
{
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Tests\Fixtures\TestBundle\Attributes\RestfulApi;
17+
use Doctrine\ORM\Mapping as ORM;
18+
19+
#[RestfulApi]
20+
#[ORM\Entity]
21+
class InstanceOfApiResource
22+
{
23+
#[ORM\Id]
24+
#[ORM\Column(type: 'integer')]
25+
#[ORM\GeneratedValue(strategy: 'AUTO')]
26+
private $id;
27+
28+
public function __construct()
29+
{
30+
}
31+
32+
public function getId()
33+
{
34+
return $this->id;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Metadata\Resource\Factory;
15+
16+
use ApiPlatform\Metadata\Resource\Factory\AttributesResourceNameCollectionFactory;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\InstanceOfApiResource;
18+
use PHPUnit\Framework\TestCase;
19+
use Prophecy\PhpUnit\ProphecyTrait;
20+
21+
/**
22+
* @author Yoann Brieux <[email protected]>
23+
*/
24+
class AttributesResourceNameCollectionFactoryTest extends TestCase
25+
{
26+
use ProphecyTrait;
27+
28+
public function testCreateWithInstanceOfApiResource(): void
29+
{
30+
$attributesResourceNameCollectionFactory = new AttributesResourceNameCollectionFactory(paths: [__DIR__.'/../../../Fixtures/TestBundle/Entity/']);
31+
32+
$this->assertContains(InstanceOfApiResource::class, $attributesResourceNameCollectionFactory->create()->getIterator());
33+
}
34+
}

0 commit comments

Comments
 (0)