Skip to content

Commit 3da47bd

Browse files
blanky0230Thomas Blank
authored andcommitted
Begin Introduction of new 'sunset' API-Resource annotation in order to support: sunset header
1 parent b15030b commit 3da47bd

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

src/Annotation/ApiResource.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
* @Attribute("routePrefix", type="string"),
5252
* @Attribute("shortName", type="string"),
5353
* @Attribute("subresourceOperations", type="array"),
54+
* @Attribute("sunset", type="string"),
5455
* @Attribute("swaggerContext", type="array"),
55-
* @Attribute("validationGroups", type="mixed")
56+
* @Attribute("validationGroups", type="mixed"),
5657
* )
5758
*/
5859
final class ApiResource
@@ -248,6 +249,11 @@ final class ApiResource
248249
*/
249250
private $validationGroups;
250251

252+
/**
253+
* @var string
254+
*/
255+
private $sunset;
256+
251257
/**
252258
* @throws InvalidArgumentException
253259
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Core\Sunset\EventListener;
15+
16+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
17+
use ApiPlatform\Core\Util\RequestAttributesExtractor;
18+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
19+
20+
class AddSunsetHeaderListener
21+
{
22+
private $resourceMetadataFactory;
23+
24+
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory)
25+
{
26+
$this->resourceMetadataFactory = $resourceMetadataFactory;
27+
}
28+
29+
public function onKernelResponse(FilterResponseEvent $event)
30+
{
31+
$request = $event->getRequest();
32+
$attributes = RequestAttributesExtractor::extractAttributes($request);
33+
34+
if (!$attributes) {
35+
return;
36+
}
37+
38+
$response = $event->getResponse();
39+
$metadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
40+
41+
if (null !== $metadata->getAttribute('sunset')) {
42+
$response->headers->set('Sunset', $metadata->getAttribute('sunset'));
43+
}
44+
}
45+
}

tests/Annotation/ApiResourceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function testConstruct()
5454
'shortName' => 'shortName',
5555
'subresourceOperations' => [],
5656
'validationGroups' => ['foo', 'bar'],
57+
'sunset' => 'Thu, 11 Oct 2018 00:00:00 +0200',
5758
]);
5859

5960
$this->assertSame('shortName', $resource->shortName);
@@ -84,6 +85,7 @@ public function testConstruct()
8485
'pagination_partial' => true,
8586
'route_prefix' => '/foo',
8687
'validation_groups' => ['baz', 'qux'],
88+
'sunset' => 'Thu, 11 Oct 2018 00:00:00 +0200',
8789
], $resource->attributes);
8890
}
8991

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Core\Tests\Sunset;
15+
16+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
17+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
18+
use ApiPlatform\Core\Sunset\EventListener\AddSunsetHeaderListener;
19+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
20+
use PHPUnit\Framework\TestCase;
21+
use Symfony\Component\HttpFoundation\Request;
22+
use Symfony\Component\HttpFoundation\Response;
23+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
24+
25+
class AddSunsetHeaderListenerTest extends TestCase
26+
{
27+
public function testSetSunsetHeader()
28+
{
29+
$request = new Request([], [], ['_api_resource_class' => Dummy::class, '_api_item_operation_name' => 'get']);
30+
$request->setMethod('GET');
31+
$response = new Response();
32+
$date = new \DateTimeImmutable('tomorrow');
33+
34+
$eventProphecy = $this->prophesize(FilterResponseEvent::class);
35+
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
36+
$eventProphecy->getResponse()->willReturn($response)->shouldBeCalled();
37+
$event = $eventProphecy->reveal();
38+
39+
$resourceMetadata = new ResourceMetadata(null, null, null, null,
40+
null, ['sunset' => $date->format(DATE_RFC1123)]);
41+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
42+
$resourceMetadataFactoryProphecy->create(Dummy::class)->willReturn($resourceMetadata)->shouldBeCalled();
43+
44+
$listener = new AddSunsetHeaderListener($resourceMetadataFactoryProphecy->reveal());
45+
$listener->onKernelResponse($event);
46+
$this->assertSame($event->getResponse(), $response);
47+
$this->assertEquals($date,
48+
\DateTime::createFromFormat(DATE_RFC1123, $event->getResponse()->headers->get('Sunset')));
49+
}
50+
}

0 commit comments

Comments
 (0)