vendor/ezsystems/ezplatform-http-cache/src/EventSubscriber/RestKernelViewSubscriber.php line 45

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\Values\Content\Section;
  8. use eZ\Publish\API\Repository\Values\ContentType\ContentType;
  9. use eZ\Publish\Core\REST\Common\Values\Root;
  10. use eZ\Publish\Core\REST\Server\Values\CachedValue;
  11. use eZ\Publish\Core\REST\Server\Values\ContentTypeGroupList;
  12. use eZ\Publish\Core\REST\Server\Values\ContentTypeGroupRefList;
  13. use eZ\Publish\Core\REST\Server\Values\RestContentType;
  14. use eZ\Publish\Core\REST\Server\Values\VersionList;
  15. use EzSystems\MultiFileUpload\API\Repository\Values\PermissionReport;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use FOS\HttpCache\Handler\TagHandler;
  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\Handler\TagHandler */
  30.     private $tagHandler;
  31.     public function __construct(TagHandler $tagHandler)
  32.     {
  33.         $this->tagHandler $tagHandler;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [KernelEvents::VIEW => ['tagUIRestResult'10]];
  38.     }
  39.     public function tagUIRestResult(GetResponseForControllerResultEvent $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[] = 'content-' $value->versions[0]->contentInfo->id;
  66.                 $tags[] = 'content-versions-' $value->versions[0]->contentInfo->id;
  67.                 break;
  68.             case $value instanceof Section:
  69.                 $tags[] = 'section-' $value->id;
  70.                 break;
  71.             case $value instanceof ContentTypeGroupRefList:
  72.                 if ($value->contentType->status !== ContentType::STATUS_DEFINED) {
  73.                     return [];
  74.                 }
  75.                 $tags[] = 'type-' $value->contentType->id;
  76.             case $value instanceof ContentTypeGroupList:
  77.                 foreach ($value->contentTypeGroups as $contentTypeGroup) {
  78.                     $tags[] = 'type-group-' $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[] = 'type-' $value->id;
  88.                 break;
  89.             case $value instanceof Root:
  90.                 $tags[] = 'ez-all';
  91.                 break;
  92.                 // @deprecated The following logic is 1.x specific, and should be removed before a 1.0 version
  93.             case $value instanceof PermissionReport:
  94.                 // We requrie v0.1.5 with added location property to be able to add tags
  95.                 if (!isset($value->parentLocation)) {
  96.                     return [];
  97.                 }
  98.                 // In case of for instance location swap where content type might change affecting allowed content types
  99.                 $tags[] = 'content-' $value->parentLocation->contentId;
  100.                 $tags[] = 'content-type-' $value->parentLocation->contentInfo->contentTypeId;
  101.                 $tags[] = 'location-' $value->parentLocation->id;
  102.                 // In case of permissions assigned by subtree, so if path changes affecting this (move subtree operation)
  103.                 foreach ($value->parentLocation->path as $pathItem) {
  104.                     $tags[] = 'path-' $pathItem;
  105.                 }
  106.                 break;
  107.         }
  108.         return $tags;
  109.     }
  110. }