Files
datahub/backend/app/Command/AppCacheClear.php
T

72 lines
1.8 KiB
PHP
Raw Normal View History

2026-02-06 09:29:04 +08:00
<?php
declare(strict_types=1);
namespace App\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
#[Command]
class AppCacheClear extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('app:cache-clear');
}
public function configure()
{
parent::configure();
$this->setDescription('Clear application caches (proxy, runtime)');
}
public function handle(): void
{
$basePath = BASE_PATH . '/runtime';
$directories = [
$basePath . '/container/proxy',
$basePath . '/container/aspects',
$basePath . '/cache',
];
$clearedCount = 0;
foreach ($directories as $dir) {
if (is_dir($dir)) {
$files = glob($dir . '/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
$clearedCount++;
} elseif (is_dir($file)) {
$this->removeDirectory($file);
$clearedCount++;
}
}
$this->info("Cleared: {$dir}");
} else {
$this->line("Skipped (not exists): {$dir}");
}
}
$this->info("Cache cleared. Removed {$clearedCount} items.");
}
private function removeDirectory(string $dir): void
{
if (!is_dir($dir)) {
return;
}
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
$path = $dir . '/' . $file;
is_dir($path) ? $this->removeDirectory($path) : unlink($path);
}
rmdir($dir);
}
}