Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Fulltext.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\FullFactory;
9 use Magento\CatalogSearch\Model\Indexer\Scope\StateFactory;
14 
23 class Fulltext implements
27 {
31  const INDEXER_ID = 'catalogsearch_fulltext';
32 
36  protected $data;
37 
41  private $indexerHandlerFactory;
42 
46  private $fullAction;
47 
51  private $fulltextResource;
52 
56  private $indexSwitcher;
57 
61  private $indexScopeState;
62 
66  private $dimensionProvider;
67 
71  private $processManager;
72 
83  public function __construct(
84  FullFactory $fullActionFactory,
85  IndexerHandlerFactory $indexerHandlerFactory,
86  FulltextResource $fulltextResource,
87  IndexSwitcherInterface $indexSwitcher,
88  StateFactory $indexScopeStateFactory,
89  DimensionProviderInterface $dimensionProvider,
90  array $data,
91  ProcessManager $processManager = null
92  ) {
93  $this->fullAction = $fullActionFactory->create(['data' => $data]);
94  $this->indexerHandlerFactory = $indexerHandlerFactory;
95  $this->fulltextResource = $fulltextResource;
96  $this->data = $data;
97  $this->indexSwitcher = $indexSwitcher;
98  $this->indexScopeState = $indexScopeStateFactory->create();
99  $this->dimensionProvider = $dimensionProvider;
100  $this->processManager = $processManager ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
101  ProcessManager::class
102  );
103  }
104 
112  public function execute($entityIds)
113  {
114  foreach ($this->dimensionProvider->getIterator() as $dimension) {
115  $this->executeByDimensions($dimension, new \ArrayIterator($entityIds));
116  }
117  }
118 
124  public function executeByDimensions(array $dimensions, \Traversable $entityIds = null)
125  {
126  if (count($dimensions) > 1 || !isset($dimensions[StoreDimensionProvider::DIMENSION_NAME])) {
127  throw new \InvalidArgumentException('Indexer "' . self::INDEXER_ID . '" support only Store dimension');
128  }
129  $storeId = $dimensions[StoreDimensionProvider::DIMENSION_NAME]->getValue();
130  $saveHandler = $this->indexerHandlerFactory->create([
131  'data' => $this->data
132  ]);
133 
134  if (null === $entityIds) {
135  $this->indexScopeState->useTemporaryIndex();
136  $saveHandler->cleanIndex($dimensions);
137  $saveHandler->saveIndex($dimensions, $this->fullAction->rebuildStoreIndex($storeId));
138 
139  $this->indexSwitcher->switchIndex($dimensions);
140  $this->indexScopeState->useRegularIndex();
141 
142  $this->fulltextResource->resetSearchResultsByStore($storeId);
143  } else {
144  // internal implementation works only with array
145  $entityIds = iterator_to_array($entityIds);
146  $productIds = array_unique(
147  array_merge($entityIds, $this->fulltextResource->getRelationsByChild($entityIds))
148  );
149  if ($saveHandler->isAvailable($dimensions)) {
150  $saveHandler->deleteIndex($dimensions, new \ArrayIterator($productIds));
151  $saveHandler->saveIndex($dimensions, $this->fullAction->rebuildStoreIndex($storeId, $productIds));
152  }
153  }
154  }
155 
162  public function executeFull()
163  {
164  $userFunctions = [];
165  foreach ($this->dimensionProvider->getIterator() as $dimension) {
166  $userFunctions[] = function () use ($dimension) {
167  $this->executeByDimensions($dimension);
168  };
169  }
170  $this->processManager->execute($userFunctions);
171  }
172 
179  public function executeList(array $ids)
180  {
181  $this->execute($ids);
182  }
183 
190  public function executeRow($id)
191  {
192  $this->execute([$id]);
193  }
194 }
__construct(FullFactory $fullActionFactory, IndexerHandlerFactory $indexerHandlerFactory, FulltextResource $fulltextResource, IndexSwitcherInterface $indexSwitcher, StateFactory $indexScopeStateFactory, DimensionProviderInterface $dimensionProvider, array $data, ProcessManager $processManager=null)
Definition: Fulltext.php:83
$id
Definition: fieldset.phtml:14
executeByDimensions(array $dimensions, \Traversable $entityIds=null)
Definition: Fulltext.php:124