12
12
namespace Symfony \Component \HttpFoundation \Session \Storage ;
13
13
14
14
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 ;
15
18
16
19
/**
17
20
* This provides a base class for session attribute storage.
18
21
*
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
- *
26
22
* @author Drak <drak@zikula.org>
27
23
*/
28
- abstract class AbstractSessionStorage implements SessionStorageInterface
24
+ class SessionStorage implements SessionStorageInterface
29
25
{
30
26
/**
31
27
* Array of SessionBagInterface
@@ -49,6 +45,11 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
49
45
*/
50
46
protected $ closed = false ;
51
47
48
+ /**
49
+ * @var AbstractProxy
50
+ */
51
+ protected $ saveHandler ;
52
+
52
53
/**
53
54
* Constructor.
54
55
*
@@ -75,7 +76,6 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
75
76
* hash_function, "0"
10000
code>
76
77
* name, "PHPSESSID"
77
78
* referer_check, ""
78
- * save_path, ""
79
79
* serialize_handler, "php"
80
80
* use_cookies, "1"
81
81
* use_only_cookies, "1"
@@ -89,12 +89,23 @@ abstract class AbstractSessionStorage implements SessionStorageInterface
89
89
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
90
90
*
91
91
* @param array $options Session configuration options.
92
+ * @param $handler SessionHandlerInterface.
92
93
*/
93
- public function __construct (array $ options = array ())
94
+ public function __construct (array $ options = array (), $ handler = null )
94
95
{
95
96
$ 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 ;
98
109
}
99
110
100
111
/**
@@ -117,6 +128,10 @@ public function start()
117
128
118
129
$ this ->loadSession ();
119
130
131
+ if (!$ this ->saveHandler ->isWrapper () && !$ this ->saveHandler ->isSessionHandlerInterface ()) {
132
+ $ this ->saveHandler ->setActive (false );
133
+ }
134
+
120
135
$ this ->started = true ;
121
136
$ this ->closed = false ;
122
137
@@ -149,6 +164,11 @@ public function regenerate($destroy = false)
149
164
public function save ()
150
165
{
151
166
session_write_close ();
167
+
168
+ if (!$ this ->saveHandler ->isWrapper () && !$ this ->getSaveHandler ()->isSessionHandlerInterface ()) {
169
+ $ this ->saveHandler ->setActive (false );
170
+ }
171
+
152
172
$ this ->closed = true ;
153
173
}
154
174
@@ -230,7 +250,7 @@ protected function setOptions(array $options)
230
250
'entropy_file ' , 'entropy_length ' , 'gc_divisor ' ,
231
251
'gc_maxlifetime ' , 'gc_probability ' , 'hash_bits_per_character ' ,
232
252
'hash_function ' , 'name ' , 'referer_check ' ,
233
- 'save_path ' , ' serialize_handler ' , 'use_cookies ' ,
253
+ 'serialize_handler ' , 'use_cookies ' ,
234
254
'use_only_cookies ' , 'use_trans_sid ' , 'upload_progress.enabled ' ,
235
255
'upload_progress.cleanup ' , 'upload_progress.prefix ' , 'upload_progress.name ' ,
236
256
'upload_progress.freq ' , 'upload_progress.min-freq ' , 'url_rewriter.tags ' ))) {
@@ -240,7 +260,7 @@ protected function setOptions(array $options)
240
260
}
241
261
242
262
/**
243
- * Registers this storage device as a PHP session handler.
263
+ * Registers save handler as a PHP session handler.
244
264
*
245
265
* To use internal PHP session save handlers, override this method using ini_set with
246
266
* session.save_handlers and session.save_path e.g.
@@ -250,34 +270,37 @@ protected function setOptions(array $options)
250
270
*
251
271
* @see http://php.net/session-set-save-handler
252
272
* @see http://php.net/sessionhandlerinterface
273
+ * @see http://php.net/sessionhandler
274
+ *
275
+ * @param object $saveHandler
253
276
*/
254
- protected function registerSaveHandlers ( )
277
+ public function setSaveHandler ( $ saveHandler )
255
278
{
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 );
267
284
}
268
- }
269
285
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
+ }
281
304
}
282
305
283
306
/**
0 commit comments