Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read attributes as provided by the event object, and pass on attributes to other event handlers #53

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jobs:
matrix:
include:
- { php-version: 8.1, symfony-locked-version: none, dependency-version: prefer-lowest }
- { php-version: 8.2, symfony-locked-version: 5.4.*, dependency-version: prefer-stable }
- { php-version: 8.2, symfony-locked-version: 6.4.*, dependency-version: prefer-stable }
- { php-version: 8.3, symfony-locked-version: none, dependency-version: prefer-stable }
name: PHPUnit (PHP ${{matrix.php-version}}, Symfony Version Lock ${{ matrix.symfony-locked-version }}, ${{ matrix.dependency-version }})
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"symfony/filesystem": "^5.4|^6.4|^7.0",
"symfony/finder": "^5.4|^6.4|^7.0",
"symfony/http-foundation": "^5.4|^6.4|^7.0",
"symfony/http-kernel": "^5.4|^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/lock": "^5.4|^6.4|^7.0",
"symfony/twig-bundle": "^5.4|^6.4|^7.0",
"twig/twig": "^2.0|^3.0"
Expand Down
30 changes: 5 additions & 25 deletions src/Caching/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Webfactory\Bundle\WfdMetaBundle\Caching;

use ReflectionObject;
use SplObjectStorage;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
Expand Down Expand Up @@ -38,21 +37,19 @@ public function __construct(MetaQueryFactory $metaQueryFactory, $debug)

public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();
$request = $event->getRequest();

$attribute = $this->findAttribute($controller);
$attributes = $event->getAttributes(Send304IfNotModified::class);

if (!$attribute) {
if (!$attributes) {
return;
}

$lastTouched = $attribute->calculateLastModified($this->metaQueryFactory);
$lastTouched = $attributes[0]->calculateLastModified($this->metaQueryFactory);

if (!$lastTouched) {
return;
}

$request = $event->getRequest();
$this->lastTouchedResults[$request] = $lastTouched;

/*
Expand All @@ -73,7 +70,7 @@ public function onKernelController(ControllerEvent $event)
if ($response->isNotModified($request)) {
$event->setController(function () use ($response) {
return $response;
});
}, $event->getAttributes());
}
}

Expand All @@ -86,21 +83,4 @@ public function onKernelResponse(ResponseEvent $event)
$response->setLastModified($this->lastTouchedResults[$request]);
}
}

/**
* @param $callback array A PHP callback (array) pointing to the method to reflect on.
*/
protected function findAttribute($callback): ?Send304IfNotModified
{
if (!\is_array($callback)) {
return null;
}

$object = new ReflectionObject($callback[0]);
$method = $object->getMethod($callback[1]);

$attributes = $method->getAttributes(Send304IfNotModified::class);

return $attributes ? $attributes[0]->newInstance() : null;
}
}