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

Commit

Permalink
Merge 69eb479 into 7e73dc4
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Nov 28, 2017
2 parents 7e73dc4 + 69eb479 commit 0f4c128
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,20 @@ matrix:
- php: 7.1
env:
- DEPS=latest
- php: nightly
- php: 7.2
env:
- DEPS=lowest
- php: nightly
- php: 7.2
env:
- DEPS=locked
- php: nightly
- php: 7.2
env:
- DEPS=latest
allow_failures:
- php: nightly

before_install:
- pecl -q install mongodb
- if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
- pecl -q upgrade mongodb
- $(php -m | grep -q mongodb) || echo "extension=mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- travis_retry composer self-update

install:
Expand Down
88 changes: 78 additions & 10 deletions src/Config/SessionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Session\Config;

use SessionHandlerInterface;
use Zend\Session\Exception;

/**
Expand All @@ -33,6 +34,11 @@ class SessionConfig extends StandardConfig
*/
protected $rememberMeSeconds = 1209600; // 2 weeks

/**
* @var string
*/
protected $saveHandler;

/**
* @var string session.serialize_handler
*/
Expand Down Expand Up @@ -89,6 +95,10 @@ public function setStorageOption($storageName, $storageValue)
case 'url_rewriter_tags':
$key = 'url_rewriter.tags';
break;
case 'save_handler':
// Save handlers must be treated differently due to changes
// introduced in PHP 7.2. Do not alter running INI setting.
return $this;
default:
$key = 'session.' . $storageName;
break;
Expand Down Expand Up @@ -126,11 +136,31 @@ public function getStorageOption($storageOption)
case 'use_trans_sid':
case 'cookie_httponly':
return (bool) ini_get('session.' . $storageOption);
case 'save_handler':
// Save handlers must be treated differently due to changes
// introduced in PHP 7.2.
return $this->saveHandler ?: ini_get('session.save_handler');
default:
return ini_get('session.' . $storageOption);
}
}

/**
* Proxy to setPhpSaveHandler()
*
* Prevents calls to `setSaveHandler()` from hitting `setOption()` instead,
* and thus bypassing the logic of `setPhpSaveHandler()`.
*
* @param string $phpSaveHandler
* @return SessionConfig
* @throws Exception\InvalidArgumentException
*/
public function setSaveHandler($phpSaveHandler)
{
// error_log(sprintf('In %s with %s', __METHOD__, var_export($phpSaveHandler, true)));
return $this->setPhpSaveHandler($phpSaveHandler);
}

/**
* Set session.save_handler
*
Expand All @@ -140,17 +170,55 @@ public function getStorageOption($storageOption)
*/
public function setPhpSaveHandler($phpSaveHandler)
{
$phpSaveHandler = (string) $phpSaveHandler;
set_error_handler([$this, 'handleError']);
ini_set('session.save_handler', $phpSaveHandler);
restore_error_handler();
if ($this->phpErrorCode >= E_WARNING) {
throw new Exception\InvalidArgumentException(
'Invalid save handler specified: ' . $this->phpErrorMessage
);
// error_log(sprintf('In %s with %s', __METHOD__, var_export($phpSaveHandler, true)));
if ('files' === $phpSaveHandler
|| ('user' === $phpSaveHandler && \PHP_MAJOR_VERSION > 5 && \PHP_MINOR_VERSION < 2)
) {
set_error_handler([$this, 'handleError']);
ini_set('session.save_handler', $phpSaveHandler);
restore_error_handler();
if ($this->phpErrorCode >= E_WARNING) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid save handler specified: %s',
$this->phpErrorMessage
));
}

$this->saveHandler = $phpSaveHandler;
$this->setOption('save_handler', $phpSaveHandler);
return $this;
}

if (is_string($phpSaveHandler)
&& (! class_exists($phpSaveHandler)
|| ! (in_array(SessionHandlerInterface::class, class_implements($phpSaveHandler)))
)
) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid save handler specified ("%s"); must be one of "files"'
. ' or a class implementing %s',
$phpSaveHandler,
SessionHandlerInterface::class,
SessionHandlerInterface::class
));
}

if (is_string($phpSaveHandler)) {
$phpSaveHandler = new $phpSaveHandler();
}

$this->setOption('save_handler', $phpSaveHandler);
if (! $phpSaveHandler instanceof SessionHandlerInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid save handler specified ("%s"); must implement %s',
get_class($phpSaveHandler),
SessionHandlerInterface::class
));
}

session_set_save_handler($phpSaveHandler);

$this->saveHandler = get_class($phpSaveHandler);
$this->setOption('save_handler', $this->saveHandler);
return $this;
}

Expand All @@ -163,7 +231,7 @@ public function setPhpSaveHandler($phpSaveHandler)
*/
public function setSavePath($savePath)
{
if ($this->getOption('save_handler') == 'files') {
if ($this->getOption('save_handler') === 'files') {
parent::setSavePath($savePath);
}
$this->savePath = $savePath;
Expand Down
19 changes: 10 additions & 9 deletions test/Config/SessionConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
namespace ZendTest\Session\Config;

use PHPUnit\Framework\TestCase;
use SessionHandlerInterface;
use Zend\Session\Config\SessionConfig;
use ZendTest\Session\TestAsset\TestSaveHandler;

/**
* @group Zend_Session
* @runTestsInSeparateProcesses
* @covers Zend\Session\Config\SessionConfig
*/
Expand Down Expand Up @@ -65,7 +66,7 @@ public function testSavePathAltersIniSetting()

public function testSavePathCanBeNonDirectoryWhenSaveHandlerNotFiles()
{
$this->config->setPhpSaveHandler('user');
$this->config->setPhpSaveHandler(TestSaveHandler::class);
$this->config->setSavePath('/tmp/sessions.db');
$this->assertEquals('/tmp/sessions.db', ini_get('session.save_path'));
}
Expand Down Expand Up @@ -102,14 +103,14 @@ public function testSaveHandlerDefaultsToIniSettings()

public function testSaveHandlerIsMutable()
{
$this->config->setSaveHandler('user');
$this->assertEquals('user', $this->config->getSaveHandler());
$this->config->setSaveHandler(TestSaveHandler::class);
$this->assertSame(TestSaveHandler::class, $this->config->getSaveHandler());
}

public function testSaveHandlerAltersIniSetting()
public function testSaveHandlerDoesNotAlterIniSetting()
{
$this->config->setSaveHandler('user');
$this->assertEquals('user', ini_get('session.save_handler'));
$this->config->setSaveHandler(TestSaveHandler::class);
$this->assertNotSame(TestSaveHandler::class, ini_get('session.save_handler'));
}

public function testSettingInvalidSaveHandlerRaisesException()
Expand Down Expand Up @@ -1023,10 +1024,10 @@ public function optionsProvider()
'getName',
'FOOBAR',
],
[
'UserDefinedSaveHandler' => [
'save_handler',
'getOption',
'user',
'files',
],
[
'gc_probability',
Expand Down

0 comments on commit 0f4c128

Please sign in to comment.