1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
<?php
namespace Core\CLI\Cache;
use Core;
final class Clear
{
private static $CACHES;
public static function init(array $params = array())
{
self::$CACHES = array(
'system' => 'temp/cache',
);
$modes = Core\Config()->modes();
foreach ($modes as $mode) {
Core\Config()->setMode($mode);
$rootPath = Core\Config()->paths('root');
$assetsPath = Core\Config()->paths('assets');
$assets = Core\Utils::replaceFirstOccurrence($rootPath, '', $assetsPath['distribution']);
if (file_exists($assetsPath['distribution'] . 'js')) {
self::$CACHES['assets'][] = $assets . 'js';
}
if (file_exists($assetsPath['distribution'] . 'css')) {
self::$CACHES['assets'][] = $assets . 'css';
}
}
}
public static function run($cache = null)
{
$cwd = getcwd() . DIRECTORY_SEPARATOR;
if ($cache) {
if (isset(self::$CACHES[$cache])) {
if (is_array(self::$CACHES[$cache])) {
foreach (self::$CACHES[$cache] as $path) {
self::deleteFolder($cwd . $path);
}
} else {
self::deleteFolder($cwd . self::$CACHES[$cache]);
}
}
} else {
foreach (self::$CACHES as $type => $path) {
if (is_array($path)) {
foreach ($path as $p) {
self::deleteFolder($cwd . $p);
}
} else {
self::deleteFolder($cwd . $path);
}
}
}
}
private static function deleteFolder($path)
{
if (!file_exists($path)) {
return;
}
$i = new \DirectoryIterator($path);
foreach ($i as $f) {
if ($f->isFile()) {
unlink($f->getRealPath());
} elseif (!$f->isDot() && $f->isDir()) {
self::deleteFolder($f->getRealPath());
rmdir($f->getRealPath());
}
}
}
}