Completed
Push — master ( 07fc20...4efd3c )
by David de
34:38
created

Php8AttributesListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace FOS\HttpCacheBundle\EventListener;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
6
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
9
use Symfony\Component\HttpKernel\Event\RequestEvent;
10
use Symfony\Component\HttpKernel\Kernel;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
13
if (Kernel::MAJOR_VERSION >= 5) {
14
    class_alias(RequestEvent::class, 'FOS\HttpCacheBundle\EventListener\AttributeRequestEvent');
15
} else {
16
    class_alias(GetResponseEvent::class, 'FOS\HttpCacheBundle\EventListener\AttributeRequestEvent');
17
}
18
19
/**
20
 * On kernel.request event, this event handler fetch PHP8 attributes.
21
 * It is available from PHP 8.0.0.
22
 *
23
 * @author Yoann Chocteau <[email protected]>
24
 */
25
class Php8AttributesListener implements EventSubscriberInterface
26
{
27
    /**
28
     * @var ControllerResolver
29
     */
30
    private $controllerResolver;
31
32
    public function __construct(ControllerResolver $controllerResolver)
33
    {
34
        if (\PHP_VERSION_ID < 80000) {
35
            throw new \Exception(sprintf('Php8AttributesListener must not be loaded for PHP %s', phpversion()));
36
        }
37
        $this->controllerResolver = $controllerResolver;
38
    }
39
40
    public function onKernelRequest(AttributeRequestEvent $event)
41
    {
42
        $request = $event->getRequest();
43
        $controller = $this->controllerResolver->getController($request);
44
45
        if (!is_array($controller) || 2 !== count($controller)) {
46
            return;
47
        }
48
49
        $class = new \ReflectionClass($controller[0]);
50
        $method = $class->getMethod($controller[1]);
51
        $attributes = [];
52
        $addAttributes = function ($instance) use (&$attributes) {
53
            if (
54
                $instance instanceof ConfigurationInterface &&
55
                in_array(
56
                    $instance->getAliasName(), [
57
                    'tag', 'invalidate_path', 'invalidate_route',
58
                ])
59
            ) {
60
                $attributes['_'.$instance->getAliasName()][] = $instance;
61
            }
62
        };
63
64
        foreach ($class->getAttributes() as $classAttribute) {
65
            $addAttributes($classAttribute->newInstance());
66
        }
67
        foreach ($method->getAttributes() as $methodAttribute) {
68
            $addAttributes($methodAttribute->newInstance());
69
        }
70
71
        foreach ($attributes as $key => $attr) {
72
            $request->attributes->set(
73
                $key,
74
                array_merge($attr, $request->attributes->get($key, []))
75
            );
76
        }
77
    }
78
79
    public static function getSubscribedEvents()
80
    {
81
        return [
82
            KernelEvents::REQUEST => 'onKernelRequest',
83
        ];
84
    }
85
}
86