Files
2026-09-01 10:46:29 -03:00

73 lines
2.1 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CacheClearCommand extends Command
{
protected static $defaultName = 'cache:clear';
protected static $defaultDescription = 'Clears the application cache';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Clearing cache...');
$cacheDirs = [
dirname(__DIR__, 3) . '/cache/twig',
// Add other cache directories here
];
$hasError = false;
foreach ($cacheDirs as $dir) {
if (is_dir($dir)) {
$output->write("Limpando <info>{$dir}</info>... ");
if ($this->deleteDirectory($dir)) {
$output->writeln('<info>OK</info>');
// Recreate empty directory
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
chmod($dir, 0777);
}
} else {
$output->writeln('<error>FALHA</error>');
$hasError = true;
}
}
}
if ($hasError) {
$output->writeln('<error>Alguns caches não puderam ser limpos.</error>');
return Command::FAILURE;
}
$output->writeln('<info>Cache limpo com sucesso!</info>');
return Command::SUCCESS;
}
private function deleteDirectory($dir) {
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!$this->deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}
}