<?php
namespace app\admin\controller\setting;
use app\admin\controller\Controller;
use think\Cache as CacheDriver;
class Cache extends Controller
{
public function clear($isForce = false)
{
if ($this->request->isAjax()) {
$this->rmCache($this->postData('cache'));
return $this->renderSuccess('操作成功');
}
return $this->fetch('clear', [
'isForce' => !!$isForce ?: config('app_debug'),
]);
}
private function rmCache($data)
{
if (in_array('data', $data['item'])) {
$isForce = isset($data['isForce']) ? !!$data['isForce'] : false;
CacheDriver::clear($isForce ? null : 'cache');
}
if (in_array('temp', $data['item'])) {
$paths = [
'temp' => WEB_PATH . 'temp/',
'runtime' => RUNTIME_PATH . 'image/'
];
foreach ($paths as $path) {
$this->deleteFolder($path);
}
}
}
private function deleteFolder($path)
{
if (!is_dir($path))
return false;
foreach (scandir($path) as $val) {
if (!in_array($val, ['.', '..', '.gitignore'])) {
if (is_dir($path . $val)) {
$this->deleteFolder($path . $val . '/');
rmdir($path . $val . '/');
} else {
unlink($path . $val);
}
}
}
return true;
}
}