From ba8658d7068e25663db6b197b637d1af97e7ad7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Thu, 7 Jun 2018 20:40:38 +0200 Subject: [PATCH] Fix cache, add clear-cache command --- libs/Cache.php | 34 +++++++++++++++++++++++++++++----- libs/Console/Application.php | 1 + libs/Console/ClearCache.php | 23 +++++++++++++++++++++++ libs/Daux.php | 6 +++--- 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 libs/Console/ClearCache.php diff --git a/libs/Cache.php b/libs/Cache.php index 954ba95..e1f88bc 100644 --- a/libs/Cache.php +++ b/libs/Cache.php @@ -3,11 +3,12 @@ use Symfony\Component\Console\Output\OutputInterface; use Todaymade\Daux\Daux; -class Cache { +class Cache +{ static $printed = false; - protected static function getDirectory() + public static function getDirectory() { $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "dauxio" . DIRECTORY_SEPARATOR; @@ -29,7 +30,7 @@ class Cache { public static function put($key, $value) { Cache::ensureCacheDirectoryExists($path = Cache::path($key)); - file_put_contents($path, serialize($value)); + file_put_contents($path, $value); } /** @@ -90,6 +91,29 @@ class Cache { protected static function path($key) { $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2); - return Cache::getDirectory().'/'.implode('/', $parts).'/'.$hash; + return Cache::getDirectory() . '/' . implode('/', $parts) . '/' . $hash; } -} \ No newline at end of file + + public static function clear() + { + Cache::rrmdir(Cache::getDirectory()); + } + + protected static function rrmdir($dir) + { + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (is_dir($dir . "/" . $object)) { + Cache::rrmdir($dir . "/" . $object); + } else { + unlink($dir . "/" . $object); + } + + } + } + rmdir($dir); + } + } +} diff --git a/libs/Console/Application.php b/libs/Console/Application.php index a40f135..ccfec22 100644 --- a/libs/Console/Application.php +++ b/libs/Console/Application.php @@ -10,6 +10,7 @@ class Application extends SymfonyApplication $this->add(new Generate()); $this->add(new Serve()); + $this->add(new ClearCache()); $app_name = "daux/daux.io"; diff --git a/libs/Console/ClearCache.php b/libs/Console/ClearCache.php new file mode 100644 index 0000000..b8a2087 --- /dev/null +++ b/libs/Console/ClearCache.php @@ -0,0 +1,23 @@ +setName('clear-cache') + ->setDescription('Clears the cache'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $output->writeln("Clearing cache at '" . Cache::getDirectory() ."'"); + Cache::clear(); + $output->writeln("Cache cleared"); + } +} diff --git a/libs/Daux.php b/libs/Daux.php index 639f8cc..f4637cf 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -354,7 +354,7 @@ class Daux public static function getOutput() { if (!Daux::$output) { - Daux:$output = new NullOutput(); + Daux::$output = new NullOutput(); } return Daux::$output; @@ -368,7 +368,7 @@ class Daux * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL */ public static function write($messages, $newline = false, $options = 0) { - Daux::$output->write($messages, $newline, $options); + Daux::getOutput()->write($messages, $newline, $options); } /** @@ -378,7 +378,7 @@ class Daux * @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL */ public static function writeln($messages, $options = 0) { - Daux::write($messages, true, $options); + Daux::getOutput()->write($messages, true, $options); } public static function getVerbosity() {