Skip to content

Commit 8273afa

Browse files
author
Yoann Brieux
committed
feat(metadata): use a custom Attribute extends ApiResource (#5076)
1 parent f1ecc30 commit 8273afa

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

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,33 @@
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+
public function __construct(
22+
?string $routePrefix = null,
23+
) {
24+
parent::__construct(
25+
routePrefix: $routePrefix,
26+
normalizationContext: ['groups' => ['restful_read']],
27+
denormalizationContext: ['groups' => ['restful_write']],
28+
openapiContext: ['tags' => ['LiveChat']],
29+
order: ['id' => 'DESC'],
30+
security: 'is_granted("ROLE_OPERATOR")'
31+
);
32+
}
33+
}
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)