Skip to content

Commit

Permalink
Merge pull request #18 from farzai/parsilver/use-temporary-filesystem…
Browse files Browse the repository at this point in the history
…-storage

Use TemporaryFilesystemStorage by default
  • Loading branch information
parsilver authored Jul 7, 2024
2 parents d72ed9b + 617e89c commit dd8bab7
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 15 deletions.
6 changes: 3 additions & 3 deletions bin/viola
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -27,4 +27,4 @@ foreach ($commands as $command) {
$application->add($command);
}

$application->run();
$application->run();
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
52 changes: 52 additions & 0 deletions src/Storage/TemporaryFilesystemStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Farzai\Viola\Storage;

use Farzai\Viola\Contracts\StorageRepositoryInterface;

class TemporaryFilesystemStorage implements StorageRepositoryInterface
{
private string $prefix;

public function __construct(string $prefix = 'viola_storage')
{
$this->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);
}
}
4 changes: 1 addition & 3 deletions src/ViolaResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions tests/BinViolaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

beforeEach(function () {
$this->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');
});
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
<?php

use Farzai\Viola\Storage\CacheFilesystemStorage;
use Farzai\Viola\Storage\TemporaryFilesystemStorage;

beforeEach(function () {
// Clear the cache before each test.
$storage = new CacheFilesystemStorage('test');
// Clear the temporary storage before each test.
$storage = new TemporaryFilesystemStorage('test');

$storage->remove('foo');
});

it('should return the value of the given key', function () {
$storage = new CacheFilesystemStorage('test');
$storage = new TemporaryFilesystemStorage('test');

$storage->set('foo', 'bar');

expect($storage->get('foo'))->toBe('bar');
});

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');

expect($storage->has('foo'))->toBeTrue();
});

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

0 comments on commit dd8bab7

Please sign in to comment.