vendor/ezsystems/ezplatform-http-cache/src/EventSubscriber/XLocationIdResponseSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace EzSystems\PlatformHttpCacheBundle\EventSubscriber;
  7. use eZ\Publish\API\Repository\Exceptions\NotFoundException;
  8. use eZ\Publish\API\Repository\Repository;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use FOS\HttpCache\Handler\TagHandler;
  13. /**
  14.  * Rewrites the X-Location-Id HTTP header.
  15.  *
  16.  * This is a BC layer for custom controllers (including REST server) still
  17.  * using X-Location-Id header which is now deprecated. For
  18.  * full value of tagging, see docs/using_tags.md for how to take advantage of the
  19.  * system.
  20.  */
  21. class XLocationIdResponseSubscriber implements EventSubscriberInterface
  22. {
  23.     const LOCATION_ID_HEADER 'X-Location-Id';
  24.     /** @var \FOS\HttpCache\Handler\TagHandler */
  25.     private $tagHandler;
  26.     /** @var \eZ\Publish\API\Repository\Repository */
  27.     private $repository;
  28.     public function __construct(TagHandler $tagHandlerRepository $repository)
  29.     {
  30.         $this->tagHandler $tagHandler;
  31.         $this->repository $repository;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [KernelEvents::RESPONSE => ['rewriteCacheHeader'10]];
  36.     }
  37.     public function rewriteCacheHeader(FilterResponseEvent $event)
  38.     {
  39.         $response $event->getResponse();
  40.         if (!$response->headers->has(static::LOCATION_ID_HEADER)) {
  41.             return;
  42.         }
  43.         @trigger_error(
  44.             'X-Location-Id is no longer preferred way to tag content responses, see ezplatform-http-cache/docs/using_tags.md',
  45.             E_USER_DEPRECATED
  46.         );
  47.         // Map the tags, even if not officially supported, handle comma separated values as was possible with Varnish
  48.         $tags = [];
  49.         foreach (explode(','$response->headers->get(static::LOCATION_ID_HEADER)) as $id) {
  50.             $id trim($id);
  51.             try {
  52.                 /** @var $location \eZ\Publish\API\Repository\Values\Content\Location */
  53.                 $location $this->repository->sudo(function (Repository $repository) use ($id) {
  54.                     return $repository->getLocationService()->loadLocation($id);
  55.                 });
  56.                 $tags[] = 'location-' $location->id;
  57.                 $tags[] = 'parent-' $location->parentLocationId;
  58.                 foreach ($location->path as $pathItem) {
  59.                     $tags[] = 'path-' $pathItem;
  60.                 }
  61.                 $contentInfo $location->getContentInfo();
  62.                 $tags[] = 'content-' $contentInfo->id;
  63.                 $tags[] = 'content-type-' $contentInfo->contentTypeId;
  64.                 if ($contentInfo->mainLocationId !== $location->id) {
  65.                     $tags[] = 'location-' $contentInfo->mainLocationId;
  66.                 }
  67.             } catch (NotFoundException $e) {
  68.                 $tags[] = "location-$id";
  69.                 $tags[] = "path-$id";
  70.             }
  71.         }
  72.         $this->tagHandler->addTags($tags);
  73.         $response->headers->remove(static::LOCATION_ID_HEADER);
  74.     }
  75. }