Skip to content

Commit

Permalink
Merge branch 'topic/zend-session-type-hinting' of https://github.com/…
Browse files Browse the repository at this point in the history
…mwillbanks/zf2 into hotfix/session-typehinting
  • Loading branch information
weierophinney committed Mar 1, 2012
2 parents 4ae234a + b73eb96 commit 1bba550
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 305 deletions.
111 changes: 19 additions & 92 deletions library/Zend/Session/AbstractManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,82 +69,35 @@ abstract class AbstractManager implements Manager
* Allow passing a configuration object or class name, a storage object or
* class name, or an array of configuration.
*
* @param null|string|Configuration|array $config
* @param null|string|Storage $storage
* @param null|string|SaveHandler $saveHandler
* @param Configuration $config
* @param Storage $storage
* @param SaveHandler $saveHandler
* @return void
*/
public function __construct($config = null, $storage = null, $saveHandler = null)
public function __construct(Configuration $config = null, Storage $storage = null, SaveHandler $saveHandler = null)
{
if ($config instanceof \Zend\Config\Config) {
$config = $config->toArray();
}
if (is_array($config)) {
foreach ($config as $key => $value) {
switch (strtolower($key)) {
case 'storage':
if (null === $storage) {
$storage = $value;
}
unset($config[$key]);
break;
case 'savehandler':
if (null === $saveHandler) {
$saveHandler = $value;
}
unset($config[$key]);
break;
}
}
} elseif (is_string($config)) {
if (!class_exists($config)) {
throw new Exception\InvalidArgumentException('Configuration class provided is invalid; not found');
}
$config = new $config;
}

$this->setConfig($config);
$this->setStorage($storage);
$this->setSaveHandler($saveHandler);
if ($saveHandler) {
$this->setSaveHandler($saveHandler);
}
}

