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/118' into develop
Browse files Browse the repository at this point in the history
Forward port #118
  • Loading branch information
michalbundyra committed Aug 11, 2019
2 parents 9abed5e + 953245a commit c3c0c11
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ All notable changes to this project will be documented in this file, in reverse
can be now used with PHP 7.1+ instead of `session.hash_bits_per_character`
(used with PHP versions prior to 7.1).

- [#118](https://github.com/zendframework/zend-session/pull/118) avoid unnecessary phpinfo() call
when register own save handler which is an object.

## 2.8.5 - 2018-02-22

### Added
Expand Down
55 changes: 26 additions & 29 deletions src/Config/SessionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,40 +447,37 @@ private function locateRegisteredSaveHandlers()
*/
private function performSaveHandlerUpdate($phpSaveHandler)
{
$knownHandlers = $this->locateRegisteredSaveHandlers();

if (in_array($phpSaveHandler, $knownHandlers, true)) {
$phpSaveHandler = strtolower($phpSaveHandler);
set_error_handler([$this, 'handleError']);
session_module_name($phpSaveHandler);
restore_error_handler();
if ($this->phpErrorCode >= E_WARNING) {
if (is_string($phpSaveHandler)) {
$knownHandlers = $this->locateRegisteredSaveHandlers();

if (in_array($phpSaveHandler, $knownHandlers, true)) {
$phpSaveHandler = strtolower($phpSaveHandler);
set_error_handler([$this, 'handleError']);
session_module_name($phpSaveHandler);
restore_error_handler();
if ($this->phpErrorCode >= E_WARNING) {
throw new Exception\InvalidArgumentException(sprintf(
'Error setting session save handler module "%s": %s',
$phpSaveHandler,
$this->phpErrorMessage
));
}

return $phpSaveHandler;
}

if (! class_exists($phpSaveHandler)
|| ! is_a($phpSaveHandler, SessionHandlerInterface::class, true)
) {
throw new Exception\InvalidArgumentException(sprintf(
'Error setting session save handler module "%s": %s',
'Invalid save handler specified ("%s"); must be one of [%s]'
. ' or a class implementing %s',
$phpSaveHandler,
$this->phpErrorMessage
implode(', ', $knownHandlers),
SessionHandlerInterface::class
));
}

return $phpSaveHandler;
}

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 [%s]'
. ' or a class implementing %s',
$phpSaveHandler,
implode(', ', $knownHandlers),
SessionHandlerInterface::class,
SessionHandlerInterface::class
));
}

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

Expand Down
20 changes: 18 additions & 2 deletions test/Config/SessionConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use stdClass;
use Zend\Session\Config\SessionConfig;
use Zend\Session\Exception;
use Zend\Session\SaveHandler\SaveHandlerInterface;
use ZendTest\Session\TestAsset\TestSaveHandler;

/**
* @runTestsInSeparateProcesses
* @covers Zend\Session\Config\SessionConfig
* @covers \Zend\Session\Config\SessionConfig
*/
class SessionConfigTest extends TestCase
{
Expand All @@ -31,11 +32,16 @@ class SessionConfigTest extends TestCase
*/
protected $config;

public function setUp()
protected function setUp()
{
$this->config = new SessionConfig;
}

protected function tearDown()
{
$this->config = null;
}

public function assertPhpLessThan71($message = 'This test requires a PHP version less than 7.1.0')
{
if (PHP_VERSION_ID < 70100) {
Expand Down Expand Up @@ -1245,4 +1251,14 @@ public function testCanProvidePathWhenUsingRedisSaveHandler()

$this->assertSame($url, $this->config->getOption('save_path'));
}

public function testNotCallLocateRegisteredSaveHandlersMethodIfSessionHandlerInterfaceWasPassed()
{
$phpinfo = $this->getFunctionMock('Zend\Session\Config', 'phpinfo');
$phpinfo
->expects($this->never());

$saveHandler = $this->createMock(SessionHandlerInterface::class);
$this->config->setPhpSaveHandler($saveHandler);
}
}

0 comments on commit c3c0c11

Please sign in to comment.