Skip to content

Commit

Permalink
Dont require a fork of kevinrob/guzzle-cache-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dpi authored Jul 23, 2021
1 parent 1550805 commit 959d16f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
8 changes: 1 addition & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"composer/semver": "^3.2",
"czproject/git-php": "^4.0",
"guzzlehttp/guzzle": "^7.3",
"kevinrob/guzzle-cache-middleware": "dev-feature-flysystem2",
"kevinrob/guzzle-cache-middleware": "^3.4",
"league/flysystem": "^2.1",
"m4tthumphrey/php-gitlab-api": "^11.4",
"nyholm/psr7": "^1.4",
Expand Down Expand Up @@ -38,12 +38,6 @@
"phpstan/phpstan-symfony": "^0.12.41",
"phpunit/phpunit": "^9.5"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/dpi/guzzle-cache-middleware.git"
}
],
"config": {
"sort-packages": true
},
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Traits/HttpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace dogit\Commands\Traits;

use dogit\Flysystem2Storage;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\SetCookie;
Expand All @@ -13,7 +14,6 @@
use Http\Adapter\Guzzle7\Client;
use Http\Discovery\Psr17FactoryDiscovery;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Storage\Flysystem2Storage;
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Psr\Log\LoggerInterface;
Expand Down
70 changes: 70 additions & 0 deletions src/Flysystem2Storage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace dogit;

use Kevinrob\GuzzleCache\CacheEntry;
use Kevinrob\GuzzleCache\Storage\CacheStorageInterface;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\UnableToDeleteFile;

/**
* Flysystem2 cache.
*
* Similar to \Kevinrob\GuzzleCache\Storage\Flysystem2Storage from
* https://github.com/Kevinrob/guzzle-cache-middleware/pull/138/files.
*/
final class Flysystem2Storage implements CacheStorageInterface
{
private Filesystem $filesystem;

public function __construct(FilesystemAdapter $adapter)
{
$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 null;
}

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

return true;
}

/**
* {@inheritdoc}
*/
public function delete($key)
{
try {
$this->filesystem->delete($key);

return true;
} catch (UnableToDeleteFile $ex) {
return false;
}
}
}

0 comments on commit 959d16f

Please sign in to comment.