vendor/ibexa/http-cache/src/lib/EventSubscriber/RestKernelViewSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace Ibexa\HttpCache\EventSubscriber;
  7. use FOS\HttpCache\ResponseTagger;
  8. use Ibexa\Contracts\Core\Repository\Values\Content\Section;
  9. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  10. use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface;
  11. use Ibexa\Rest\Server\Values\CachedValue;
  12. use Ibexa\Rest\Server\Values\ContentTypeGroupList;
  13. use Ibexa\Rest\Server\Values\ContentTypeGroupRefList;
  14. use Ibexa\Rest\Server\Values\RestContentType;
  15. use Ibexa\Rest\Server\Values\VersionList;
  16. use Ibexa\Rest\Values\Root;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpKernel\Event\ViewEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. /**
  21.  * Set cache tags on a few REST responses used by UI in order to be able to cache them.
  22.  *
  23.  * @deprecated This is a temprary approach to caching certain parts of REST used by UI, it is deprecated in favour of
  24.  *             nativly using tags and CachedValue in kernel's REST server itself once we switch to FOSHttpCache 2.x
  25.  *             where tagger service can be used directly.
  26.  */
  27. class RestKernelViewSubscriber implements EventSubscriberInterface
  28. {
  29.     /** @var \FOS\HttpCache\ResponseTagger */
  30.     private $tagHandler;
  31.     public function __construct(ResponseTagger $tagHandler)
  32.     {
  33.         $this->tagHandler $tagHandler;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [KernelEvents::VIEW => ['tagUIRestResult'10]];
  38.     }
  39.     public function tagUIRestResult(ViewEvent $event)
  40.     {
  41.         $request $event->getRequest();
  42.         if (!$request->isMethodCacheable() || !$request->attributes->get('is_rest_request')) {
  43.             return;
  44.         }
  45.         // Get tags, and exit if none where found
  46.         $restValue $event->getControllerResult();
  47.         $tags $this->getTags($restValue);
  48.         if (empty($tags)) {
  49.             return;
  50.         }
  51.         // Add tags and swap Rest Value for cached value now that REST server can safely cache it
  52.         $this->tagHandler->addTags($tags);
  53.         $event->setControllerResult(new CachedValue($restValue));
  54.     }
  55.     /**
  56.      * @param object $value
  57.      *
  58.      * @return array
  59.      */
  60.     protected function getTags($value)
  61.     {
  62.         $tags = [];
  63.         switch ($value) {
  64.             case $value instanceof VersionList && !empty($value->versions):
  65.                 $tags[] = ContentTagInterface::CONTENT_PREFIX $value->versions[0]->contentInfo->id;
  66.                 $tags[] = ContentTagInterface::CONTENT_VERSION_PREFIX $value->versions[0]->contentInfo->id;
  67.                 break;
  68.             case $value instanceof Section:
  69.                 $tags[] = 's' $value->id;
  70.                 break;
  71.             case $value instanceof ContentTypeGroupRefList:
  72.                 if ($value->contentType->status !== ContentType::STATUS_DEFINED) {
  73.                     return [];
  74.                 }
  75.                 $tags[] = 't' $value->contentType->id;
  76.             case $value instanceof ContentTypeGroupList:
  77.                 foreach ($value->contentTypeGroups as $contentTypeGroup) {
  78.                     $tags[] = 'tg' $contentTypeGroup->id;
  79.                 }
  80.                 break;
  81.             case $value instanceof RestContentType:
  82.                 $value $value->contentType;
  83.             case $value instanceof ContentType:
  84.                 if ($value->status !== ContentType::STATUS_DEFINED) {
  85.                     return [];
  86.                 }
  87.                 $tags[] = 't' $value->id;
  88.                 break;
  89.             case $value instanceof Root:
  90.                 $tags[] = ContentTagInterface::ALL_TAG;
  91.                 break;
  92.         }
  93.         return $tags;
  94.     }
  95. }
  96. class_alias(RestKernelViewSubscriber::class, 'EzSystems\PlatformHttpCacheBundle\EventSubscriber\RestKernelViewSubscriber');