diff --git a/bin/viola b/bin/viola old mode 100755 new mode 100644 index 175339f..9c9e25a --- a/bin/viola +++ b/bin/viola @@ -8,9 +8,9 @@ if (file_exists(__DIR__.'/../../../autoload.php')) { } -$storage = new Farzai\Viola\Storage\CacheFilesystemStorage(); +$storage = new Farzai\Viola\Storage\TemporaryFilesystemStorage(); $databaseConfig = new Farzai\Viola\Storage\DatabaseConnectionRepository( - new Farzai\Viola\Storage\CacheFilesystemStorage() + new Farzai\Viola\Storage\TemporaryFilesystemStorage() ); $commands = [ @@ -27,4 +27,4 @@ foreach ($commands as $command) { $application->add($command); } -$application->run(); \ No newline at end of file +$application->run(); diff --git a/composer.json b/composer.json index 0d90017..f3cb26d 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,8 @@ "php": "^8.1", "doctrine/dbal": "^3.6", "farzai/transport": "^1.2.0", - "symfony/cache": "^5.4.21|^6.2.10", - "symfony/console": "^5.4.21|^6.2.10" + "symfony/console": "^5.4.21|^6.2.10", + "symfony/process": "^6.4|^7.1" }, "require-dev": { "laravel/pint": "^1.2", diff --git a/src/Storage/TemporaryFilesystemStorage.php b/src/Storage/TemporaryFilesystemStorage.php new file mode 100644 index 0000000..2b3aa8f --- /dev/null +++ b/src/Storage/TemporaryFilesystemStorage.php @@ -0,0 +1,52 @@ +prefix = md5($prefix); + } + + public function get(string $key, mixed $default = null): mixed + { + $filename = $this->getFilename($key); + + if (file_exists($filename)) { + return unserialize(file_get_contents($filename)); + } + + return $default; + } + + public function set(string $key, mixed $value): void + { + $filename = $this->getFilename($key); + + file_put_contents($filename, serialize($value)); + } + + public function has(string $key): bool + { + return file_exists($this->getFilename($key)); + } + + public function remove(string $key): void + { + $filename = $this->getFilename($key); + + if (file_exists($filename)) { + unlink($filename); + } + } + + private function getFilename(string $key): string + { + return sys_get_temp_dir().DIRECTORY_SEPARATOR.$this->prefix.'_'.md5($key); + } +} diff --git a/src/ViolaResponse.php b/src/ViolaResponse.php index 72c5272..1e9c519 100644 --- a/src/ViolaResponse.php +++ b/src/ViolaResponse.php @@ -12,9 +12,7 @@ final class ViolaResponse implements ViolaResponseInterface public function __construct( private array $body, private ?string $query, - private ?array $results) - { - } + private ?array $results) {} /** * Return the answer. diff --git a/tests/BinViolaTest.php b/tests/BinViolaTest.php new file mode 100644 index 0000000..813266a --- /dev/null +++ b/tests/BinViolaTest.php @@ -0,0 +1,24 @@ +storage = new Farzai\Viola\Storage\TemporaryFilesystemStorage(); + + $this->storage->remove('api_key'); +}); + +it('should use TemporaryFilesystemStorage by default', function () { + $this->storage->set('api_key', 'test'); + + $process = new Process(['php', 'bin/viola', 'config:show']); + $process->run(); + + if (! $process->isSuccessful()) { + throw new ProcessFailedException($process); + } + + $output = $process->getOutput(); + expect($output)->toContain('Database Connection'); +}); diff --git a/tests/Storage/CacheFilesystemStorageTest.php b/tests/Storage/TemporaryFilesystemStorageTest.php similarity index 50% rename from tests/Storage/CacheFilesystemStorageTest.php rename to tests/Storage/TemporaryFilesystemStorageTest.php index 80a9912..a1a9297 100644 --- a/tests/Storage/CacheFilesystemStorageTest.php +++ b/tests/Storage/TemporaryFilesystemStorageTest.php @@ -1,16 +1,16 @@ remove('foo'); }); it('should return the value of the given key', function () { - $storage = new CacheFilesystemStorage('test'); + $storage = new TemporaryFilesystemStorage('test'); $storage->set('foo', 'bar'); @@ -18,13 +18,13 @@ }); it('should return the default value if the key does not exist', function () { - $storage = new CacheFilesystemStorage('test'); + $storage = new TemporaryFilesystemStorage('test'); expect($storage->get('foo', 'bar'))->toBe('bar'); }); it('should return true if the key exists', function () { - $storage = new CacheFilesystemStorage('test'); + $storage = new TemporaryFilesystemStorage('test'); $storage->set('foo', 'bar'); @@ -32,7 +32,16 @@ }); it('should return false if the key does not exist', function () { - $storage = new CacheFilesystemStorage('test'); + $storage = new TemporaryFilesystemStorage('test'); + + expect($storage->has('foo'))->toBeFalse(); +}); + +it('should remove the given key', function () { + $storage = new TemporaryFilesystemStorage('test'); + + $storage->set('foo', 'bar'); + $storage->remove('foo'); expect($storage->has('foo'))->toBeFalse(); });