|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * (c) webfactory GmbH <info@webfactory.de> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Webfactory\HttpCacheBundle\NotModified; |
| 11 | + |
| 12 | +use Doctrine\Common\Annotations\Reader; |
| 13 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | +use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
| 16 | +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
| 17 | +use Webfactory\HttpCacheBundle\NotModified\Annotation\ReplaceWithNotModifiedResponse; |
| 18 | + |
| 19 | +/** |
| 20 | + * Symfony EventListener for adding a "last modified" header to the response on the one hand. On the other hand, it |
| 21 | + * replaces the execution of a controller action with a Not Modified HTTP response, if no newer "last modified" date is |
| 22 | + * determined than the one in the header of a subsequent request. |
| 23 | + */ |
| 24 | +final class EventListener |
| 25 | +{ |
| 26 | + /** @var Reader */ |
| 27 | + private $reader; |
| 28 | + |
| 29 | + /** @var ContainerInterface */ |
| 30 | + private $container; |
| 31 | + |
| 32 | + /** |
| 33 | + * Maps (master and sub) requests to their corresponding last modified date. This date is determined by the |
| 34 | + * ReplaceWithNotModifiedResponse annotation of the corresponding controller's action. |
| 35 | + * |
| 36 | + * @var \SplObjectStorage |
| 37 | + */ |
| 38 | + private $lastModified; |
| 39 | + |
| 40 | + /** |
| 41 | + * @param Reader $reader |
| 42 | + * @param ContainerInterface $container |
| 43 | + */ |
| 44 | + public function __construct(Reader $reader, ContainerInterface $container) |
| 45 | + { |
| 46 | + $this->reader = $reader; |
| 47 | + $this->container = $container; |
| 48 | + $this->lastModified = new \SplObjectStorage(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * When the controller action for a request is determined, check it for a ReplaceWithNotModifiedResponse annotation. |
| 53 | + * If it determines that the underlying ressources for the response were not modified after the "If-Modified-Since" |
| 54 | + * header in the request, replace the determines controller action with an minimal action that just returns an |
| 55 | + * "empty" response with a 304 Not Modified HTTP status code. |
| 56 | + * |
| 57 | + * @param FilterControllerEvent $event |
| 58 | + */ |
| 59 | + public function onKernelController(FilterControllerEvent $event) |
| 60 | + { |
| 61 | + $annotation = $this->findAnnotation($event->getController()); |
| 62 | + if (!$annotation) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + $request = $event->getRequest(); |
| 67 | + $annotation->setContainer($this->container); |
| 68 | + $lastModified = $annotation->determineLastModified($request); |
| 69 | + if (!$lastModified) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + $this->lastModified[$request] = $lastModified; |
| 74 | + |
| 75 | + $response = new Response(); |
| 76 | + $response->setLastModified($lastModified); |
| 77 | + |
| 78 | + if ($response->isNotModified($request)) { |
| 79 | + $event->setController(function () use ($response) { |
| 80 | + return $response; |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * If a last modified date was determined for the current (master or sub) request, set it to the response so the |
| 87 | + * client can use it for the "If-Modified-Since" header in subsequent requests. |
| 88 | + * |
| 89 | + * @param FilterResponseEvent $event |
| 90 | + */ |
| 91 | + public function onKernelResponse(FilterResponseEvent $event) |
| 92 | + { |
| 93 | + $request = $event->getRequest(); |
| 94 | + $response = $event->getResponse(); |
| 95 | + |
| 96 | + if (isset($this->lastModified[$request])) { |
| 97 | + $response->setLastModified($this->lastModified[$request]); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param $controllerCallable callable PHP callback pointing to the method to reflect on. |
| 103 | + * @return ReplaceWithNotModifiedResponse|null The annotation, if found. Null otherwise. |
| 104 | + */ |
| 105 | + private function findAnnotation(callable $controllerCallable) |
| 106 | + { |
| 107 | + if (!is_array($controllerCallable)) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + list($class, $methodName) = $controllerCallable; |
| 112 | + $method = new \ReflectionMethod($class, $methodName); |
| 113 | + |
| 114 | + /** @var ReplaceWithNotModifiedResponse|null $annotation */ |
| 115 | + $annotation = $this->reader->getMethodAnnotation($method, ReplaceWithNotModifiedResponse::class); |
| 116 | + return $annotation; |
| 117 | + } |
| 118 | +} |
0 commit comments