8000 [HttpFoundation] Split session handler callbacks to separate object. · symfony/symfony@2326707 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2326707

Browse files
author
Drak
committed
[HttpFoundation] Split session handler callbacks to separate object.
1 parent bb30a44 commit 2326707

File tree

4 files changed

+77
-44
lines changed

4 files changed

+77
-44
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/PdoSessionStorage.php renamed to src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\HttpFoundation\Session\Storage;
12+
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
1313

1414
/**
1515
* PdoSessionStorage.
1616
*
1717
* @author Fabien Potencier <fabien@symfony.com>
1818
* @author Michael Williams <michael.williams@funsational.com>
1919
*/
20-
class PdoSessionStorage extends AbstractSessionStorage implements \SessionHandlerInterface
20+
class PdoSessionHandler implements \SessionHandlerInterface
2121
{
2222
/**
2323
* PDO instance.
@@ -58,8 +58,6 @@ public function __construct(\PDO $pdo, array $dbOptions = array(), array $option
5858
'db_data_col' => 'sess_data',
5959
'db_time_col' => 'sess_time',
6060
), $dbOptions);
61-
62-
parent::__construct($options);
6361
}
6462

6563
/**

src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Session\Storage;
1313

14+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
15+
1416
/**
1517
* MockArraySessionStorage mocks the session for unit tests.
1618
*
@@ -23,7 +25,7 @@
2325
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
2426
* @author Drak <drak@zikula.org>
2527
*/
26-
class MockArraySessionStorage extends AbstractSessionStorage
28+
class MockArraySessionStorage extends SessionStorage
2729
{
2830
/**
2931
* @var string
@@ -35,6 +37,16 @@ class MockArraySessionStorage extends AbstractSessionStorage
3537
*/
3638
protected $sessionData = array();
3739

40+
public function __construct(array $options = array())
41+
{
42+
parent::__construct($options, new NullSessionHandler());
43+
}
44+
45+
/**
46+
* Sets the session data.
47+
*
48+
* @param array $array
49+
*/
3850
public function setSessionData(array $array)
3951
{
4052
$this->sessionData = $array;

src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MockFileSessionStorage extends MockArraySessionStorage
3333
* @param string $savePath Path of directory to save session files.
3434
* @param array $options Session options.
3535
*
36-
* @see AbstractSessionStorage::__construct()
36+
* @see SessionStorage::__construct()
3737
*/
3838
public function __construct($savePath = null, array $options = array())
3939
{

src/Symfony/Component/HttpFoundation/Session/Storage/AbstractSessionStorage.php renamed to src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorage.php

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,16 @@
1212
namespace Symfony\Component\HttpFoundation\Session\Storage;
1313

1414
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
15+
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
16+
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
17+
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
1518

1619
/**
1720
* This provides a base class for session attribute storage.
1821
*
19-
* This can be used to implement internal PHP session handlers
20-
* provided by PHP extensions or custom session save handlers
21-
* implementing the \SessionHandlerInterface
22-
*
23-
* @see http://php.net/session.customhandler
24-
* @see http://php.net/sessionhandlerinterface
25-
*
2622
* @author Drak <drak@zikula.org>
2723
*/
28-
abstract class AbstractSessionStorage implements SessionStorageInterface
24+
class SessionStorage implements SessionStorageInterface
2925
{
3026
/**
3127
* Array of SessionBagInterface
@@ -49,6 +45,11 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
4945
*/
5046
protected $closed = false;
5147

48+
/**
49+
* @var AbstractProxy
50+
*/
51+
protected $saveHandler;
52+
5253
/**
5354
* Constructor.
5455
*
@@ -75,7 +76,6 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
7576
* hash_function, "0"
7677
* name, "PHPSESSID"
7778
* referer_check, ""
78-
* save_path, ""
7979
* serialize_handler, "php"
8080
* use_cookies, "1"
8181
* use_only_cookies, "1"
@@ -89,12 +89,23 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
8989
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
9090
*
9191
* @param array $options Session configuration options.
92+
* @param $handler SessionHandlerInterface.
9293
*/
93-
public function __construct(array $options = array())
94+
public function __construct(array $options = array(), $handler = null)
9495
{
9596
$this->setOptions($options);
96-
$this->registerSaveHandlers();
97-
$this->registerShutdownFunction();
97+
98+
$this->setSaveHandler($handler);
99+
}
100+
101+
/**
102+
* Gets the save handler instance.
103+
*
104+
* @return AbstractProxy
105+
*/
106+
public function getSaveHandler()
107+
{
108+
return $this->saveHandler;
98109
}
99110

100111
/**
@@ -117,6 +128,10 @@ public function start()
117128

118129
$this->loadSession();
119130

131+
if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) {
132+
$this->saveHandler->setActive(false);
133+
}
134+
120135
$this->started = true;
121136
$this->closed = false;
122137

@@ -149,6 +164,11 @@ public function regenerate($destroy = false)
149164
public function save()
150165
{
151166
session_write_close();
167+
168+
if (!$this->saveHandler->isWrapper() && !$this->getSaveHandler()->isSessionHandlerInterface()) {
169+
$this->saveHandler->setActive(false);
170+
}
171+
152172
$this->closed = true;
153173
}
154174

@@ -230,7 +250,7 @@ protected function setOptions(array $options)
230250
'entropy_file', 'entropy_length', 'gc_divisor',
231251
'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character',
232252
'hash_function', 'name', 'referer_check',
233-
'save_path', 'serialize_handler', 'use_cookies',
253+
'serialize_handler', 'use_cookies',
234254
'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
235255
'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
236256
'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags'))) {
@@ -240,7 +260,7 @@ protected function setOptions(array $options)
240260
}
241261

242262
/**
243-
* Registers this storage device as a PHP session handler.
263+
* Registers save handler as a PHP session handler.
244264
*
245265
* To use internal PHP session save handlers, override this method using ini_set with
246266
* session.save_handlers and session.save_path e.g.
@@ -250,34 +270,37 @@ protected function setOptions(array $options)
250270
*
251271
* @see http://php.net/session-set-save-handler
252272
* @see http://php.net/sessionhandlerinterface
273+
* @see http://php.net/sessionhandler
274+
*
275+
* @param object $saveHandler
253276
*/
254-
protected function registerSaveHandlers()
277+
public function setSaveHandler($saveHandler)
255278
{
256-
// note this can be reset to PHP's control using ini_set('session.save_handler', 'files');
257-
// so long as ini_set() is called before the session is started.
258-
if ($this instanceof \SessionHandlerInterface) {
259-
session_set_save_handler(
260-
array($this, 'open'),
261-
array($this, 'close'),
262-
array($this, 'read'),
263-
array($this, 'write'),
264-
array($this, 'destroy'),
265-
array($this, 'gc')
266-
);
279+
// Wrap $saveHandler in proxy
280+
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
281+
$saveHandler = new SessionHandlerProxy($saveHandler);
282+
} else {
283+
$saveHandler = new NativeProxy($saveHandler);
267284
}
268-
}
269285

270-
/**
271-
* Registers PHP shutdown function.
272-
*
273-
* This method is required to avoid strange issues when using PHP objects as
274-
* session save handlers.
275-
*
276-
* @see http://php.net/register-shutdown-function
277-
*/
278-
protected function registerShutdownFunction()
279-
{
280-
register_shutdown_function('session_write_close');
286+
$this->saveHandler = $saveHandler;
287+
288+
if ($this->saveHandler instanceof \SessionHandlerInterface) {
289+
if (version_compare(phpversion(), '5.4.0', '>=')) {
290+
session_set_save_handler($this->saveHandler, true);
291+
} else {
292+
session_set_save_handler(
293+
array($this->saveHandler, 'open'),
294+
array($this->saveHandler, 'close'),
295+
array($this->saveHandler, 'read'),
296+
array($this->saveHandler, 'write'),
297+
array($this->saveHandler, 'destroy'),
298+
array($this->saveHandler, 'gc')
299+
);
300+
301+
register_shutdown_function('session_write_close');
302+
}
303+
}
281304
}
282305

283306
/**

0 commit comments

Comments
 (0)
0