Skip to content

Instantly share code, notes, and snippets.

@spekkionu
Created October 9, 2014 21:41
Show Gist options
  • Save spekkionu/6ef2a2461601aa483685 to your computer and use it in GitHub Desktop.
Save spekkionu/6ef2a2461601aa483685 to your computer and use it in GitHub Desktop.
Symfony HttpFoundation Zend\Authentication\Storage adapter
<?php
namespace Spekkionu\Zend\Authentication\Storage;
use Zend\Authentication\Storage\StorageInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class SymfonySession implements StorageInterface
{
/**
* Default session namespace
*/
const NAMESPACE_DEFAULT = 'Zend_Auth';
/**
* Session namespace
*
* @var mixed
*/
protected $namespace = self::NAMESPACE_DEFAULT;
/**
* @var Session $session
*/
protected $session;
/**
* Sets session storage options and initializes session namespace object
*
* @param SessionManager $manager
* @param mixed $namespace
*/
public function __construct(Session $session, $namespace = null)
{
if ($namespace !== null) {
$this->namespace = $namespace;
}
$this->session = $session;
}
/**
* Returns the session namespace
*
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}
/**
* Returns true if and only if storage is empty
*
* @throws \Zend\Authentication\Exception\ExceptionInterface If it is impossible to determine whether storage is empty
* @return bool
*/
public function isEmpty()
{
return $this->session->has($this->namespace);
}
/**
* Returns the contents of storage
*
* Behavior is undefined when storage is empty.
*
* @throws \Zend\Authentication\Exception\ExceptionInterface If reading contents from storage is impossible
* @return mixed
*/
public function read()
{
return $this->session->get($this->namespace);
}
/**
* Writes $contents to storage
*
* @param mixed $contents
* @throws \Zend\Authentication\Exception\ExceptionInterface If writing $contents to storage is impossible
* @return void
*/
public function write($contents){
$session->set($this->namespace, $contents);
}
/**
* Clears contents from storage
*
* @throws \Zend\Authentication\Exception\ExceptionInterface If clearing contents from storage is impossible
* @return void
*/
public function clear()
{
$this->session->remove($this->namespace);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment