-
Notifications
You must be signed in to change notification settings - Fork 80
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
14 changed files
with
425 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
|
||
namespace FOS\HttpCacheBundle\EventListener; | ||
|
||
use FOS\HttpCacheBundle\CacheManager; | ||
use FOS\HttpCacheBundle\Http\RuleMatcherInterface; | ||
use FOS\HttpCacheBundle\Http\SymfonyResponseTagger; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
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'); | ||
} | ||
|
||
class CatchAttributesListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var ControllerResolver | ||
*/ | ||
private $controllerResolver; | ||
|
||
public function __construct(ControllerResolver $controllerResolver) { | ||
$this->controllerResolver = $controllerResolver; | ||
} | ||
|
||
public function onKernelRequest(AttributeRequestEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
|
||
if ( | ||
method_exists(\ReflectionProperty::class, 'getAttributes') && | ||
$controller = $this->controllerResolver->getController($request) | ||
) { | ||
$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,16 @@ | ||
<?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.controller_resolver" class="Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver"> | ||
<argument type="service" id="service_container" /> | ||
</service> | ||
|
||
<service id="fos_http_cache.event_listener.catch_attributes" class="FOS\HttpCacheBundle\EventListener\CatchAttributesListener"> | ||
<argument type="service" id="fos_http_cache.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
45 changes: 45 additions & 0 deletions
45
tests/Functional/Fixtures/Controller/InvalidationAttributeController.php
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,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSHttpCacheBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller; | ||
|
||
use FOS\HttpCacheBundle\Configuration\InvalidatePath; | ||
use FOS\HttpCacheBundle\Configuration\InvalidateRoute; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
if (!\class_exists(AbstractController::class)) { | ||
\class_alias(Controller::class, AbstractController::class); | ||
} | ||
|
||
class InvalidationAttributeController extends AbstractController | ||
{ | ||
#[InvalidateRoute('test_noncached')] | ||
#[InvalidateRoute('test_cached', params: ['id' => 'myhardcodedid'])] | ||
#[InvalidateRoute('tag_one', params: ['id' => ['expression' => 'id']])] | ||
public function itemAction($id) | ||
{ | ||
return new Response("Done $id"); | ||
} | ||
|
||
#[InvalidatePath('/php8/cached')] | ||
public function otherAction($statusCode) | ||
{ | ||
return new Response('Done.', $statusCode); | ||
} | ||
|
||
#[InvalidatePath('/php8/somepath')] | ||
public function errorAction() | ||
{ | ||
return new Response('Forbidden', 403); | ||
} | ||
} |
Oops, something went wrong.