Skip to content

Commit

Permalink
Improved LiteMage cli flush
Browse files Browse the repository at this point in the history
  • Loading branch information
litespeedtech committed Feb 13, 2024
1 parent 20005ef commit 3043f93
Show file tree
Hide file tree
Showing 11 changed files with 460 additions and 149 deletions.
105 changes: 105 additions & 0 deletions Console/Command/AbstractLitemageCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* LiteMage
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) LiteSpeed Technologies, Inc. All rights reserved. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

namespace Litespeed\Litemage\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;


/**
* Command for flush litemage cache by tags
*/
abstract class AbstractLitemageCommand extends Command
{
/**
* @var \Magento\Framework\Event\ManagerInterface
*/
protected $eventManager;

/**
*
* @var string
*/
protected $type;
/**
*
* @var array
*/
protected $tags;
/**
*
* @var string
*/
protected $tag_format;

/**
* @param EventManagerInterface $eventManager
*/
public function __construct(EventManagerInterface $eventManager)
{
$this->eventManager = $eventManager;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();
}

/**
* Perform a cache management action on cache types
*
* @return void
*
abstract protected function performAction(); */

/**
* Get display message
*
* @return string
*/
abstract protected function getDisplayMessage();

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getInputList($input);
$param = ['tags' => $this->tags,
'reason' => 'cli command: ' . $this->getName()];
$this->eventManager->dispatch('litemage_cli_purge', $param);
$output->writeln($this->getDisplayMessage());
}

protected function getInputList(InputInterface $input)
{
$list = $input->getArgument($this->type);
if (!empty($list)) {
$list = array_filter(array_map('trim', $list), 'strlen');
}
if (empty($list)) {
throw new \InvalidArgumentException(
"Missing the required space-separated list");
}
foreach ($list as $tag) {
if (!preg_match($this->tag_format, $tag)) {
throw new \InvalidArgumentException(
"Tag [$tag] contains invalid characters.");
}
}
$this->tags = $list;
}

}
49 changes: 49 additions & 0 deletions Console/Command/LitemageFlushCats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* LiteMage
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) LiteSpeed Technologies, Inc. All rights reserved. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

namespace Litespeed\Litemage\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;


/**
* Command for flush litemage cache by category IDs
*/
class LitemageFlushCats extends AbstractLitemageCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->type = 'cats';
$this->tag_format = '/^[\d+]+$/';
$this->setName('cache:litemage:flush:cats');
$this->setDescription('Flushes LiteMage cache by a list of category IDs');
$this->addArgument(
'cats',
InputArgument::IS_ARRAY,
'Space-separated list of category IDs (integer).'
);
parent::configure();
}

protected function getInputList(InputInterface $input)
{
parent::getInputList($input);
$this->tags = array_map(function($value) {
return 'C' . $value;
}, $this->tags);
}

protected function getDisplayMessage()
{
return 'Flushed LiteMage cache by category IDs.';
}
}
50 changes: 50 additions & 0 deletions Console/Command/LitemageFlushProds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* LiteMage
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) LiteSpeed Technologies, Inc. All rights reserved. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

namespace Litespeed\Litemage\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;


/**
* Command for flush litemage cache by product IDs
*/
class LitemageFlushProds extends AbstractLitemageCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->type = 'prods';
$this->tag_format = '/^[\d+]+$/';
$this->setName('cache:litemage:flush:prods');
$this->setDescription('Flushes LiteMage cache by a list of product IDs');
$this->addArgument(
'prods',
InputArgument::IS_ARRAY,
'Space-separated list of product IDs (integer).'
);
parent::configure();
}

protected function getInputList(InputInterface $input)
{
parent::getInputList($input);
$this->tags = array_map(function($value) {
return 'P' . $value;
}, $this->tags);

}

protected function getDisplayMessage()
{
return 'Flushed LiteMage cache by product IDs.';
}
}
40 changes: 40 additions & 0 deletions Console/Command/LitemageFlushTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* LiteMage
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) LiteSpeed Technologies, Inc. All rights reserved. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

namespace Litespeed\Litemage\Console\Command;

use Symfony\Component\Console\Input\InputArgument;


/**
* Command for flush litemage cache by tags
*/
class LitemageFlushTags extends AbstractLitemageCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->type = 'tags';
$this->tag_format = '/^[a-zA-Z\d_-]+$/';
$this->setName('cache:litemage:flush:tags');
$this->setDescription('Flushes LiteMage cache by a list of tags');
$this->addArgument(
'tags',
InputArgument::IS_ARRAY,
'Space-separated list of cache tags.'
);
parent::configure();
}

protected function getDisplayMessage()
{
return 'Flushed LiteMage cache by tags.';
}
}
14 changes: 14 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Config
private const CFG_DEBUGON = 'debug' ;
private const CFG_FRONT_STORE_ID = 'frontend_store_id';
private const CFG_SERVER_IP = 'server_ip';
private const CFG_BASIC_AUTH = 'basic_auth';

//const CFG_ADMINIPS = 'admin_ips';
private const CFG_PUBLICTTL = 'public_ttl';
Expand Down Expand Up @@ -279,6 +280,17 @@ public function getServerIp()
return $this->getConf(self::CFG_SERVER_IP);
}

public function getBasicAuth()
{
$auth = $this->getConf(self::CFG_BASIC_AUTH);
if ($auth) {
if (!strpos($auth, ':')) {
throw new \Exception("Invalid Basic Authentication format. Please use \"user:password\" format in LiteMage config - Developer Settings.");
}
}
return $auth;
}

protected function _initConf( $type = '' )
{
$this->_conf = [];
Expand Down Expand Up @@ -312,8 +324,10 @@ protected function _initConf( $type = '' )
if ($debugon) {
$this->_debug_trace = $lm['dev']['debug_trace'] ?? 0;
}

$this->_conf[self::CFG_FRONT_STORE_ID] = $lm['dev'][self::CFG_FRONT_STORE_ID] ?? 1; // default is store 1
$this->_conf[self::CFG_SERVER_IP] = $lm['dev'][self::CFG_SERVER_IP] ?? '';
$this->_conf[self::CFG_BASIC_AUTH] = $lm['dev'][self::CFG_BASIC_AUTH] ?? '';
$this->_conf[self::CFG_PUBLICTTL] = $this->scopeConfig->getValue(self::STOREXML_PUBLICTTL);

$this->load_conf_field_array(self::CFG_CONTEXTBYPASS, $lm['general']);
Expand Down
Loading

0 comments on commit 3043f93

Please sign in to comment.