seeddms-code/utils/Commands/ListcacheCommand.php
2025-11-13 13:57:03 +01:00

102 lines
3.2 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 Seeddms\Seeddms\Settings;
use Seeddms\Seeddms\Translator;
use SeedDMS_Core_File;
use SeedDMS_View_Common;
use Log_file;
use FilesystemIterator;
class ListcacheCommand 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:list')
->setDescription('List caches')
->setHelp('Lists all caches including size.')
;
}
protected function execute(InputInterface $input, OutputInterface $output) : int
{
$app = $this->getApplication();
$settings = $this->settings;
$logger = $this->logger;
$translator = $this->translator;
$outformat = '%-20s %-40s %15s %7d';
require_once('inc/inc.DBInit.php');
$cachedir = $settings->_cacheDir;
$totalc = 0;
$totalspace = 0;
// Preview for png, pdf, and txt */
foreach(['png', 'pdf', 'txt'] as $t) {
$path = addDirSep($cachedir).$t;
if(file_exists($path)) {
$space = dskspace($path);
$fi = new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS);
$c = iterator_count($fi);
} else {
$space = $c = 0;
}
$totalc += $c;
$totalspace += $space;
$output->writeln(sprintf($outformat, 'preview'.$t, $translator->translate('preview_'.$t), SeedDMS_Core_File::format_filesize($space), $c));
}
/* Javascript */
$path = addDirSep($cachedir).'js';
if(file_exists($path)) {
$space = dskspace($path);
$fi = new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS);
$c = iterator_count($fi);
} else {
$space = $c = 0;
}
$totalc += $c;
$totalspace += $space;
$output->writeln(sprintf($outformat, 'js', $translator->translate('temp_jscode'), SeedDMS_Core_File::format_filesize($space), $c));
$caches = [];
/* Create a dummy view for passing it to additionalCache() */
$view = new SeedDMS_View_Common(['settings'=>$settings]);
if (isset($GLOBALS['SEEDDMS_HOOKS']['view']['clearCache'])) {
foreach ($GLOBALS['SEEDDMS_HOOKS']['view']['clearCache'] as $hookObj) {
if (method_exists($hookObj, 'additionalCache')) {
$caches += $hookObj->additionalCache($view);
}
}
}
foreach($caches as $cache) {
$output->writeln(sprintf($outformat, $cache[0], $cache[1], SeedDMS_Core_File::format_filesize($cache[2]), $cache[3]));
}
// print_r($caches);
return Command::SUCCESS;
}
}
// vim: ts=4 sw=4 expandtab