Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated flysystem version support #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 55 additions & 40 deletions FilesystemCachePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,64 @@
use Cache\Adapter\Common\AbstractCachePool;
use Cache\Adapter\Common\Exception\InvalidArgumentException;
use Cache\Adapter\Common\PhpCacheItem;
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException;
use League\Flysystem\UnableToDeleteFile;

/**
* @author Tobias Nyholm <[email protected]>
*/
class FilesystemCachePool extends AbstractCachePool
{
/**
* @type FilesystemInterface
* @type Filesystem
*/
private $filesystem;
private Filesystem $filesystem;

/**
* The folder should not begin nor end with a slash. Example: path/to/cache.
*
* @type string
*/
private $folder;
private string $folder;

/**
* @param FilesystemInterface $filesystem
* @param string $folder
* @param Filesystem $filesystem
* @param string $folder
*
* @throws \League\Flysystem\FilesystemException
*/
public function __construct(FilesystemInterface $filesystem, $folder = 'cache')
public function __construct(Filesystem $filesystem, string $folder = 'cache')
{
$this->folder = $folder;

$this->filesystem = $filesystem;
$this->filesystem->createDir($this->folder);
$this->filesystem->createDirectory($this->folder, []);
}

/**
* @param string $folder
*/
public function setFolder($folder)
public function setFolder(string $folder)
{
$this->folder = $folder;
}

/**
* {@inheritdoc}
* @throws \League\Flysystem\FilesystemException
*/
protected function fetchObjectFromCache($key)
protected function fetchObjectFromCache($key): array
{
$empty = [false, null, [], null];
$file = $this->getFilePath($key);
$file = $this->getFilePath($key);

try {
$data = @unserialize($this->filesystem->read($file));
if ($data === false) {
return $empty;
}
} catch (FileNotFoundException $e) {
} catch (FilesystemException $e) {
return $empty;
}

Expand All @@ -88,27 +91,28 @@ protected function fetchObjectFromCache($key)

/**
* {@inheritdoc}
* @throws \League\Flysystem\FilesystemException
*/
protected function clearAllObjectsFromCache()
protected function clearAllObjectsFromCache(): bool
{
$this->filesystem->deleteDir($this->folder);
$this->filesystem->createDir($this->folder);
$this->filesystem->deleteDirectory($this->folder);
$this->filesystem->createDirectory($this->folder);

return true;
}

/**
* {@inheritdoc}
*/
protected function clearOneObjectFromCache($key)
protected function clearOneObjectFromCache($key): bool
{
return $this->forceClear($key);
}

/**
* {@inheritdoc}
*/
protected function storeItemInCache(PhpCacheItem $item, $ttl)
protected function storeItemInCache(PhpCacheItem $item, $ttl): bool
{
$data = serialize(
[
Expand All @@ -119,29 +123,25 @@ protected function storeItemInCache(PhpCacheItem $item, $ttl)
);

$file = $this->getFilePath($item->getKey());
if ($this->filesystem->has($file)) {
// Update file if it exists
return $this->filesystem->update($file, $data);
}

try {
return $this->filesystem->write($file, $data);
} catch (FileExistsException $e) {
// To handle issues when/if race conditions occurs, we try to update here.
return $this->filesystem->update($file, $data);
$this->filesystem->write($file, $data);

return true;
} catch (FilesystemException $e) {
return false;
}
}

/**
* @param string $key
*
* @return string
* @throws InvalidArgumentException
*
* @return string
*/
private function getFilePath($key)
private function getFilePath(string $key): string
{
if (!preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) {
if (! preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) {
throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid filenames must match [a-zA-Z0-9_\.! ].', $key));
}

Expand All @@ -150,11 +150,11 @@ private function getFilePath($key)

/**
* {@inheritdoc}
* @throws \League\Flysystem\FilesystemException
*/
protected function getList($name)
{
$file = $this->getFilePath($name);

if (!$this->filesystem->has($file)) {
$this->filesystem->write($file, serialize([]));
}
Expand All @@ -164,6 +164,7 @@ protected function getList($name)

/**
* {@inheritdoc}
* @throws \League\Flysystem\FilesystemException
*/
protected function removeList($name)
{
Expand All @@ -173,19 +174,26 @@ protected function removeList($name)

/**
* {@inheritdoc}
* @throws \League\Flysystem\FilesystemException
*/
protected function appendListItem($name, $key)
protected function appendListItem($name, $key): bool
{
$list = $this->getList($name);
$list = $this->getList($name);
$list[] = $key;

return $this->filesystem->update($this->getFilePath($name), serialize($list));
try {
$this->filesystem->write($this->getFilePath($name), serialize($list));
return true;
} catch (FilesystemException $e) {
return false;
}
}

/**
* {@inheritdoc}
* @throws \League\Flysystem\FilesystemException
*/
protected function removeListItem($name, $key)
protected function removeListItem($name, $key): bool
{
$list = $this->getList($name);
foreach ($list as $i => $item) {
Expand All @@ -194,20 +202,27 @@ protected function removeListItem($name, $key)
}
}

return $this->filesystem->update($this->getFilePath($name), serialize($list));
try {
$this->filesystem->write($this->getFilePath($name), serialize($list));
return true;
} catch (FilesystemException $e) {
return false;
}
}

/**
* @param $key
*
* @return bool
*/
private function forceClear($key)
private function forceClear($key): bool
{
try {
return $this->filesystem->delete($this->getFilePath($key));
} catch (FileNotFoundException $e) {
$this->filesystem->delete($this->getFilePath($key));

return true;
} catch (FilesystemException $e) {
return false;
}
}
}
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
"require": {
"php": ">=7.4",
"cache/adapter-common": "^1.0",
"league/flysystem": "^1.0",
"psr/cache": "^1.0 || ^2.0",
"psr/simple-cache": "^1.0"
"league/flysystem": "^3.0",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"cache/integration-tests": "^0.17",
"phpunit/phpunit": "^7.5.20 || ^9.5.10"
},
"provide": {
"psr/cache-implementation": "^1.0",
"psr/simple-cache-implementation": "^1.0"
"psr/cache-implementation": "^1.0 || ^2.0 || ^3.0",
"psr/simple-cache-implementation": "^1.0 || ^2.0 || ^3.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down