123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Indexer\Action;
- use Magento\Framework\App\ResourceConnection as AppResource;
- use Magento\Framework\App\ResourceConnection\SourceProviderInterface;
- use Magento\Framework\DB\Adapter\AdapterInterface;
- use Magento\Framework\DB\Ddl\Table;
- use Magento\Framework\Indexer\SaveHandler\IndexerInterface;
- use Magento\Framework\Stdlib\StringUtils as StdString;
- use Magento\Framework\Indexer\ActionInterface;
- use Magento\Framework\Indexer\FieldsetPool;
- use Magento\Framework\Indexer\HandlerPool;
- use Magento\Framework\Indexer\IndexStructureInterface;
- use Magento\Framework\Indexer\SaveHandlerFactory;
- use Magento\Framework\App\ResourceConnection\SourceFactory;
- use Magento\Framework\Indexer\HandlerInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- */
- class Base implements ActionInterface
- {
- /**
- * Prefix
- */
- const PREFIX = 'index_';
- /**
- * @var FieldsetPool
- */
- protected $fieldsetPool;
- /**
- * @var AdapterInterface
- * @deprecated 101.0.0
- */
- protected $connection;
- /**
- * @var SourceProviderInterface[]
- * @deprecated 101.0.0
- */
- protected $sources;
- /**
- * @var SourceProviderInterface
- */
- protected $primarySource;
- /**
- * @var HandlerInterface[]
- * @deprecated 101.0.0
- */
- protected $handlers;
- /**
- * @var array
- */
- protected $data;
- /**
- * @var array
- * @deprecated 101.0.0
- */
- protected $columnTypesMap = [
- 'varchar' => ['type' => Table::TYPE_TEXT, 'size' => 255],
- 'mediumtext' => ['type' => Table::TYPE_TEXT, 'size' => 16777216],
- 'text' => ['type' => Table::TYPE_TEXT, 'size' => 65536],
- ];
- /**
- * @var array
- * @deprecated 101.0.0
- */
- protected $filterColumns;
- /**
- * @var array
- * @deprecated 101.0.0
- */
- protected $searchColumns;
- /**
- * @var SourceFactory
- */
- protected $sourceFactory;
- /**
- * @var HandlerPool
- */
- protected $handlerPool;
- /**
- * @var SaveHandlerFactory
- */
- protected $saveHandlerFactory;
- /**
- * @var String
- * @deprecated 101.0.0
- */
- protected $string;
- /**
- * @var IndexStructureInterface
- */
- protected $indexStructure;
- /**
- * @var array
- * @deprecated 101.0.0
- */
- protected $filterable = [];
- /**
- * @var array
- * @deprecated 101.0.0
- */
- protected $searchable = [];
- /**
- * @var IndexerInterface
- */
- protected $saveHandler;
- /**
- * @var string
- */
- protected $tableAlias = 'main_table';
- /**
- * @param AppResource $resource
- * @param SourceFactory $sourceFactory
- * @param HandlerPool $handlerPool
- * @param SaveHandlerFactory $saveHandlerFactory
- * @param FieldsetPool $fieldsetPool
- * @param StdString $string
- * @param IndexStructureInterface $indexStructure
- * @param array $data
- */
- public function __construct(
- AppResource $resource,
- SourceFactory $sourceFactory,
- HandlerPool $handlerPool,
- SaveHandlerFactory $saveHandlerFactory,
- FieldsetPool $fieldsetPool,
- StdString $string,
- IndexStructureInterface $indexStructure,
- $data = []
- ) {
- $this->connection = $resource->getConnection();
- $this->fieldsetPool = $fieldsetPool;
- $this->data = $data;
- $this->sourceFactory = $sourceFactory;
- $this->handlerPool = $handlerPool;
- $this->saveHandlerFactory = $saveHandlerFactory;
- $this->string = $string;
- $this->indexStructure = $indexStructure;
- }
- /**
- * Execute
- *
- * @param null|int|array $ids
- * @return void
- */
- protected function execute(array $ids = [])
- {
- $this->prepareFields();
- if (!count($ids)) {
- $this->getSaveHandler()->cleanIndex([]);
- }
- $this->getSaveHandler()->deleteIndex([], new \ArrayObject($ids));
- $this->getSaveHandler()->saveIndex([], $this->prepareDataSource($ids));
- }
- /**
- * Execute full indexation
- *
- * @return void
- */
- public function executeFull()
- {
- $this->execute();
- }
- /**
- * Execute partial indexation by ID list
- *
- * @param int[] $ids
- * @return void
- */
- public function executeList(array $ids)
- {
- $this->execute($ids);
- }
- /**
- * Execute partial indexation by ID
- *
- * @param int $id
- * @return void
- */
- public function executeRow($id)
- {
- $this->execute([$id]);
- }
- /**
- * Prepare select query
- *
- * @param array|int|null $ids
- * @return SourceProviderInterface
- */
- protected function prepareDataSource(array $ids = [])
- {
- return !count($ids)
- ? $this->createResultCollection()
- : $this->createResultCollection()->addFieldToFilter($this->getPrimaryResource()->getIdFieldName(), $ids);
- }
- /**
- * Return index table name
- *
- * @return string
- */
- protected function getTableName()
- {
- return self::PREFIX . $this->getPrimaryResource()->getMainTable();
- }
- /**
- * Return save handler
- *
- * @return IndexerInterface
- */
- protected function getSaveHandler()
- {
- if ($this->saveHandler === null) {
- $this->saveHandler = $this->saveHandlerFactory->create(
- $this->data['saveHandler'],
- [
- 'indexStructure' => $this->indexStructure,
- 'data' => $this->data,
- ]
- );
- }
- return $this->saveHandler;
- }
- /**
- * Return primary source provider
- *
- * @return SourceProviderInterface
- */
- protected function getPrimaryResource()
- {
- return $this->getPrimaryFieldset()['source'];
- }
- /**
- * Return primary fieldset
- *
- * @return []
- */
- protected function getPrimaryFieldset()
- {
- return $this->data['fieldsets'][0];
- }
- /**
- * Create select from indexer configuration
- *
- * @return SourceProviderInterface
- */
- protected function createResultCollection()
- {
- $select = $this->getPrimaryResource()->getSelect();
- $select->reset(\Magento\Framework\DB\Select::COLUMNS);
- $select->columns($this->getPrimaryResource()->getIdFieldName());
- foreach ($this->data['fieldsets'] as $fieldset) {
- if (isset($fieldset['references'])) {
- foreach ($fieldset['references'] as $reference) {
- $source = $fieldset['source'];
- /** @var SourceProviderInterface $source */
- $currentEntityName = $source->getMainTable();
- $alias = $this->getPrimaryFieldset()['name'] == $reference['fieldset']
- ? $this->tableAlias
- : $reference['fieldset'];
- $select->joinLeft(
- [$fieldset['name'] => $currentEntityName],
- new \Zend_Db_Expr(
- $fieldset['name'] . '.' . $reference['from'] . '=' . $alias . '.' . $reference['to']
- ),
- null
- );
- }
- }
- foreach ($fieldset['fields'] as $field) {
- $handler = $field['handler'];
- /** @var HandlerInterface $handler */
- $handler->prepareSql(
- $this->getPrimaryResource(),
- $this->getPrimaryFieldset()['name'] == $fieldset['name'] ? $this->tableAlias : $fieldset['name'],
- $field
- );
- }
- }
- return $this->getPrimaryResource();
- }
- /**
- * Prepare configuration data
- *
- * @return void
- */
- protected function prepareFields()
- {
- foreach ($this->data['fieldsets'] as $fieldsetName => $fieldset) {
- $this->data['fieldsets'][$fieldsetName]['source'] = $this->sourceFactory->create($fieldset['source']);
- if (isset($fieldset['provider'])) {
- $fieldsetObject = $this->fieldsetPool->get($fieldset['provider']);
- $this->data['fieldsets'][$fieldsetName] =
- $fieldsetObject->addDynamicData($this->data['fieldsets'][$fieldsetName]);
- }
- foreach ($this->data['fieldsets'][$fieldsetName]['fields'] as $fieldName => $field) {
- $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['origin'] =
- $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['origin']
- ?: $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['name'];
- if ($fieldsetName != 0) {
- $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['name'] =
- $this->data['fieldsets'][$fieldsetName]['name'] . '_'
- . $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['name'];
- }
- $this->saveFieldByType($field);
- $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['handler'] =
- $this->handlerPool->get($field['handler']);
- $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['dataType'] =
- isset($field['dataType']) ? $field['dataType'] : 'varchar';
- }
- }
- }
- /**
- * Save field by type
- *
- * @param array $field
- * @return void
- *
- * @deprecated 101.0.0
- */
- protected function saveFieldByType($field)
- {
- switch ($field['type']) {
- case 'filterable':
- $this->filterable[] = $field;
- break;
- case 'searchable':
- $this->searchable[] = $field;
- break;
- }
- }
- }
|