From bb30a447c5c067d6b2b59cc31452bf1ce403d319 Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 3 Mar 2012 08:28:48 +0545 Subject: [PATCH] [HttpFoundation] Prepare to split out session handler callback from session storage. --- .../Storage/Handler/NativeSessionHandler.php | 24 +++++ .../Session/Storage/Proxy/AbstractProxy.php | 80 ++++++++++++++++ .../Session/Storage/Proxy/NativeProxy.php | 45 +++++++++ .../Storage/Proxy/SessionHandlerProxy.php | 93 +++++++++++++++++++ 4 files changed, 242 insertions(+) create mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php create mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxy.php create mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/NativeProxy.php create mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php new file mode 100644 index 0000000000..a5338d2da2 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; + +/** + * Adds SessionHandler functionality if available. + * + * @see http://php.net/sessionhandler + */ + +if (version_compare(phpversion(), '5.4.0', '>=')) { + class NativeSessionHandler extends \SessionHandler {} +} else { + class NativeSessionHandler {} +} \ No newline at end of file diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxy.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxy.php new file mode 100644 index 0000000000..41cd49cb1e --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxy.php @@ -0,0 +1,80 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Session\Storage\Proxy; + +/** + * AbstractProxy. + */ +abstract class AbstractProxy +{ + /** + * Flag if handler wraps an internal PHP session handler (using \SessionHandler). + * + * @var boolean + */ + protected $wrapper = false; + + /** + * @var boolean + */ + protected $active = false; + + /** + * @var string + */ + protected $saveHandlerName; + + /** + * Gets the session.save_handler name. + * + * @return string + */ + public function getSaveHandlerName() + { + return $this->saveHandlerName; + } + + public function isSessionHandlerInterface() + { + return (bool)($this instanceof \SessionHandlerInterface); + } + + /** + * Returns true if this handler wraps an internal PHP session save handler using \SessionHandler. + * + * @return bool + */ + public function isWrapper() + { + return $this->wrapper; + } + + /** + * Has a session started? + * + * @return bool + */ + public function isActive() + { + return $this->active; + } + + /** + * Sets the active flag. + * + * @param bool $flag + */ + public function setActive($flag) + { + $this->active = (bool)$flag; + } +} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/NativeProxy.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/NativeProxy.php new file mode 100644 index 0000000000..952c7105cd --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/NativeProxy.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Session\Storage\Proxy; + +/** + * NativeProxy. + * + * This proxy is built-in session handlers in PHP 5.3.x + */ +class NativeProxy extends AbstractProxy +{ + /** + * Constructor. + * + * @param $handler + */ + public function __construct($handler) + { + if (version_compare(phpversion(), '5.4.0', '>=') && $handler instanceof \SessionHandlerInterface) { + throw new \InvalidArgumentException('This proxy is only for PHP 5.3 and not for instances of \SessionHandler or \SessionHandlerInterface'); + } + + $this->handler = $handler; + $this->saveHandlerName = ini_get('session.save_handler'); + } + + /** + * Returns true if this handler wraps an internal PHP session save handler using \SessionHandler. + * + * @return bool False. + */ + public function isWrapper() + { + return false; + } +} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php new file mode 100644 index 0000000000..93a1010a73 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php @@ -0,0 +1,93 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Session\Storage\Proxy; + +/** + * SessionHandler proxy. + */ +class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface +{ + /** + * @var \SessionHandlerInterface + */ + protected $handler; + + /** + * Constructor. + * + * @param \SessionHandlerInterface $handler + */ + public function __construct(\SessionHandlerInterface $handler) + { + $this->handler = $handler; + $this->wrapper = (bool)(class_exists('SessionHandler') && $handler instanceof \SessionHandler); + $this->saveHandlerName = $this->wrapper ? ini_get('session.save_handler') : 'user'; + } + + // \SessionHandlerInterface + + /** + * {@inheritdoc} + */ + function open($savePath, $sessionName) + { + $return = (bool)$this->handler->open($savePath, $sessionName); + + if (true === $return) { + $this->active = true; + } + + return $return; + } + + /** + * {@inheritdoc} + */ + function close() + { + $this->active = false; + + return (bool)$this->handler->close(); + } + + /** + * {@inheritdoc} + */ + function read($id) + { + return (string)$this->handler->read($id); + } + + /** + * {@inheritdoc} + */ + function write($id, $data) + { + return (bool)$this->handler->write($id, $data); + } + + /** + * {@inheritdoc} + */ + function destroy($id) + { + return (bool)$this->handler->destroy($id); + } + + /** + * {@inheritdoc} + */ + function gc($maxlifetime) + { + return (bool)$this->handler->gc($maxlifetime); + } +}