This repository was archived by the owner on Oct 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Add PSR-16 compatible adapter #49
Open
heino
wants to merge
4
commits into
asm89:master
Choose a base branch
from
heino:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
110 changes: 110 additions & 0 deletions
110
lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php
This file contains hidden or 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,110 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of twig-cache-extension. | ||
* | ||
* (c) Alexander <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Asm89\Twig\CacheExtension\CacheProvider; | ||
|
||
/** | ||
* Adapter class to make the Asm89 Twig caching extension interoperable with every PSR-16 adapter. | ||
* | ||
* @see http://www.php-fig.org/psr/psr-16/ | ||
* | ||
* @author Heino H. Gehlsen <[email protected]> | ||
* @copyright 2017 Heino H. Gehlsen | ||
* @license MIT | ||
*/ | ||
class PsrSimpleCacheAdapter | ||
implements \Asm89\Twig\CacheExtension\CacheProviderInterface | ||
{ | ||
/** | ||
* @var \Psr\SimpleCache\CacheInterface | ||
*/ | ||
private $cache; | ||
|
||
/** | ||
* @var \Psr\SimpleCache\CacheException | ||
*/ | ||
private $lastException; | ||
|
||
/** | ||
* @param \Psr\SimpleCache\CacheInterface $cache | ||
*/ | ||
public function __construct(\Psr\SimpleCache\CacheInterface $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @return mixed|false False, if there was no value to be fetched. Null or a string otherwise. | ||
*/ | ||
public function fetch($key) | ||
{ | ||
// Reset last exception | ||
$this->lastException = null; | ||
|
||
try { | ||
// Get value from implementing library. Return false if there was no value to be fetched | ||
$value = $this->cache->get($key, false); | ||
|
||
// Return cached value | ||
return $value; | ||
|
||
// "Exception interface for invalid cache arguments." | ||
} catch (\Psr\SimpleCache\InvalidArgumentException $e) { | ||
// Save exception for retrieval by caller | ||
$this->lastException = $e; | ||
// Return fail to caller | ||
return false; | ||
|
||
// "Interface used for all types of exceptions thrown by the implementing library." | ||
} catch (\Psr\SimpleCache\CacheException $e) { | ||
// Save exception for retrieval by caller | ||
$this->lastException = $e; | ||
// Return fail to caller | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param string $value | ||
* @param integer $lifetime | ||
* | ||
* @return bool | ||
*/ | ||
public function save($key, $value, $lifetime = 0) | ||
{ | ||
// Reset last exception | ||
$this->lastException = null; | ||
|
||
try { | ||
// Send value to implementing library. | ||
$success = $this->cache->set($key, $value, $lifetime); | ||
|
||
// Return boolean from implementing library. | ||
return $success; | ||
|
||
// "Exception interface for invalid cache arguments." | ||
} catch (\Psr\SimpleCache\InvalidArgumentException $e) { | ||
// Save exception for retrieval by caller | ||
$this->lastException = $e; | ||
// Return fail to caller | ||
return false; | ||
|
||
// "Interface used for all types of exceptions thrown by the implementing library." | ||
} catch (\Psr\SimpleCache\CacheException $e) { | ||
// Save exception for retrieval by caller | ||
$this->lastException = $e; | ||
// Return fail to caller | ||
return false; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this private property is never accessible, so there is no reason to have it IMO