Skip to content

Instantly share code, notes, and snippets.

@primitive-type
Created January 21, 2014 22:16
Show Gist options
  • Save primitive-type/8549579 to your computer and use it in GitHub Desktop.
Save primitive-type/8549579 to your computer and use it in GitHub Desktop.
Implementation of a cache storage adapter implementing Zend\Authentication\Storage\StorageInterface for use with Zend\Authentication\AuthenticationService. This is modeled directly after Zend\Authentication\Storage\Session in order to mimic that class's behavior when used with the aforementioned AuthenticationService class.
<?php
namespace Zend\Authentication\Storage;
use Zend\Authentication\Storage\StorageInterface as AuthStorageInterface;
use Zend\Cache\Storage\StorageInterface as CacheStorageInterface;
class Cache implements AuthStorageInterface
{
/**
* Default cache namespace
*/
const NAMESPACE_DEFAULT = 'Zend_Auth';
/**
* Default cache key name for auth data
*/
const KEY_DEFAULT = 'storage';
/**
* Cache adapter to mimic PHP session storage
*
* @var CacheStorageInterface
*/
protected $cache;
/**
* Cache namespace
*
* @var mixed
*/
protected $namespace = self::NAMESPACE_DEFAULT;
/**
* Cache key name for auth data
*
* @var mixed
*/
protected $key = self::KEY_DEFAULT;
/**
* Sets cache storage options and initializes cache namespace object
*
* @param mixed $namespace
* @param mixed $key
* @param CacheStorageInterface $cache
*/
public function __construct(CacheStorageInterface $cache, $namespace = null, $key = null)
{
$this->cache = $cache;
if ($namespace !== null) {
$this->setNamespace($namespace);
}
if ($key !== null) {
$this->key = $key;
}
}
/**
* Defined by Zend\Authentication\Storage\StorageInterface
*
* @return bool
*/
public function isEmpty()
{
return !$this->cache->hasItem($this->key);
}
/**
* Defined by Zend\Authentication\Storage\StorageInterface
*
* @return mixed
*/
public function read()
{
return $this->cache->getItem($this->key);
}
/**
* Defined by Zend\Authentication\Storage\StorageInterface
*
* @param mixed $contents
*/
public function write($contents)
{
$this->cache->setItem($this->key, $contents);
}
/**
* Defined by Zend\Authentication\Storage\StorageInterface
*
*/
public function clear()
{
$this->cache->removeItem($this->key);
}
/**
* Returns the name of the cache key being set to and read from
*
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Returns the cache storage adapter
*
* @return CacheStorageInterface
*/
public function getCache()
{
return $this->cache;
}
/**
* Sets the cache namespace
*
* @param string $namespace
*/
public function setNamespace($namespace)
{
$this->cache->getOptions()->setNamespace($namespace);
}
/**
* Returns the cache namespace
*
* @return string
*/
public function getNamespace()
{
return $this->cache->getOptions()->getNamespace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment