Skip to content

Commit 2f23846

Browse files
author
Alessandro Chitolina
committed
add sunset header field annotation handler
1 parent 144e986 commit 2f23846

File tree

12 files changed

+297
-1
lines changed

12 files changed

+297
-1
lines changed

src/Annotation/Sunset.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\Annotation;
4+
5+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
/**
9+
* @Annotation()
10+
*/
11+
class Sunset implements ConfigurationInterface
12+
{
13+
/**
14+
* @var string
15+
*
16+
* @Required()
17+
*/
18+
public $date;
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function getAliasName(): string
24+
{
25+
return 'rest_sunset';
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function allowArray(): bool
32+
{
33+
return false;
34+
}
35+
}

src/Annotation/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Component\HttpFoundation\Response;
77

88
/**
9-
* @Annotation
9+
* @Annotation()
1010
*/
1111
class View implements ConfigurationInterface
1212
{

src/DependencyInjection/ApiPlatformExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function load(array $configs, ContainerBuilder $container): void
3232

3333
if ($config['view']['enabled']) {
3434
$loader->load('view.xml');
35+
$loader->load('sunset.xml');
3536
$loader->load('exception_listeners.xml');
3637
}
3738

src/HttpKernel/SunsetHandler.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\HttpKernel;
4+
5+
use Fazland\ApiPlatformBundle\Annotation\Sunset;
6+
use Fazland\ApiPlatformBundle\Annotation\View as ViewAnnotation;
7+
use Fazland\ApiPlatformBundle\HttpKernel\View\Context;
8+
use Fazland\ApiPlatformBundle\HttpKernel\View\View;
9+
use Kcs\Serializer\Exception\UnsupportedFormatException;
10+
use Kcs\Serializer\Type\Type;
11+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12+
use Symfony\Component\HttpFoundation\Response;
13+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
14+
use Symfony\Component\HttpKernel\KernelEvents;
15+
16+
/**
17+
* Adds Sunset header field if Sunset annotation is found.
18+
*
19+
* @see https://tools.ietf.org/html/draft-wilde-sunset-header-10
20+
*/
21+
class SunsetHandler implements EventSubscriberInterface
22+
{
23+
/**
24+
* Modify the response adding a Sunset header if needed.
25+
*
26+
* @param FilterResponseEvent $event
27+
*/
28+
public function onResponse(FilterResponseEvent $event): void
29+
{
30+
$request = $event->getRequest();
31+
32+
$annotation = $request->attributes->get('_rest_sunset');
33+
if (! $annotation instanceof Sunset) {
34+
return;
35+
}
36+
37+
$date = new \DateTimeImmutable($annotation->date);
38+
39+
$response = $event->getResponse();
40+
$response->headers->set('Sunset', $date->format(\DateTime::RFC2822));
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public static function getSubscribedEvents(): array
47+
{
48+
return [
49+
KernelEvents::RESPONSE => 'onResponse',
50+
];
51+
}
52+
}

src/Resources/config/sunset.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<service class="Fazland\ApiPlatformBundle\HttpKernel\SunsetHandler" id="Fazland\ApiPlatformBundle\HttpKernel\SunsetHandler">
9+
<tag name="kernel.event_subscriber" />
10+
</service>
11+
</services>
12+
</container>

tests/Fixtures/Sunset/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
logs/
2+
cache/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\Tests\Fixtures\Sunset;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class AppBundle extends Bundle
8+
{
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\Tests\Fixtures\Sunset;
4+
5+
use Fazland\ApiPlatformBundle\Tests\Fixtures\TestKernel;
6+
use Kcs\Serializer\Bundle\SerializerBundle;
7+
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
8+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
9+
use Symfony\Bundle\SecurityBundle\SecurityBundle;
10+
use Symfony\Component\Config\Loader\LoaderInterface;
11+
12+
class AppKernel extends TestKernel
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function registerBundles(): iterable
18+
{
19+
return [
20+
new FrameworkBundle(),
21+
new SensioFrameworkExtraBundle(),
22+
new SecurityBundle(),
23+
new SerializerBundle(),
24+
new AppBundle(),
25+
];
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function registerContainerConfiguration(LoaderInterface $loader): void
32+
{
33+
$loader->load(__DIR__.'/config.yml');
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\Tests\Fixtures\Sunset\Controller;
4+
5+
use Fazland\ApiPlatformBundle\Annotation\Sunset;
6+
use Fazland\ApiPlatformBundle\Annotation\View;
7+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8+
9+
class TestController extends Controller
10+
{
11+
/**
12+
* @return mixed
13+
*
14+
* @View()
15+
* @Sunset("2019-03-01")
16+
*/
17+
public function indexAction(): array
18+
{
19+
return [
20+
'test_foo' => 'foo.test',
21+
];
22+
}
23+
}

tests/Fixtures/Sunset/config.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
imports:
2+
- { resource: ../../../src/Resources/config/view.xml }
3+
- { resource: ../../../src/Resources/config/sunset.xml }
4+
- { resource: ../../../src/Resources/config/serializer.xml }
5+
- { resource: ../../../src/Resources/config/exception_listeners.xml }
6+
7+
parameters:
8+
fazland_api.response_charset: UTF-8
9+
10+
framework:
11+
test: ~
12+
secret: secret
13+
form: ~
14+
csrf_protection: false
15+
router:
16+
resource: "%kernel.root_dir%/routing.yml"
17+
18+
security:
19+
providers:
20+
in_memory:
21+
memory: ~
22+
23+
firewalls:
24+
dev:
25+
pattern: ^/(_(profiler|wdt))/
26+
security: false
27+
main:
28+
anonymous: ~
29+
30+
sensio_framework_extra:
31+
router: { annotations: false }
32+
request: { converters: true, auto_convert: true }
33+
view: { annotations: false }
34+
cache: { annotations: true }
35+
security: { annotations: true }
36+
psr_message: { enabled: false }

0 commit comments

Comments
 (0)