Improve code, fix filesystem test failing after first run
This commit is contained in:
@@ -49,21 +49,21 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param SerializerInterface $serializer
|
||||
* @param int|null $chmod
|
||||
* @param int|null $ttl
|
||||
* @param string|null $directorySeperator
|
||||
* @param string|null $directorySeparator
|
||||
*/
|
||||
public function __construct(
|
||||
string $directory,
|
||||
SerializerInterface $serializer,
|
||||
int $chmod = null,
|
||||
int $ttl = null,
|
||||
string $directorySeperator = null
|
||||
string $directorySeparator = null
|
||||
)
|
||||
{
|
||||
$this->indexLocation = $directory . '/' . md5($directory) . 'index';
|
||||
$this->ttlLocation = $directory . '/' . md5($directory) . 'ttl';
|
||||
$this->directory = $directory;
|
||||
$this->serializer = $serializer;
|
||||
$this->directorySeparator = $directorySeperator;
|
||||
$this->directorySeparator = $directorySeparator;
|
||||
$this->chmod = $chmod;
|
||||
$this->ttl = $ttl;
|
||||
|
||||
@@ -106,7 +106,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param int|null $ttl
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $key, $value, int $ttl = null)
|
||||
public function set(string $key, $value, int $ttl = null): void
|
||||
{
|
||||
$file = $this->getFilename($key);
|
||||
file_put_contents($file, $this->serializer->serialize($value));
|
||||
@@ -136,8 +136,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
if ($this->exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
unset($this->index[$key]);
|
||||
unset($this->timeToLive[$key]);
|
||||
unset($this->index[$key], $this->timeToLive[$key]);
|
||||
$this->saveIndex();
|
||||
}
|
||||
}
|
||||
@@ -175,7 +174,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
/**
|
||||
* @param $pattern
|
||||
*/
|
||||
private function deleteGlob($pattern)
|
||||
private function deleteGlob($pattern): void
|
||||
{
|
||||
foreach ($this->index as $key => $value) {
|
||||
if (fnmatch($pattern, $key)) {
|
||||
@@ -188,7 +187,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param $file
|
||||
* @return bool
|
||||
*/
|
||||
private function exists(string $file)
|
||||
private function exists(string $file): bool
|
||||
{
|
||||
return file_exists($file);
|
||||
}
|
||||
@@ -197,9 +196,9 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param $key
|
||||
* @return string
|
||||
*/
|
||||
private function getFilename(string $key)
|
||||
private function getFilename(string $key): string
|
||||
{
|
||||
list($directory, $file) = $this->getDirectoryAndFile($key);
|
||||
[$directory, $file] = $this->getDirectoryAndFile($key);
|
||||
return $directory . '/' . md5($file);
|
||||
}
|
||||
|
||||
@@ -207,7 +206,7 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
private function getDirectoryAndFile(string $key)
|
||||
private function getDirectoryAndFile(string $key): array
|
||||
{
|
||||
$directory = $this->directory;
|
||||
if ($this->directorySeparator !== null) {
|
||||
@@ -224,17 +223,19 @@ class FilesystemAdapter implements CacheAdapterInterface
|
||||
/**
|
||||
* @param $directory
|
||||
*/
|
||||
private function createSubDirectoryIfNotExists($directory)
|
||||
private function createSubDirectoryIfNotExists($directory): void
|
||||
{
|
||||
if (file_exists($directory) === false) {
|
||||
mkdir($directory);
|
||||
if (!mkdir($directory) && !is_dir($directory)) {
|
||||
throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory));
|
||||
}
|
||||
if ($this->chmod !== null) {
|
||||
chmod($directory, $this->chmod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function saveIndex()
|
||||
private function saveIndex(): void
|
||||
{
|
||||
file_put_contents($this->indexLocation, json_encode($this->index));
|
||||
file_put_contents($this->ttlLocation, json_encode($this->timeToLive));
|
||||
|
||||
Reference in New Issue
Block a user