-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/SamJUK/magento2-reindex int…
…o SamJUK-master
- Loading branch information
Showing
18 changed files
with
469 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
|
||
namespace MagePal\Reindex\Api; | ||
|
||
/** | ||
* Handles the implementation of the reindex logic | ||
* | ||
* @api | ||
*/ | ||
interface ReindexInterface | ||
{ | ||
/** | ||
* @param array|null $indexIds | ||
*/ | ||
public function reindex(array $indexIds = null) : void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
|
||
namespace MagePal\Reindex\Api; | ||
|
||
/** | ||
* Handles strategy for reindexing from backend. Process now, defer to message queue etc. | ||
* | ||
* @api | ||
*/ | ||
interface StrategyInterface | ||
{ | ||
/** | ||
* @param array|null $indexIds | ||
*/ | ||
public function process(array $indexIds = null) : void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
|
||
namespace MagePal\Reindex\Api; | ||
|
||
/** | ||
* @api | ||
*/ | ||
interface SubscriberInterface | ||
{ | ||
/** | ||
* @param array $indices | ||
*/ | ||
public function processMessage(array $indices) : void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal, LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
namespace MagePal\Reindex\Block\Adminhtml\System\Config\Source; | ||
|
||
use Magento\Framework\Data\OptionSourceInterface; | ||
|
||
class Strategy implements OptionSourceInterface | ||
{ | ||
/** @var array */ | ||
private $strategies; | ||
|
||
/** | ||
* Strategy constructor. | ||
* @param array $strategies | ||
*/ | ||
public function __construct(array $strategies) | ||
{ | ||
$this->strategies = $strategies; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function toOptionArray() : array | ||
{ | ||
$options = []; | ||
foreach ($this->strategies as $key => $label) { | ||
$options[] = ['value' => $key, 'label' => $label]; | ||
} | ||
return $options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal, LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
namespace MagePal\Reindex\Model; | ||
|
||
use Magento\Indexer\Model\IndexerFactory; | ||
use MagePal\Reindex\Api\ReindexInterface; | ||
|
||
class Reindex implements ReindexInterface | ||
{ | ||
/** @var IndexerFactory */ | ||
private $indexerFactory; | ||
|
||
/** | ||
* Reindex constructor. | ||
* @param IndexerFactory $indexerFactory | ||
*/ | ||
public function __construct(IndexerFactory $indexerFactory) | ||
{ | ||
$this->indexerFactory = $indexerFactory; | ||
} | ||
|
||
/** | ||
* Implements synchronous reindexing | ||
* | ||
* @param array|null $indexIds | ||
*/ | ||
public function reindex(array $indexIds = null) : void | ||
{ | ||
foreach ($indexIds as $index) { | ||
$indexer = $this->indexerFactory->create(); | ||
$indexer->load($index)->reindexAll(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal, LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
namespace MagePal\Reindex\Model\Strategies; | ||
|
||
use MagePal\Reindex\Api\StrategyInterface; | ||
use Magento\Framework\MessageQueue\PublisherInterface; | ||
|
||
class Deferred implements StrategyInterface | ||
{ | ||
const STRATEGY_KEY = 'deferred'; | ||
const TOPIC_NAME = 'magepal.reindex'; | ||
|
||
/** @var PublisherInterface */ | ||
private $publisher; | ||
|
||
/** | ||
* Deferred constructor. | ||
* @param PublisherInterface $publisher | ||
*/ | ||
public function __construct(PublisherInterface $publisher) | ||
{ | ||
$this->publisher = $publisher; | ||
} | ||
|
||
/** | ||
* Push the indexIDs to our message queue to processed by another process | ||
* | ||
* @param array|null $indexIds | ||
*/ | ||
public function process(array $indexIds = null) : void | ||
{ | ||
$this->publisher->publish(self::TOPIC_NAME, $indexIds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal, LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
namespace MagePal\Reindex\Model\Strategies; | ||
|
||
use MagePal\Reindex\Api\StrategyInterface; | ||
use MagePal\Reindex\Model\Reindex; | ||
|
||
class Standard implements StrategyInterface | ||
{ | ||
const STRATEGY_KEY = 'standard'; | ||
|
||
/** @var Reindex */ | ||
private $reindexService; | ||
|
||
/** | ||
* Standard constructor. | ||
* @param Reindex $reindexService | ||
*/ | ||
public function __construct(Reindex $reindexService) | ||
{ | ||
$this->reindexService = $reindexService; | ||
} | ||
|
||
/** | ||
* Handle the reindex within the current process | ||
* | ||
* @param array|null $indexIds | ||
*/ | ||
public function process(array $indexIds = null) : void | ||
{ | ||
$this->reindexService->reindex($indexIds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal, LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
namespace MagePal\Reindex\Model; | ||
|
||
use MagePal\Reindex\Api\StrategyInterface; | ||
|
||
class Strategy implements StrategyInterface | ||
{ | ||
/** @var StrategyResolver */ | ||
private $resolver; | ||
|
||
/** | ||
* Strategy constructor. | ||
* @param StrategyResolver $resolver | ||
*/ | ||
public function __construct(StrategyResolver $resolver) | ||
{ | ||
$this->resolver = $resolver; | ||
} | ||
|
||
/** | ||
* @param array|null $indexIds | ||
* @throws \Magento\Framework\Exception\InputException | ||
*/ | ||
public function process(array $indexIds = null) : void | ||
{ | ||
$this->resolver->resolveActive()->process($indexIds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Copyright © MagePal, LLC. All rights reserved. | ||
* See COPYING.txt for license details. | ||
* http://www.magepal.com | [email protected] | ||
*/ | ||
namespace MagePal\Reindex\Model; | ||
|
||
use MagePal\Reindex\Api\StrategyInterface; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Exception\InputException; | ||
|
||
class StrategyResolver | ||
{ | ||
const XML_STRATEGY_PATH = 'magepal_reindex/about/strategy'; | ||
|
||
/** @var ScopeConfigInterface */ | ||
private $scopeConfig; | ||
|
||
/** @var array */ | ||
private $strategies; | ||
|
||
/** | ||
* StrategyResolver constructor. | ||
* @param ScopeConfigInterface $scopeConfigInterface | ||
* @param array $strategies | ||
*/ | ||
public function __construct( | ||
ScopeConfigInterface $scopeConfigInterface, | ||
array $strategies | ||
) { | ||
$this->scopeConfig = $scopeConfigInterface; | ||
$this->strategies = $strategies; | ||
} | ||
|
||
/** | ||
* Resolve a strategy key to its correct class | ||
* | ||
* @param string $strategy | ||
* @return StrategyInterface | ||
* @throws InputException | ||
*/ | ||
public function resolve(string $strategy) : StrategyInterface | ||
{ | ||
if (!array_key_exists($strategy, $this->strategies)) { | ||
throw new InputException(__("Invalid Strategy Key: $strategy")); | ||
} | ||
|
||
return $this->strategies[$strategy]; | ||
} | ||
|
||
/** | ||
* Handle resolving the current active strategy from system config to a class | ||
* | ||
* @return StrategyInterface | ||
* @throws InputException | ||
*/ | ||
public function resolveActive() : StrategyInterface | ||
{ | ||
return $this->resolve($this->getActiveStrategy()); | ||
} | ||
|
||
/** | ||
* Get the active indexation strategy key from the backend | ||
* | ||
* @return string | ||
*/ | ||
public function getActiveStrategy() : string | ||
{ | ||
return $this->scopeConfig->getValue(self::XML_STRATEGY_PATH); | ||
} | ||
} |
Oops, something went wrong.