Fix cache, add clear-cache command

This commit is contained in:
Stéphane Goetz 2018-06-07 20:40:38 +02:00
parent 6a7dae3023
commit ba8658d706
4 changed files with 56 additions and 8 deletions

View File

@ -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;
}
}
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);
}
}
}

View File

@ -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";

View File

@ -0,0 +1,23 @@
<?php namespace Todaymade\Daux\Console;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Cache;
class ClearCache extends SymfonyCommand
{
protected function configure()
{
$this
->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("<info>Cache cleared</info>");
}
}

View File

@ -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() {