vendor/sulu/sulu/src/Sulu/Bundle/WebsiteBundle/EventSubscriber/DomainEventEventSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\WebsiteBundle\EventSubscriber;
  11. use Sulu\Bundle\ActivityBundle\Application\Dispatcher\DomainEventDispatcherInterface;
  12. use Sulu\Bundle\WebsiteBundle\Domain\Event\CacheClearedEvent;
  13. use Sulu\Bundle\WebsiteBundle\Event\CacheClearEvent;
  14. use Sulu\Bundle\WebsiteBundle\Events;
  15. use Sulu\Bundle\WebsiteBundle\ReferenceStore\WebspaceReferenceStore;
  16. use Sulu\Component\Webspace\Manager\WebspaceManagerInterface;
  17. use Sulu\Component\Webspace\Webspace;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. /**
  20. * @internal
  21. */
  22. class DomainEventEventSubscriber implements EventSubscriberInterface
  23. {
  24. /**
  25. * @var DomainEventDispatcherInterface
  26. */
  27. private $domainEventDispatcher;
  28. /**
  29. * @var WebspaceManagerInterface
  30. */
  31. private $webspaceManager;
  32. public function __construct(
  33. DomainEventDispatcherInterface $domainEventDispatcher,
  34. WebspaceManagerInterface $webspaceManager
  35. ) {
  36. $this->domainEventDispatcher = $domainEventDispatcher;
  37. $this->webspaceManager = $webspaceManager;
  38. }
  39. public static function getSubscribedEvents()
  40. {
  41. return [
  42. Events::CACHE_CLEAR => 'onCacheClear',
  43. ];
  44. }
  45. public function onCacheClear(CacheClearEvent $event): void
  46. {
  47. $tags = $event->getTags();
  48. if (null === $tags || 0 === \count($tags)) {
  49. /** @var Webspace $webspace */
  50. foreach ($this->webspaceManager->getWebspaceCollection() as $webspace) {
  51. $this->domainEventDispatcher->dispatch(new CacheClearedEvent($webspace->getKey(), null));
  52. }
  53. return;
  54. }
  55. foreach ($tags as $tag) {
  56. $webspaceKey = $this->getWebspaceKeyFromTag($tag);
  57. if ($webspaceKey) {
  58. $this->domainEventDispatcher->dispatch(new CacheClearedEvent($webspaceKey, $tags));
  59. }
  60. }
  61. }
  62. private function getWebspaceKeyFromTag(string $tag): ?string
  63. {
  64. $parts = \explode('-', $tag, 2);
  65. if (!isset($parts[1]) || WebspaceReferenceStore::WEBSPACE_REFERENCE_ALIAS !== $parts[0]) {
  66. return null;
  67. }
  68. return $parts[1];
  69. }
  70. }