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

performSaveHandlerUpdate #118

Merged
merged 12 commits into from
Aug 11, 2019
25 changes: 24 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.8.6 - TBD
## 2.8.7 - TBD

### Added

Expand All @@ -24,6 +24,29 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 2.8.6 - 2019-08-10

### Added

- Nothing.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above line can be removed, too many params.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it

));
}

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);
}
}