vendor/friendsofsymfony/http-cache/src/SymfonyCache/EventDispatchingHttpCache.php line 89

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSHttpCache package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\HttpCache\SymfonyCache;
  11. use Symfony\Component\EventDispatcher\EventDispatcher;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\HttpKernelInterface;
  17. /**
  18.  * Trait for enhanced Symfony reverse proxy based on the symfony kernel component.
  19.  *
  20.  * Your kernel needs to implement CacheInvalidatorInterface and redeclare the
  21.  * fetch method as public. (The latter is needed because the trait declaring it
  22.  * public does not satisfy the interface for whatever reason. See also
  23.  * http://stackoverflow.com/questions/31877844/php-trait-exposing-a-method-and-interfaces )
  24.  *
  25.  * CacheInvalidator kernels support event listeners that can act on the
  26.  * events defined in FOS\HttpCache\SymfonyCache\Events and may alter the
  27.  * request flow.
  28.  *
  29.  * If your kernel overwrites any of the methods defined in this trait, make
  30.  * sure to also call the trait method. You might get into issues with the order
  31.  * of events, in which case you will need to copy event triggering into your
  32.  * kernel.
  33.  *
  34.  * @author Jérôme Vieilledent <lolautruche@gmail.com> (courtesy of eZ Systems AS)
  35.  * @author David Buchmann <mail@davidbu.ch>
  36.  *
  37.  * {@inheritdoc}
  38.  */
  39. trait EventDispatchingHttpCache
  40. {
  41.     /**
  42.      * @var EventDispatcherInterface
  43.      */
  44.     private $eventDispatcher;
  45.     /**
  46.      * Get event dispatcher.
  47.      *
  48.      * @return EventDispatcherInterface
  49.      */
  50.     public function getEventDispatcher()
  51.     {
  52.         if (!$this->eventDispatcher) {
  53.             $this->eventDispatcher = new EventDispatcher();
  54.         }
  55.         return $this->eventDispatcher;
  56.     }
  57.     /**
  58.      * Add an event subscriber.
  59.      *
  60.      * @see EventDispatcherInterface::addSubscriber
  61.      */
  62.     public function addSubscriber(EventSubscriberInterface $subscriber)
  63.     {
  64.         $this->getEventDispatcher()->addSubscriber($subscriber);
  65.     }
  66.     /**
  67.      * Add an event listener to this HttpCache.
  68.      *
  69.      * @see EventDispatcherInterface::addListener
  70.      */
  71.     public function addListener($eventName$listener$priority 0)
  72.     {
  73.         $this->getEventDispatcher()->addListener($eventName$listener$priority);
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      *
  78.      * Adding the Events::PRE_HANDLE and Events::POST_HANDLE events.
  79.      */
  80.     public function handle(Request $request$type HttpKernelInterface::MASTER_REQUEST$catch true): Response
  81.     {
  82.         // trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used.
  83.         class_exists(CacheEvent::class);
  84.         if ($response $this->dispatch(Events::PRE_HANDLE$requestnull$type)) {
  85.             return $this->dispatch(Events::POST_HANDLE$request$response$type);
  86.         }
  87.         $response parent::handle($request$type$catch);
  88.         return $this->dispatch(Events::POST_HANDLE$request$response$type);
  89.     }
  90.     /**
  91.      * {@inheritdoc}
  92.      *
  93.      * Trigger event to alter response before storing it in the cache.
  94.      */
  95.     protected function store(Request $requestResponse $response)
  96.     {
  97.         $response $this->dispatch(Events::PRE_STORE$request$response);
  98.         parent::store($request$response);
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      *
  103.      * Adding the Events::PRE_INVALIDATE event.
  104.      */
  105.     protected function invalidate(Request $request$catch false): Response
  106.     {
  107.         if ($response $this->dispatch(Events::PRE_INVALIDATE$request)) {
  108.             return $response;
  109.         }
  110.         return parent::invalidate($request$catch);
  111.     }
  112.     /**
  113.      * Dispatch an event if there are any listeners.
  114.      *
  115.      * @param string        $name        Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events
  116.      * @param Response|null $response    If already available
  117.      * @param int           $requestType The request type (default HttpKernelInterface::MASTER_REQUEST)
  118.      *
  119.      * @return Response|null The response to return, which might be provided/altered by a listener
  120.      */
  121.     protected function dispatch($nameRequest $requestResponse $response null$requestType HttpKernelInterface::MASTER_REQUEST): ?Response
  122.     {
  123.         if ($this->getEventDispatcher()->hasListeners($name)) {
  124.             $event = new CacheEvent($this$request$response$requestType);
  125.             $this->getEventDispatcher()->dispatch($event$name);
  126.             $response $event->getResponse();
  127.         }
  128.         return $response;
  129.     }
  130. }