From 659155dd5a603c55aa83937e07703534eeb06a81 Mon Sep 17 00:00:00 2001 From: parsilver Date: Sun, 7 Jul 2024 11:39:38 +0700 Subject: [PATCH 1/2] Use TemporaryFilesystemStorage by default --- bin/viola | 6 +-- composer.json | 4 +- src/Storage/TemporaryFilesystemStorage.php | 52 +++++++++++++++++++ src/ViolaResponse.php | 4 +- tests/BinViolaTest.php | 24 +++++++++ ...php => TemporaryFilesystemStorageTest.php} | 23 +++++--- 6 files changed, 98 insertions(+), 15 deletions(-) mode change 100755 => 100644 bin/viola create mode 100644 src/Storage/TemporaryFilesystemStorage.php create mode 100644 tests/BinViolaTest.php rename tests/Storage/{CacheFilesystemStorageTest.php => TemporaryFilesystemStorageTest.php} (50%) 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..a3de028 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": "^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(); }); From 617e89cbb8f5a3b6b45af78b55edcf2922156d3b Mon Sep 17 00:00:00 2001 From: parsilver Date: Sun, 7 Jul 2024 13:28:56 +0700 Subject: [PATCH 2/2] Update composer.json for symfony/process to support version ^6.4 or ^7.1 Update `composer.json` to support `symfony/process` version ^6.4 or ^7.1 * Update the `symfony/process` version in the `require` section to `^6.4|^7.1` --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/farzai/viola-php/pull/18?shareId=caab059c-3832-4b3d-a0b9-67a5f7867ac8). --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a3de028..f3cb26d 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "doctrine/dbal": "^3.6", "farzai/transport": "^1.2.0", "symfony/console": "^5.4.21|^6.2.10", - "symfony/process": "^7.1" + "symfony/process": "^6.4|^7.1" }, "require-dev": { "laravel/pint": "^1.2",