vendor/ibexa/http-cache/src/lib/EventSubscriber/XLocationIdResponseSubscriber.php line 46

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\Exceptions\NotFoundException;
  9. use Ibexa\Contracts\Core\Repository\Repository;
  10. use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. /**
  15. * Rewrites the X-Location-Id HTTP header.
  16. *
  17. * This is a BC layer for custom controllers (including REST server) still
  18. * using X-Location-Id header which is now deprecated. For
  19. * full value of tagging, see docs/using_tags.md for how to take advantage of the
  20. * system.
  21. */
  22. class XLocationIdResponseSubscriber implements EventSubscriberInterface
  23. {
  24. public const LOCATION_ID_HEADER = 'X-Location-Id';
  25. /** @var \FOS\HttpCache\ResponseTagger */
  26. private $responseTagger;
  27. /** @var \Ibexa\Contracts\Core\Repository\Repository */
  28. private $repository;
  29. public function __construct(ResponseTagger $responseTagger, Repository $repository)
  30. {
  31. $this->responseTagger = $responseTagger;
  32. $this->repository = $repository;
  33. }
  34. public static function getSubscribedEvents()
  35. {
  36. return [KernelEvents::RESPONSE => ['rewriteCacheHeader', 10]];
  37. }
  38. public function rewriteCacheHeader(ResponseEvent $event)
  39. {
  40. $response = $event->getResponse();
  41. if (!$response->headers->has(static::LOCATION_ID_HEADER)) {
  42. return;
  43. }
  44. @trigger_error(
  45. 'X-Location-Id is no longer preferred way to tag content responses, see ezplatform-http-cache/docs/using_tags.md',
  46. E_USER_DEPRECATED
  47. );
  48. // Map the tags, even if not officially supported, handle comma separated values as was possible with Varnish
  49. $tags = [];
  50. foreach (explode(',', $response->headers->get(static::LOCATION_ID_HEADER)) as $id) {
  51. $id = trim($id);
  52. try {
  53. /** @var $location \Ibexa\Contracts\Core\Repository\Values\Content\Location */
  54. $location = $this->repository->sudo(static function (Repository $repository) use ($id) {
  55. return $repository->getLocationService()->loadLocation($id);
  56. });
  57. $tags[] = ContentTagInterface::LOCATION_PREFIX . $location->id;
  58. $tags[] = ContentTagInterface::PARENT_LOCATION_PREFIX . $location->parentLocationId;
  59. foreach ($location->path as $pathItem) {
  60. $tags[] = ContentTagInterface::PATH_PREFIX . $pathItem;
  61. }
  62. $contentInfo = $location->getContentInfo();
  63. $tags[] = ContentTagInterface::CONTENT_PREFIX . $contentInfo->id;
  64. $tags[] = ContentTagInterface::CONTENT_TYPE_PREFIX . $contentInfo->contentTypeId;
  65. if ($contentInfo->mainLocationId !== $location->id) {
  66. $tags[] = ContentTagInterface::LOCATION_PREFIX . $contentInfo->mainLocationId;
  67. }
  68. } catch (NotFoundException $e) {
  69. $tags[] = ContentTagInterface::LOCATION_PREFIX . $id;
  70. $tags[] = ContentTagInterface::PATH_PREFIX . $id;
  71. }
  72. }
  73. $this->responseTagger->addTags($tags);
  74. $response->headers->remove(static::LOCATION_ID_HEADER);
  75. }
  76. }
  77. class_alias(XLocationIdResponseSubscriber::class, 'EzSystems\PlatformHttpCacheBundle\EventSubscriber\XLocationIdResponseSubscriber');