vendor/shopware/core/Framework/DataAbstractionLayer/Indexing/IndexerRegistry.php line 77

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Indexing;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  6. /**
  7.  * @deprecated tag:v6.3.0 - Use \Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexerRegistry instead
  8.  */
  9. class IndexerRegistry implements EventSubscriberInterfaceIndexerRegistryInterface
  10. {
  11.     /**
  12.      * 0    => Shopware\Core\Content\ProductStream\DataAbstractionLayer\Indexing\ProductStreamIndexer
  13.      * 0    => Shopware\Core\Content\Rule\DataAbstractionLayer\Indexing\RulePayloadIndexer
  14.      * 200  => Shopware\Core\Framework\Search\DataAbstractionLayer\Indexing\SearchKeywordIndexer
  15.      * 300  => Shopware\Core\Content\Product\DataAbstractionLayer\Indexing\ProductListingPriceIndexer
  16.      * 400  => Shopware\Core\Content\Product\DataAbstractionLayer\Indexing\ProductPropertyIndexer
  17.      * 500  => Shopware\Core\Content\Product\DataAbstractionLayer\Indexing\ProductCategoryTreeIndexer
  18.      * 500  => Shopware\Core\Content\Media\DataAbstractionLayer\Indexing\MediaFolderConfigIndexer
  19.      * 900  => Shopware\Core\Content\Product\DataAbstractionLayer\Indexing\ProductOptionIndexer
  20.      * 1000 => Shopware\Core\Framework\DataAbstractionLayer\Dbal\Indexing\ChildCountIndexer
  21.      * 1000 => Shopware\Core\Framework\DataAbstractionLayer\Dbal\Indexing\TreeIndexer
  22.      * 1500 => Shopware\Core\Framework\DataAbstractionLayer\Dbal\Indexing\InheritanceIndexer
  23.      *
  24.      * @var IndexerInterface[]
  25.      */
  26.     private $indexer;
  27.     /**
  28.      * @var EventDispatcherInterface
  29.      */
  30.     private $eventDispatcher;
  31.     /**
  32.      * Internal working state to prevent endless loop if an indexer fires the EntityWrittenContainerEvent
  33.      *
  34.      * @var bool
  35.      */
  36.     private $working false;
  37.     public function __construct(iterable $indexerEventDispatcherInterface $eventDispatcher)
  38.     {
  39.         $this->indexer $indexer;
  40.         $this->eventDispatcher $eventDispatcher;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             EntityWrittenContainerEvent::class => [
  46.                 ['refresh'500],
  47.             ],
  48.         ];
  49.     }
  50.     public function index(\DateTimeInterface $timestamp): void
  51.     {
  52.         if ($this->working) {
  53.             return;
  54.         }
  55.         $preEvent = new IndexerRegistryStartEvent(new \DateTimeImmutable());
  56.         $this->eventDispatcher->dispatch($preEvent);
  57.         $this->working true;
  58.         foreach ($this->indexer as $indexer) {
  59.             $indexer->index($timestamp);
  60.         }
  61.         $this->working false;
  62.         $preEvent = new IndexerRegistryEndEvent(new \DateTimeImmutable());
  63.         $this->eventDispatcher->dispatch($preEvent);
  64.     }
  65.     public function refresh(EntityWrittenContainerEvent $event): void
  66.     {
  67.         if ($this->working) {
  68.             return;
  69.         }
  70.         $preEvent = new IndexerRegistryStartEvent(new \DateTimeImmutable(), $event->getContext());
  71.         $this->eventDispatcher->dispatch($preEvent);
  72.         $this->working true;
  73.         foreach ($this->indexer as $indexer) {
  74.             $indexer->refresh($event);
  75.         }
  76.         $this->working false;
  77.         $preEvent = new IndexerRegistryEndEvent(new \DateTimeImmutable(), $event->getContext());
  78.         $this->eventDispatcher->dispatch($preEvent);
  79.     }
  80.     public function partial(?string $lastIndexer, ?array $lastId, \DateTimeInterface $timestamp): ?IndexerRegistryPartialResult
  81.     {
  82.         $indexers $this->getIndexers();
  83.         foreach ($indexers as $index => $indexer) {
  84.             if (!$lastIndexer) {
  85.                 return $this->doPartial($indexer$lastId$index$timestamp);
  86.             }
  87.             if ($lastIndexer === $indexer::getName()) {
  88.                 return $this->doPartial($indexer$lastId$index$timestamp);
  89.             }
  90.         }
  91.         return null;
  92.     }
  93.     private function doPartial(IndexerInterface $indexer, ?array $lastId$index, \DateTimeInterface $timestamp): ?IndexerRegistryPartialResult
  94.     {
  95.         $nextId $indexer->partial($lastId$timestamp);
  96.         $next $indexer::getName();
  97.         if ($nextId !== null) {
  98.             return new IndexerRegistryPartialResult($next$nextId);
  99.         }
  100.         ++$index;
  101.         $indexers $this->getIndexers();
  102.         if (!isset($indexers[$index])) {
  103.             return null;
  104.         }
  105.         return new IndexerRegistryPartialResult($indexers[$index]::getName(), null);
  106.     }
  107.     private function getIndexers()
  108.     {
  109.         if (!is_array($this->indexer)) {
  110.             return array_values(iterator_to_array($this->indexer));
  111.         }
  112.         return array_values($this->indexer);
  113.     }
  114. }