Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
472 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace FOS\HttpCacheBundle\EventListener; | ||
|
||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
if (Kernel::MAJOR_VERSION >= 5) { | ||
class_alias(RequestEvent::class, 'FOS\HttpCacheBundle\EventListener\AttributeRequestEvent'); | ||
} else { | ||
class_alias(GetResponseEvent::class, 'FOS\HttpCacheBundle\EventListener\AttributeRequestEvent'); | ||
} | ||
|
||
/** | ||
* On kernel.request event, this event handler fetch PHP8 attributes. | ||
* It is available from PHP 8.0.0. | ||
* | ||
* @author Yoann Chocteau <yoann@kezaweb.fr> | ||
*/ | ||
class Php8AttributesListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var ControllerResolver | ||
*/ | ||
private $controllerResolver; | ||
|
||
public function __construct(ControllerResolver $controllerResolver) | ||
{ | ||
if (\PHP_VERSION_ID < 80000) { | ||
throw new \Exception(sprintf('Php8AttributesListener must not be loaded for PHP %s', phpversion())); | ||
} | ||
$this->controllerResolver = $controllerResolver; | ||
} | ||
|
||
public function onKernelRequest(AttributeRequestEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
$controller = $this->controllerResolver->getController($request); | ||
|
||
if (!is_array($controller) || 2 !== count($controller)) { | ||
return; | ||
} | ||
|
||
$class = new \ReflectionClass($controller[0]); | ||
$method = $class->getMethod($controller[1]); | ||
$attributes = []; | ||
$addAttributes = function ($instance) use (&$attributes) { | ||
if ( | ||
$instance instanceof ConfigurationInterface && | ||
in_array( | ||
$instance->getAliasName(), [ | ||
'tag', 'invalidate_path', 'invalidate_route', | ||
]) | ||
) { | ||
$attributes['_'.$instance->getAliasName()][] = $instance; | ||
} | ||
}; | ||
|
||
foreach ($class->getAttributes() as $classAttribute) { | ||
$addAttributes($classAttribute->newInstance()); | ||
} | ||
foreach ($method->getAttributes() as $methodAttribute) { | ||
$addAttributes($methodAttribute->newInstance()); | ||
} | ||
|
||
foreach ($attributes as $key => $attr) { | ||
$request->attributes->set( | ||
$key, | ||
array_merge($attr, $request->attributes->get($key, [])) | ||
); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => 'onKernelRequest', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="fos_http_cache.event_listener.php8_attributes" class="FOS\HttpCacheBundle\EventListener\Php8AttributesListener"> | ||
<argument type="service" id="controller_resolver" /> | ||
<tag name="kernel.event_subscriber" /> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.