add app cache clear
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user