/**
* Set configuration object
*
* Allows lazy-loading a class name, passing an array of configuration to
* the defined default configuration class, or passing in a Configuration
* object. If a null value is passed, an instance of the default
* configuration class is created.
*
* @param null|string|array|Configuration $config
* @param null|Configuration $config
* @return void
*/
public function setConfig($config)
public function setConfig(Configuration $config = null)
{
if (null === $config) {
$config = new $this->configDefaultClass();
}

if (is_array($config)) {
$class = $this->configDefaultClass;
if (array_key_exists('class', $config)) {
$class = $config['class'];
unset($config['class']);
}

if (!class_exists($class)) {
throw new Exception\InvalidArgumentException('Class provided for configuration is invalid; not found');
if (!$config instanceof Configuration) {
throw new Exception\InvalidArgumentException('Default configuration type provided is invalid; must implement Zend\\Session\\Configuration');
}

$options = $config;
$config = new $class();
$config->setOptions($options);
unset($options);
}

if (!$config instanceof Configuration) {
throw new Exception\InvalidArgumentException('Configuration type provided is invalid; must implement Zend\\Session\\Configuration');
}

$this->config = $config;
}

Expand All @@ -161,27 +114,16 @@ public function getConfig()
/**
* Set session storage object
*
* Allows passing a null value, string class name, or Storage object. If a
* null value is passed, the default storage class will be used.
*
* @param null|string|Storage $storage
* @param null|Storage $storage
* @return void
*/
public function setStorage($storage)
public function setStorage(Storage $storage = null)
{
if (null === $storage) {
$storage = new $this->storageDefaultClass();
}

if (is_string($storage)) {
if (!class_exists($storage)) {
throw new Exception\InvalidArgumentException('Class provided for Storage does not exist');
if (!$storage instanceof Storage) {
throw new Exception\InvalidArgumentException('Default storage type provided is invalid; must implement Zend\\Session\\Storage');
}
$storage = new $storage();
}

if (!$storage instanceof Storage) {
throw new Exception\InvalidArgumentException('Storage type provided is invalid; must implement Zend\\Session\\Storage');
}

$this->storage = $storage;
Expand All @@ -200,29 +142,14 @@ public function getStorage()
/**
* Set session save handler object
*
* Allows passing a null value, string class name, or SaveHandler object. If a
* null value is passed, no explicit save handler will be used.
*
* @param null|string|SaveHandler $saveHandler
* @param SaveHandler $saveHandler
* @return void
*/
public function setSaveHandler($saveHandler)
public function setSaveHandler(SaveHandler $saveHandler)
{
if (null === $saveHandler) {
if ($saveHandler === null) {
return ;
}

if (is_string($saveHandler)) {
if (!class_exists($saveHandler)) {
throw new Exception\InvalidArgumentException('Class provided for SaveHandler does not exist');
}
$saveHandler = new $saveHandler();
}

if (!$saveHandler instanceof SaveHandler) {
throw new Exception\InvalidArgumentException('SaveHandler type provided is invalid; must implement Zend\\Session\\SaveHandler');
}

$this->saveHandler = $saveHandler;
}

Expand Down
16 changes: 8 additions & 8 deletions library/Zend/Session/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class Container extends ArrayObject
* Provide a name ('Default' if none provided) and a Manager instance.
*
* @param null|string $name
* @param null|Manager $manager
* @param Manager $manager
* @return void
*/
public function __construct($name = 'Default', $manager = null)
public function __construct($name = 'Default', Manager $manager = null)
{
if (!preg_match('/^[a-z][a-z0-9_\\\]+$/i', $name)) {
throw new Exception\InvalidArgumentException('Name passed to container is invalid; must consist of alphanumerics, backslashes and underscores only');
Expand Down Expand Up @@ -108,7 +108,7 @@ public static function getDefaultManager()
if (null === self::$defaultManager) {
$manager = new self::$managerDefaultClass();
if (!$manager instanceof Manager) {
throw new Exception\InvalidArgumentException('Invalid manager type provided; must implement Manager');
throw new Exception\InvalidArgumentException('Invalid default manager type provided; must implement Manager');
}
self::$defaultManager = $manager;
}
Expand Down Expand Up @@ -138,16 +138,16 @@ public function getManager()
/**
* Set session manager
*
* @param null|string|Manager $manager
* @param null|Manager $manager
* @return Container
*/
protected function setManager($manager)
protected function setManager(Manager $manager = null)
{
if (null === $manager) {
$manager = self::getDefaultManager();
}
if (!$manager instanceof Manager) {
throw new Exception\InvalidArgumentException('Manager provided is invalid; must implement Manager interface');
if (!$manager instanceof Manager) {
throw new Exception\InvalidArgumentException('Manager provided is invalid; must implement Manager interface');
}
}
$this->manager = $manager;
return $this;
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Session/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
interface Manager
{
public function __construct($config = null, $storage = null, $saveHandler = null);
public function __construct(Configuration $config = null, Storage $storage = null, SaveHandler $saveHandler = null);

public function getConfig();
public function getStorage();
Expand Down
19 changes: 4 additions & 15 deletions library/Zend/Session/SaveHandler/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ class Cache implements Savable
/**
* Constructor
*
* @param Zend\Cache\Storage\Adapter|null $storageAdapter
* @param Zend\Cache\Storage\Adapter $storageAdapter
* @return void
* @throws Zend\Session\Exception
*/
public function __construct($storageAdapter)
public function __construct(StorageAdapter $storageAdapter)
{
$this->setStorageAdapter($storageAdapter);
}
Expand Down Expand Up @@ -140,22 +140,11 @@ public function gc($maxlifetime)
*
* Allows passing a string class name or StorageAdapter object.
*
* @param string|Zend\Cache\Storage\Adapter
* @param Zend\Cache\Storage\Adapter
* @return void
*/
public function setStorageAdapter($storageAdapter)
public function setStorageAdapter(StorageAdapter $storageAdapter)
{
if (is_string($storageAdapter)) {
if (!class_exists($storageAdapter)) {
throw new Exception\InvalidArgumentException('Class provided for StorageAdapter does not exist');
}
$storageAdapter = new $storageAdapter;
}

if (!$storageAdapter instanceof StorageAdapter) {
throw new Exception\InvalidArgumentException('StorageAdapter type provided is invalid; must implement Zend\\Cache\\Storage\\Adapter');
}

$this->storageAdapter = $storageAdapter;
}

Expand Down
28 changes: 9 additions & 19 deletions library/Zend/Session/SaveHandler/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

namespace Zend\Session\SaveHandler;

use Zend\Session\SaveHandler as Savable,
use Zend\Config\Config as Configuration,
Zend\Session\SaveHandler as Savable,
Zend\Session\Container,
Zend\Session\Exception,
Zend\Db\Table\AbstractTable,
Expand All @@ -29,10 +30,6 @@
/**
* DB Table session save handler
*
* @uses Zend\Config
* @uses Zend_Db_Table_Abstract
* @uses Zend_Db_Table_Row_Abstract
* @uses Zend\Session\SaveHandler\Exception
* @category Zend
* @package Zend_Session
* @subpackage SaveHandler
Expand Down Expand Up @@ -128,9 +125,8 @@ class DbTable
/**
* Constructor
*
* $config is an instance of Zend_Config or an array of key/value pairs containing configuration options for
* Zend_Session_SaveHandler_DbTable and Zend_Db_Table_Abstract. These are the configuration options for
* Zend_Session_SaveHandler_DbTable:
* $config is an instance of Zend\Config\Config. These are the configuration options for
* Zend\Session\SaveHandler\DbTable:
*
* name => (string) Session table name
*
Expand All @@ -157,19 +153,13 @@ class DbTable
* overrideLifetime => (boolean) Whether or not the lifetime of an existing session should be overridden
* (optional; default: false)
*
* @param Zend_Config|array $config User-provided configuration
* @param Configuration User-provided configuration
* @return void
* @throws Zend_Session_SaveHandler_Exception
*/
public function __construct($config)
public function __construct(Configuration $config)
{
if ($config instanceof \Zend\Config\Config) {
$config = $config->toArray();
} else if (!is_array($config)) {
throw new Exception\InvalidArgumentException(
'$config must be an instance of Zend\\Config or array of key/value pairs containing '
. 'configuration options for Zend\\Session\\SaveHandler\\DbTable and Zend\\Db\\Table\\Abstract.');
}
$config = $config->toArray();

foreach ($config as $key => $value) {
do {
Expand Down Expand Up @@ -515,7 +505,7 @@ protected function _getPrimary($id, $type = null)
/**
* Retrieve session lifetime considering DbTable::OVERRIDE_LIFETIME
*
* @param Zend_Db_Table_Row_Abstract $row
* @param Zend\Db\Table\Row\Abstract $row
* @return int
*/
protected function _getLifetime(AbstractRow $row)
Expand All @@ -532,7 +522,7 @@ protected function _getLifetime(AbstractRow $row)
/**
* Retrieve session expiration time
*
* @param Zend_Db_Table_Row_Abstract $row
* @param Zend\Db\Table\Row\Abstract $row
* @return int
*/
protected function _getExpirationTime(AbstractRow $row)
Expand Down
10 changes: 0 additions & 10 deletions library/Zend/Session/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@
*/
class SessionManager extends AbstractManager
{
/**
* @var Configuration
*/
protected $config;

/**
* @var Storage
*/
protected $storage;

/**
* Default options when a call to {@link destroy()} is made
* - send_expire_cookie: whether or not to send a cookie expiring the current session cookie
Expand Down
6 changes: 6 additions & 0 deletions library/Zend/Session/Storage/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public function isLocked($key = null)
return array_key_exists($key, $locks);
}

/**
* Unlock an object or key marked as locked
*
* @param null|int|string $key
* @return ArrayStorage
*/
public function unlock($key = null)
{
if (null === $key) {
Expand Down
11 changes: 7 additions & 4 deletions tests/Zend/Session/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace ZendTest\Session;

use Zend\Session\Container,
Zend\Session\Configuration\StandardConfiguration,
Zend\Session\Manager,
Zend\Session;

Expand All @@ -41,10 +42,12 @@ public function setUp()
$this->forceAutoloader();
$_SESSION = array();
Container::setDefaultManager(null);
$this->manager = $manager = new TestAsset\TestManager(array(
'class' => 'Zend\\Session\\Configuration\\StandardConfiguration',

$config = new StandardConfiguration(array(
'storage' => 'Zend\\Session\\Storage\\ArrayStorage',
));

$this->manager = $manager = new TestAsset\TestManager($config);
$this->container = new Container('Default', $manager);
}

Expand Down Expand Up @@ -185,10 +188,10 @@ public function testContainerInstantiatesManagerWithDefaultsWhenNotInjected()

public function testContainerAllowsInjectingManagerViaConstructor()
{
$manager = new TestAsset\TestManager(array(
'class' => 'Zend\\Session\\Configuration\\StandardConfiguration',
$config = new StandardConfiguration(array(
'storage' => 'Zend\\Session\\Storage\\ArrayStorage',
));
$manager = new TestAsset\TestManager($config);
$container = new Container('Foo', $manager);
$this->assertSame($manager, $container->getManager());
}
Expand Down
Loading

0 comments on commit 1bba550

Please sign in to comment.