generated from farzai/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from farzai/parsilver/use-temporary-filesystem…
…-storage Use TemporaryFilesystemStorage by default
- Loading branch information
Showing
6 changed files
with
98 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
23 changes: 16 additions & 7 deletions
23
tests/Storage/CacheFilesystemStorageTest.php → ...torage/TemporaryFilesystemStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |