6
6
namespace Magento \Framework \Session ;
7
7
8
8
use Magento \Framework \Session \Config \ConfigInterface ;
9
- use \ Magento \Framework \Exception \SessionException ;
9
+ use Magento \Framework \Exception \SessionException ;
10
10
11
11
/**
12
12
* Magento session save handler
@@ -21,8 +21,21 @@ class SaveHandler implements SaveHandlerInterface
21
21
protected $ saveHandlerAdapter ;
22
22
23
23
/**
24
- * Constructor
25
- *
24
+ * @var SaveHandlerFactory
25
+ */
26
+ private $ saveHandlerFactory ;
27
+
28
+ /**
29
+ * @var ConfigInterface
30
+ */
31
+ private $ sessionConfig ;
32
+
33
+ /**
34
+ * @var string
35
+ */
36
+ private $ defaultHandler ;
37
+
38
+ /**
26
39
* @param SaveHandlerFactory $saveHandlerFactory
27
40
* @param ConfigInterface $sessionConfig
28
41
* @param string $default
@@ -32,18 +45,22 @@ public function __construct(
32
45
ConfigInterface $ sessionConfig ,
33
46
$ default = self ::DEFAULT_HANDLER
34
47
) {
48
+ $ this ->saveHandlerFactory = $ saveHandlerFactory ;
49
+ $ this ->sessionConfig = $ sessionConfig ;
50
+ $ this ->defaultHandler = $ default ;
51
+
35
52
/**
36
53
* Session handler
37
54
*
38
55
* Save handler may be set to custom value in deployment config, which will override everything else.
39
56
* Otherwise, try to read PHP settings for session.save_handler value. Otherwise, use 'files' as default.
40
57
*/
41
- $ saveMethod = $ sessionConfig ->getOption ('session.save_handler ' ) ?: $ default ;
58
+ $ saveMethod = $ this -> sessionConfig ->getOption ('session.save_handler ' ) ?: $ this -> defaultHandler ;
42
59
43
60
try {
44
- $ this ->saveHandlerAdapter = $ saveHandlerFactory ->create ($ saveMethod );
61
+ $ this ->saveHandlerAdapter = $ this -> saveHandlerFactory ->create ($ saveMethod );
45
62
} catch (SessionException $ e ) {
46
- $ this ->saveHandlerAdapter = $ saveHandlerFactory ->create ($ default );
63
+ $ this ->saveHandlerAdapter = $ this -> saveHandlerFactory ->create ($ this -> defaultHandler );
47
64
}
48
65
}
49
66
@@ -56,7 +73,7 @@ public function __construct(
56
73
*/
57
74
public function open ($ savePath , $ name )
58
75
{
59
- return $ this ->saveHandlerAdapter -> open ( $ savePath , $ name );
76
+ return $ this ->callSafely ( ' open ' , $ savePath , $ name );
60
77
}
61
78
62
79
/**
@@ -66,7 +83,7 @@ public function open($savePath, $name)
66
83
*/
67
84
public function close ()
68
85
{
69
- return $ this ->saveHandlerAdapter -> close ( );
86
+ return $ this ->callSafely ( ' close ' );
70
87
}
71
88
72
89
/**
@@ -77,7 +94,7 @@ public function close()
77
94
*/
78
95
public function read ($ sessionId )
79
96
{
80
- return $ this ->saveHandlerAdapter -> read ( $ sessionId );
97
+ return $ this ->callSafely ( ' read ' , $ sessionId );
81
98
}
82
99
83
100
/**
@@ -89,7 +106,7 @@ public function read($sessionId)
89
106
*/
90
107
public function write ($ sessionId , $ data )
91
108
{
92
- return $ this ->saveHandlerAdapter -> write ( $ sessionId , $ data );
109
+ return $ this ->callSafely ( ' write ' , $ sessionId , $ data );
93
110
}
94
111
95
112
/**
@@ -100,19 +117,38 @@ public function write($sessionId, $data)
100
117
*/
101
118
public function destroy ($ sessionId )
102
119
{
103
- return $ this ->saveHandlerAdapter -> destroy ( $ sessionId );
120
+ return $ this ->callSafely ( ' destroy ' , $ sessionId );
104
121
}
105
122
106
123
/**
107
- * Garbage Collection - remove old session data older
108
- * than $maxLifetime (in seconds)
124
+ * Garbage Collection - remove old session data older than $maxLifetime (in seconds)
109
125
*
110
126
* @param int $maxLifetime
111
127
* @return bool
112
128
* @SuppressWarnings(PHPMD.ShortMethodName)
113
129
*/
114
130
public function gc ($ maxLifetime )
115
131
{
116
- return $ this ->saveHandlerAdapter ->gc ($ maxLifetime );
132
+ return $ this ->callSafely ('gc ' , $ maxLifetime );
133
+ }
134
+
135
+ /**
136
+ * Call save handler adapter method.
137
+ *
138
+ * In case custom handler failed, default files handler is used.
139
+ *
140
+ * @param string $method
141
+ * @param mixed $arguments
142
+ *
143
+ * @return mixed
144
+ */
145
+ private function callSafely (string $ method , ...$ arguments )
146
+ {
147
+ try {
148
+ return $ this ->saveHandlerAdapter ->{$ method }(...$ arguments );
149
+ } catch (SessionException $ exception ) {
150
+ $ this ->saveHandlerAdapter = $ this ->saveHandlerFactory ->create ($ this ->defaultHandler );
151
+ return $ this ->saveHandlerAdapter ->{$ method }(...$ arguments );
152
+ }
117
153
}
118
154
}
0 commit comments