-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from andresams/CLI-COMMAND
Added CLI command "cache:refresh:invalidated"
- Loading branch information
Showing
5 changed files
with
220 additions
and
8 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,83 @@ | ||
<?php | ||
/** | ||
* Copyright © Pronko Consulting (https://www.pronkoconsulting.com) | ||
* See LICENSE for the license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Pronko\SelectiveCache\Console\Command; | ||
|
||
use Magento\Framework\DataObject; | ||
use Magento\Framework\Event\Manager as EventManager; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Command for flushing invalidated cache types | ||
*/ | ||
class CacheFlushInvalidatedCommand extends Command | ||
{ | ||
/** | ||
* @var EventManager | ||
*/ | ||
private $eventManager; | ||
|
||
/** | ||
* CacheFlushInvalidatedCommand constructor. | ||
* @param EventManager $eventManager | ||
*/ | ||
public function __construct( | ||
EventManager $eventManager | ||
) { | ||
$this->eventManager = $eventManager; | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->setName('cache:refresh:invalidated'); | ||
$this->setDescription('Flushes cache storage used by currently invalidated cache type(s)'); | ||
parent::configure(); | ||
} | ||
|
||
/** | ||
* Flushes invalidated cache types | ||
* | ||
* @param array $cacheTypes | ||
* @return void | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
/** @var DataObject $cacheContainer */ | ||
$cacheContainer = new DataObject(); | ||
|
||
$this->eventManager | ||
->dispatch( | ||
'cache_flush_invalidated', | ||
['cache_container' => $cacheContainer] | ||
); | ||
|
||
$output->writeln($this->getDisplayMessage($cacheContainer->getData('labels'))); | ||
} | ||
|
||
/** | ||
* Returns an output message to be displayed on the CLI | ||
* | ||
* @param array $labels | ||
* @return string | ||
*/ | ||
protected function getDisplayMessage(array $labels) | ||
{ | ||
if (!empty($labels)) { | ||
$message = "Flushed invalidated cache types: \n" . implode("\n", $labels); | ||
} else { | ||
$message = 'No invalidated caches were found.'; | ||
} | ||
|
||
return $message; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
/** | ||
* Copyright © Pronko Consulting (https://www.pronkoconsulting.com) | ||
* See LICENSE for the license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Pronko\SelectiveCache\Observer; | ||
|
||
use Magento\Framework\App\Cache\TypeListInterface; | ||
use Magento\Framework\DataObject; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
/** | ||
* Class FlushInvalidatedCache | ||
* @package Pronko\SelectiveCache\Observer | ||
*/ | ||
class FlushInvalidatedCache implements ObserverInterface | ||
{ | ||
/** | ||
* @var TypeListInterface | ||
*/ | ||
protected $_cacheTypeList; | ||
|
||
/** | ||
* FlushInvalidatedCache constructor. | ||
* @param TypeListInterfaceAlias $cacheTypeList | ||
*/ | ||
public function __construct(TypeListInterface $cacheTypeList) | ||
{ | ||
$this->_cacheTypeList = $cacheTypeList; | ||
} | ||
|
||
/** | ||
* Flush Invalidated cache | ||
* | ||
* @param Observer $observer | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** @var DataObject $cacheContainer */ | ||
$cacheContainer = $observer->getEvent()->getCacheContainer(); | ||
|
||
$cacheLabels = []; | ||
/** @var DataObject $invalidatedType */ | ||
foreach ($this->_cacheTypeList->getInvalidated() as $invalidatedType) { | ||
$this->_cacheTypeList->cleanType($invalidatedType->getData('id')); | ||
$cacheLabels[] = $invalidatedType->getData('cache_type'); | ||
} | ||
$cacheContainer->setData('labels', $cacheLabels); | ||
} | ||
} |
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,12 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Pronko Consulting (https://www.pronkoconsulting.com) | ||
* See LICENSE for the license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="cache_flush_invalidated"> | ||
<observer name="flush_invalidated" instance="Pronko\SelectiveCache\Observer\FlushInvalidatedCache"/> | ||
</event> | ||
</config> |