Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractIndexerCommand.php
Go to the documentation of this file.
1 <?php
7 
10 use Symfony\Component\Console\Command\Command;
13 
17 abstract class AbstractIndexerCommand extends Command
18 {
22  private $objectManagerFactory;
23 
27  private $objectManager;
28 
32  private $collectionFactory;
33 
40  public function __construct(
41  ObjectManagerFactory $objectManagerFactory,
42  \Magento\Indexer\Model\Indexer\CollectionFactory $collectionFactory = null
43  ) {
44  $this->objectManagerFactory = $objectManagerFactory;
45  $this->collectionFactory = $collectionFactory;
46  parent::__construct();
47  }
48 
54  protected function getAllIndexers()
55  {
56  $indexers = $this->getCollectionFactory()->create()->getItems();
57  return array_combine(
58  array_map(
59  function ($item) {
61  return $item->getId();
62  },
63  $indexers
64  ),
65  $indexers
66  );
67  }
68 
74  protected function getObjectManager()
75  {
76  if (null == $this->objectManager) {
78  $this->objectManager = $this->objectManagerFactory->create($_SERVER);
80  $appState = $this->objectManager->get(\Magento\Framework\App\State::class);
81  $appState->setAreaCode($area);
82  $configLoader = $this->objectManager->get(\Magento\Framework\ObjectManager\ConfigLoaderInterface::class);
83  $this->objectManager->configure($configLoader->load($area));
84  }
85  return $this->objectManager;
86  }
87 
94  private function getCollectionFactory()
95  {
96  if (null === $this->collectionFactory) {
97  $this->collectionFactory = $this->getObjectManager()
98  ->get(\Magento\Indexer\Model\Indexer\CollectionFactory::class);
99  }
100  return $this->collectionFactory;
101  }
102 }
$objectManagerFactory
__construct(ObjectManagerFactory $objectManagerFactory, \Magento\Indexer\Model\Indexer\CollectionFactory $collectionFactory=null)