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

Commit

Permalink
Merge branch 'hotfix/3639'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function start($preserveStorage = false)
$storage->fromArray($_SESSION);
}
$_SESSION = $storage;
} elseif ($storage instanceof Storage\SessionArrayStorage) {
$storage->fromArray($_SESSION);
}

if (!$this->isValid()) {
throw new Exception\RuntimeException('Session validation failed');
}
Expand Down Expand Up @@ -159,7 +162,7 @@ public function writeClose()
// object isImmutable.
$storage = $this->getStorage();
if (!$storage->isImmutable()) {
$_SESSION = $storage->toArray();
$_SESSION = $storage->toArray(true);
session_write_close();
$storage->fromArray($_SESSION);
$storage->markImmutable();
Expand Down
5 changes: 4 additions & 1 deletion src/Storage/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,12 @@ public function fromArray(array $array)
*
* @return array
*/
public function toArray()
public function toArray($metaData = false)
{
$values = $this->getArrayCopy();
if ($metaData) {
return $values;
}
if (isset($values['__ZF'])) {
unset($values['__ZF']);
}
Expand Down
15 changes: 10 additions & 5 deletions src/Storage/SessionArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ public function getIterator()
public function fromArray(array $array)
{
$ts = $this->getRequestAccessTime();
if (!$ts) {
$ts = microtime(true);
}
$_SESSION = $array;
$this->setRequestAccessTime($ts);
return $this;
Expand Down Expand Up @@ -346,10 +349,7 @@ public function setMetadata($key, $value, $overwriteArray = false)
}
} else {
if ((null === $value) && isset($_SESSION['__ZF'][$key])) {
$array = $_SESSION['__ZF'];
unset($array[$key]);
$_SESSION['__ZF'] = $array;
unset($array);
unset($_SESSION['__ZF'][$key]);
} elseif (null !== $value) {
$_SESSION['__ZF'][$key] = $value;
}
Expand Down Expand Up @@ -444,13 +444,18 @@ protected function setRequestAccessTime($time)
*
* @return array
*/
public function toArray()
public function toArray($metaData = false)
{
if (isset($_SESSION)) {
$values = $_SESSION;
} else {
$values = array();
}

if ($metaData) {
return $values;
}

if (isset($values['__ZF'])) {
unset($values['__ZF']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ public function getMetadata($key = null);
public function clear($key = null);

public function fromArray(array $array);
public function toArray();
public function toArray($metaData = false);
}
16 changes: 16 additions & 0 deletions test/SessionArrayStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,20 @@ public function testSessionWorksWithContainer()

}

public function testToArrayWithMetaData()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->setMetadata('foo', 'bar');
$expected = array(
'__ZF' => array(
'_REQUEST_ACCESS_TIME' => $this->storage->getRequestAccessTime(),
'foo' => 'bar',
),
'foo' => 'bar',
'bar' => 'baz',
);
$this->assertSame($expected, $this->storage->toArray(true));
}

}
14 changes: 14 additions & 0 deletions test/SessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,18 @@ public function testResumeSessionThatFailsAValidatorShouldRaiseException()
$this->setExpectedException('Zend\Session\Exception\RuntimeException', 'failed');
$this->manager->start();
}

/**
* @runInSeparateProcess
*/
public function testSessionWriteCloseStoresMetadata()
{
$this->manager->start();
$storage = $this->manager->getStorage();
$storage->setMetadata('foo', 'bar');
$metaData = $storage->getMetadata();
$this->manager->writeClose();
$this->assertSame($_SESSION['__ZF'], $metaData);
}

}
16 changes: 16 additions & 0 deletions test/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,20 @@ public function testRequestAccessTimeIsPreservedEvenInFactoryMethod()
$this->storage->fromArray(array());
$this->assertNotEmpty($this->storage->getRequestAccessTime());
}

public function testToArrayWithMetaData()
{
$this->storage->foo = 'bar';
$this->storage->bar = 'baz';
$this->storage->setMetadata('foo', 'bar');
$expected = array(
'__ZF' => array(
'_REQUEST_ACCESS_TIME' => $this->storage->getRequestAccessTime(),
'foo' => 'bar',
),
'foo' => 'bar',
'bar' => 'baz',
);
$this->assertSame($expected, $this->storage->toArray(true));
}
}

0 comments on commit 300993b

Please sign in to comment.