seeddms-code/utils/Commands/ClearcacheCommand.php
2025-11-13 18:56:08 +01:00

80 lines
2.8 KiB
PHP

<?php
namespace Seeddms\Console\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Seeddms\Seeddms\Settings;
use Seeddms\Seeddms\Translator;
use SeedDMS_View_Common;
use SeedDMS_Controller_Common;
use Controller;
use Log_file;
class ClearcacheCommand extends Command
{
protected $settings;
protected $logger;
protected $translator;
public function __construct(Settings $settings, Log_file $logger, Translator $translator)
{
$this->settings = $settings;
$this->logger = $logger;
$this->translator = $translator;
parent::__construct();
}
protected function configure()
{
$this->setName('cache:clear')
->setDescription('Clear cache')
->setHelp('Clears either all caches or those specified with option --cache. Think twice before you clear a cache with previews. Depending on the number of documents in your DMS, it may take a long time to recreate the cache.')
->addOption('cache', '', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Remove files from this cache.', null)
->addOption('force', '', InputOption::VALUE_NONE, 'Force operation, do not ask')
;
}
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$app = $this->getApplication();
$settings = $this->settings;
$logger = $this->logger;
$translator = $this->translator;
$output->writeln("<comment>Using configuration from '".$settings->_configFilePath."'.</comment>");
if (!is_writable($settings->_cacheDir)) {
$output->writeln(sprintf("<error>The cache dir '%s' is not writable for the system user running this script.</error>", $settings->_cacheDir));
return Command::FAILURE;
}
if (!$input->getOption('force')) {
$helper = new QuestionHelper();
$question = new ConfirmationQuestion('Do you really want to clear the cache? ', false);
if (!$helper->ask($input, $output, $question)) {
return Command::SUCCESS;
}
}
require_once('inc/inc.DBInit.php');
$post = array_flip($input->getOption('cache'));
array_walk($post, function (&$v, $k) {$v=1;});
$controller = Controller::factory('ClearCache', array('settings'=>$settings));
$controller->setParam('post', $post);
if(!$ret = $controller->run()) {
}
return Command::SUCCESS;
}
}
// vim: ts=4 sw=4 expandtab