Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into markup
Browse files Browse the repository at this point in the history
Conflicts:
	library/Zend/Markup/Parser/BBCode.php
	library/Zend/Markup/Renderer/HTML.php
	library/Zend/Markup/Renderer/RendererAbstract.php
  • Loading branch information
kokx committed Sep 26, 2010
3 parents 3a1e9c3 + f23a913 + 9ba9f17 commit 6250f70
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Configuration/SessionConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace Zend\Session\Configuration;

use Zend\Validator\Hostname\Hostname as HostnameValidator,
Zend\Session\Exception as SessionException;;
Zend\Session\Exception as SessionException;

/**
* Session configuration proxying to session INI options
Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/StandardConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use Zend\Session\Configuration as Configurable,
Zend\Session\Exception as SessionException,
Zend\Validator\Hostname\Hostname as HostnameValidator,
Zend\Validator\Hostname as HostnameValidator,
Zend\Filter\Word\CamelCaseToUnderscore as CamelCaseToUnderscoreFilter;

/**
Expand Down
82 changes: 74 additions & 8 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,18 @@ protected function _createContainer()
* If not, it raises an exception; otherwise, it returns the Storage
* object.
*
* @return Storage
* @param bool $createContainer Whether or not to create the container for the namespace
* @return Storage|null Returns null only if $createContainer is false
* @throws Exception
*/
protected function _verifyNamespace()
protected function _verifyNamespace($createContainer = true)
{
$storage = $this->_getStorage();
$name = $this->getName();
if (!isset($storage[$name])) {
if (!$createContainer) {
return;
}
$storage[$name] = $this->_createContainer();
}
if (!is_array($storage[$name]) && !$storage[$name] instanceof ArrayObject) {
Expand All @@ -205,16 +209,16 @@ protected function _verifyNamespace()
*
* Returns true if the key has expired, false otherwise.
*
* @param string $key
* @param null|string $key
* @return bool
*/
protected function _expireKeys($key)
protected function _expireKeys($key = null)
{
$storage = $this->_verifyNamespace();
$name = $this->getName();

// Return early if key not found
if (!isset($storage[$name][$key])) {
if ((null !== $key) && !isset($storage[$name][$key])) {
return true;
}

Expand Down Expand Up @@ -243,6 +247,8 @@ protected function _expireKeys($key)
protected function _expireByExpiryTime(Storage $storage, $name, $key)
{
$metadata = $storage->getMetadata($name);

// Global container expiry
if (is_array($metadata)
&& isset($metadata['EXPIRE'])
&& ($_SERVER['REQUEST_TIME'] > $metadata['EXPIRE'])
Expand All @@ -253,7 +259,9 @@ protected function _expireByExpiryTime(Storage $storage, $name, $key)
return true;
}

if (is_array($metadata)
// Expire individual key
if ((null !== $key)
&& is_array($metadata)
&& isset($metadata['EXPIRE_KEYS'])
&& isset($metadata['EXPIRE_KEYS'][$key])
&& ($_SERVER['REQUEST_TIME'] > $metadata['EXPIRE_KEYS'][$key])
Expand All @@ -264,6 +272,23 @@ protected function _expireByExpiryTime(Storage $storage, $name, $key)
return true;
}

// Find any keys that have expired
if ((null === $key)
&& is_array($metadata)
&& isset($metadata['EXPIRE_KEYS'])
) {
foreach (array_keys($metadata['EXPIRE_KEYS']) as $key) {
if ($_SERVER['REQUEST_TIME'] > $metadata['EXPIRE_KEYS'][$key]) {
unset($metadata['EXPIRE_KEYS'][$key]);
if (isset($storage[$name][$key])) {
unset($storage[$name][$key]);
}
}
}
$storage->setMetadata($name, $metadata, true);
return true;
}

return false;
}

Expand All @@ -282,6 +307,8 @@ protected function _expireByHops(Storage $storage, $name, $key)
{
$ts = $storage->getRequestAccessTime();
$metadata = $storage->getMetadata($name);

// Global container expiry
if (is_array($metadata)
&& isset($metadata['EXPIRE_HOPS'])
&& ($ts > $metadata['EXPIRE_HOPS']['ts'])
Expand All @@ -298,7 +325,9 @@ protected function _expireByHops(Storage $storage, $name, $key)
return false;
}

if (is_array($metadata)
// Single key expiry
if ((null !== $key)
&& is_array($metadata)
&& isset($metadata['EXPIRE_HOPS_KEYS'])
&& isset($metadata['EXPIRE_HOPS_KEYS'][$key])
&& ($ts > $metadata['EXPIRE_HOPS_KEYS'][$key]['ts'])
Expand All @@ -315,6 +344,27 @@ protected function _expireByHops(Storage $storage, $name, $key)
return false;
}

// Find all expired keys
if ((null === $key)
&& is_array($metadata)
&& isset($metadata['EXPIRE_HOPS_KEYS'])
) {
foreach (array_keys($metadata['EXPIRE_HOPS_KEYS']) as $key) {
if ($ts > $metadata['EXPIRE_HOPS_KEYS'][$key]['ts']) {
$metadata['EXPIRE_HOPS_KEYS'][$key]['hops']--;
if (-1 === $metadata['EXPIRE_HOPS_KEYS'][$key]['hops']) {
unset($metadata['EXPIRE_HOPS_KEYS'][$key]);
$storage->setMetadata($name, $metadata, true);
unset($storage[$name][$key]);
continue;
}
$metadata['EXPIRE_HOPS_KEYS'][$key]['ts'] = $ts;
}
}
$storage->setMetadata($name, $metadata, true);
return false;
}

return false;
}

Expand All @@ -341,7 +391,10 @@ public function offsetSet($key, $value)
*/
public function offsetExists($key)
{
$storage = $this->_verifyNamespace();
// If no container exists, we can't inspect it
if (null === ($storage = $this->_verifyNamespace(false))) {
return false;
}
$name = $this->getName();

// Return early if the key isn't set
Expand Down Expand Up @@ -385,6 +438,19 @@ public function offsetUnset($key)
unset($storage[$name][$key]);
}

/**
* Iterate over session container
*
* @return Iterator
*/
public function getIterator()
{
$this->_expireKeys();
$storage = $this->_getStorage();
$container = $storage[$this->getName()];
return $container->getIterator();
}

/**
* Set expiration TTL
*
Expand Down
4 changes: 2 additions & 2 deletions src/SaveHandler/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
use Zend\Session\SaveHandler as Savable,
Zend\Session\Container,
Zend\Session\Manager,
Zend\DB\Table\AbstractTable,
Zend\DB\Table\Row\AbstractRow;
Zend\Db\Table\AbstractTable,
Zend\Db\Table\AbstractRow;

/**
* DB Table session save handler
Expand Down
12 changes: 11 additions & 1 deletion src/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SessionManager extends AbstractManager
public function sessionExists()
{
$sid = defined('SID') ? constant('SID') : false;
if ($sid && $this->getId()) {
if ($sid !== false && $this->getId()) {
return true;
}
return false;
Expand All @@ -101,6 +101,16 @@ public function start()
if (!$this->isValid()) {
throw new Exception('Session failed validation');
}
$storage = $this->getStorage();

// Since session is starting, we need to potentially repopulate our
// session storage
if ($storage instanceof Storage\SessionStorage
&& $_SESSION !== $storage
) {
$storage->fromArray($_SESSION);
$_SESSION = $storage;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class HTTPUserAgent implements SessionValidator
class HttpUserAgent implements SessionValidator
{
/**
* Constructor - get the current user agent and store it in the session
Expand Down
86 changes: 86 additions & 0 deletions test/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->forceAutoloader();
$_SESSION = array();
Container::setDefaultManager(null);
$this->manager = $manager = new TestAsset\TestManager(array(
Expand All @@ -25,6 +26,27 @@ public function tearDown()
Container::setDefaultManager(null);
}

protected function forceAutoloader()
{
$splAutoloadFunctions = spl_autoload_functions();
if (!$splAutoloadFunctions || !in_array('ZendTest_Autoloader', $splAutoloadFunctions)) {
include __DIR__ . '/../../_autoload.php';
}
}

/**
* Hack to allow running tests in separate processes
*
* @see http://matthewturland.com/2010/08/19/process-isolation-in-phpunit/
* @param PHPUnit_Framework_TestResult $result
* @return void
*/
public function run(\PHPUnit_Framework_TestResult $result = NULL)
{
$this->setPreserveGlobalState(false);
return parent::run($result);
}

public function testInstantiationStartsSession()
{
$this->manager->destroy();
Expand Down Expand Up @@ -379,4 +401,68 @@ public function testKeysExpireAfterSpecifiedHops()
$this->assertEquals('baz', $this->container->bar);
$this->assertNull($this->container->baz);
}

public function testCanIterateOverContainer()
{
$this->container->foo = 'bar';
$this->container->bar = 'baz';
$this->container->baz = 'bat';
$expected = array(
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'bat',
);
$test = array();
foreach ($this->container as $key => $value) {
$test[$key] = $value;
}
$this->assertSame($expected, $test);
}

public function testIterationHonorsExpirationHops()
{
$this->container->foo = 'bar';
$this->container->bar = 'baz';
$this->container->baz = 'bat';
$this->container->setExpirationHops(1, array('foo', 'baz'));

$storage = $this->manager->getStorage();
$ts = $storage->getRequestAccessTime();

// First hop
$storage->setMetadata('_REQUEST_ACCESS_TIME', $ts + 60);
$expected = array(
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'bat',
);
$test = array();
foreach ($this->container as $key => $value) {
$test[$key] = $value;
}
$this->assertSame($expected, $test);

// Second hop
$storage->setMetadata('_REQUEST_ACCESS_TIME', $ts + 120);
$expected = array('bar' => 'baz');
$test = array();
foreach ($this->container as $key => $value) {
$test[$key] = $value;
}
$this->assertSame($expected, $test);
}

public function testIterationHonorsExpirationTimestamps()
{
$this->container->foo = 'bar';
$this->container->bar = 'baz';
$storage = $this->manager->getStorage();
$storage->setMetadata('Default', array('EXPIRE_KEYS' => array('foo' => $_SERVER['REQUEST_TIME'] - 18600)));
$expected = array('bar' => 'baz');
$test = array();
foreach ($this->container as $key => $value) {
$test[$key] = $value;
}
$this->assertSame($expected, $test);
}
}
Loading

0 comments on commit 6250f70

Please sign in to comment.