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

Add Flysystem 2 Storage #138

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ $stack->push(
```

## Flysystem


```php
[...]
use League\Flysystem\Adapter\Local;
Expand All @@ -133,6 +135,28 @@ $stack->push(
);
```

for Flysystem 2
Copy link

@dpi dpi Jun 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either this should be its own ## heading or the Flysystem 1 version should also get this 'for xxx' heading.

The newer version should be before the older version

If keeping 'for' headings, should be capitalized.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added that.


```php
[...]
use League\Flysystem\Local\LocalFilesystemAdapter;
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
use Kevinrob\GuzzleCache\Storage\Flysystem2Storage;

[...]

$stack->push(
new CacheMiddleware(
new PrivateCacheStrategy(
new Flysystem2Storage(
new LocalFilesystemAdapter('/path/to/cache')
)
)
),
'cache'
);
```

## WordPress Object Cache
```php
[...]
Expand Down
61 changes: 61 additions & 0 deletions src/Storage/Flysystem2Storage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Kevinrob\GuzzleCache\Storage;

use Kevinrob\GuzzleCache\CacheEntry;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\Filesystem;
use League\Flysystem\FileNotFoundException;

class Flysystem2Storage implements CacheStorageInterface
{

/**
* @var Filesystem
*/
protected $filesystem;

public function __construct(LocalFilesystemAdapter $adapter)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @diego-sorribas this shouldnt be fixed to local

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I 'll try to understand what that means and will fix it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think I got it. :)
Thanks for the hints!

{
$this->filesystem = new Filesystem($adapter);
}

/**
* @inheritdoc
*/
public function fetch($key)
{
if ($this->filesystem->fileExists($key)) {
// The file exist, read it!
$data = @unserialize(
$this->filesystem->read($key)
);

if ($data instanceof CacheEntry) {
return $data;
}
}

return;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to return NULL not void, per \Kevinrob\GuzzleCache\Storage\CacheStorageInterface::fetch

}

/**
* @inheritdoc
*/
public function save($key, CacheEntry $data)
{
return $this->filesystem->write($key, serialize($data));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\Kevinrob\GuzzleCache\Storage\CacheStorageInterface::save requires a bool return value, but \League\Flysystem\Filesystem::write returns void, and throws exceptions. So this call should be wrapped in try catch and return true/false.

}

/**
* {@inheritdoc}
*/
public function delete($key)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost the same as above:

\Kevinrob\GuzzleCache\Storage\CacheStorageInterface::delete requires a bool return value, but \League\Flysystem\Filesystem::delete returns void, and throws exceptions. So you should add a return true after the ->delete call, and change the return the catch to return false

{
try {
return $this->filesystem->delete($key);
} catch (FileNotFoundException $ex) {
return true;
}
}
}