src/Core/Framework/Adapter/Translation/TranslatorCacheInvalidate.php line 36

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Translation;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetDefinition;
  8. use Shopware\Core\System\Snippet\SnippetDefinition;
  9. use Shopware\Core\System\Snippet\SnippetEvents;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * @internal
  13.  */
  14. #[Package('core')]
  15. class TranslatorCacheInvalidate implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(private readonly CacheInvalidator $cacheInvalidator, private readonly Connection $connection)
  21.     {
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             SnippetEvents::SNIPPET_WRITTEN_EVENT => 'invalidate',
  27.             SnippetEvents::SNIPPET_DELETED_EVENT => 'invalidate',
  28.             SnippetEvents::SNIPPET_SET_DELETED_EVENT => 'invalidate',
  29.         ];
  30.     }
  31.     public function invalidate(EntityWrittenEvent $event): void
  32.     {
  33.         if ($event->getEntityName() === SnippetSetDefinition::ENTITY_NAME) {
  34.             $this->clearCache($event->getIds());
  35.             return;
  36.         }
  37.         if ($event->getEntityName() === SnippetDefinition::ENTITY_NAME) {
  38.             $snippetIds $event->getIds();
  39.             $setIds $this->connection->fetchFirstColumn(
  40.                 'SELECT LOWER(HEX(snippet_set_id)) FROM snippet WHERE HEX(id) IN (:ids)',
  41.                 ['ids' => $snippetIds],
  42.                 ['ids' => Connection::PARAM_STR_ARRAY]
  43.             );
  44.             $this->clearCache($setIds);
  45.         }
  46.     }
  47.     /**
  48.      * @param array<string> $snippetSetIds
  49.      */
  50.     private function clearCache(array $snippetSetIds): void
  51.     {
  52.         $snippetSetIds array_unique($snippetSetIds);
  53.         $snippetSetCacheKeys array_map(fn (string $setId) => 'translation.catalog.' $setId$snippetSetIds);
  54.         $this->cacheInvalidator->invalidate($snippetSetCacheKeystrue);
  55.     }
  56. }