Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"doctrine/cache": "~1.0"
},
"suggest": {
"psr/cache-implementation": "To make use of PSR-6 cache implementation via PsrCacheAdapter."
"psr/cache-implementation": "To make use of PSR-6 cache implementation via PsrCacheAdapter.",
"psr/simple-cache-implementation": "To make use of PSR-16 cache implementation via PsrSimpleCacheAdapter."
},
"autoload": {
"psr-4": {
Expand Down
110 changes: 110 additions & 0 deletions lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php
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;
Copy link

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


/**
* @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;
}
}
}