AbstractIndexerCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Console\Command;
  7. use Magento\Backend\App\Area\FrontNameResolver;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Symfony\Component\Console\Command\Command;
  10. use Magento\Framework\Indexer\IndexerInterface;
  11. use Magento\Framework\App\ObjectManagerFactory;
  12. /**
  13. * An Abstract class for Indexer related commands.
  14. */
  15. abstract class AbstractIndexerCommand extends Command
  16. {
  17. /**
  18. * @var ObjectManagerFactory
  19. */
  20. private $objectManagerFactory;
  21. /**
  22. * @var ObjectManagerInterface
  23. */
  24. private $objectManager;
  25. /**
  26. * @var \Magento\Indexer\Model\Indexer\CollectionFactory
  27. */
  28. private $collectionFactory;
  29. /**
  30. * Constructor
  31. *
  32. * @param ObjectManagerFactory $objectManagerFactory
  33. * @param \Magento\Indexer\Model\Indexer\CollectionFactory|null $collectionFactory
  34. */
  35. public function __construct(
  36. ObjectManagerFactory $objectManagerFactory,
  37. \Magento\Indexer\Model\Indexer\CollectionFactory $collectionFactory = null
  38. ) {
  39. $this->objectManagerFactory = $objectManagerFactory;
  40. $this->collectionFactory = $collectionFactory;
  41. parent::__construct();
  42. }
  43. /**
  44. * Return the array of all indexers with keys as indexer ids.
  45. *
  46. * @return IndexerInterface[]
  47. */
  48. protected function getAllIndexers()
  49. {
  50. $indexers = $this->getCollectionFactory()->create()->getItems();
  51. return array_combine(
  52. array_map(
  53. function ($item) {
  54. /** @var IndexerInterface $item */
  55. return $item->getId();
  56. },
  57. $indexers
  58. ),
  59. $indexers
  60. );
  61. }
  62. /**
  63. * Gets initialized object manager
  64. *
  65. * @return ObjectManagerInterface
  66. */
  67. protected function getObjectManager()
  68. {
  69. if (null == $this->objectManager) {
  70. $area = FrontNameResolver::AREA_CODE;
  71. $this->objectManager = $this->objectManagerFactory->create($_SERVER);
  72. /** @var \Magento\Framework\App\State $appState */
  73. $appState = $this->objectManager->get(\Magento\Framework\App\State::class);
  74. $appState->setAreaCode($area);
  75. $configLoader = $this->objectManager->get(\Magento\Framework\ObjectManager\ConfigLoaderInterface::class);
  76. $this->objectManager->configure($configLoader->load($area));
  77. }
  78. return $this->objectManager;
  79. }
  80. /**
  81. * Get collection factory
  82. *
  83. * @return \Magento\Indexer\Model\Indexer\CollectionFactory
  84. * @deprecated 100.2.0
  85. */
  86. private function getCollectionFactory()
  87. {
  88. if (null === $this->collectionFactory) {
  89. $this->collectionFactory = $this->getObjectManager()
  90. ->get(\Magento\Indexer\Model\Indexer\CollectionFactory::class);
  91. }
  92. return $this->collectionFactory;
  93. }
  94. }