Skip to content

Commit d088b84

Browse files
committed
MC-28948: Doesn't work: "Files" as fallback when Magento fails to connect to "Redis"
1 parent 81cf39d commit d088b84

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

lib/internal/Magento/Framework/Session/SaveHandler.php

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Framework\Session;
77

88
use Magento\Framework\Session\Config\ConfigInterface;
9-
use \Magento\Framework\Exception\SessionException;
9+
use Magento\Framework\Exception\SessionException;
1010

1111
/**
1212
* Magento session save handler
@@ -21,8 +21,21 @@ class SaveHandler implements SaveHandlerInterface
2121
protected $saveHandlerAdapter;
2222

2323
/**
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+
/**
2639
* @param SaveHandlerFactory $saveHandlerFactory
2740
* @param ConfigInterface $sessionConfig
2841
* @param string $default
@@ -32,18 +45,22 @@ public function __construct(
3245
ConfigInterface $sessionConfig,
3346
$default = self::DEFAULT_HANDLER
3447
) {
48+
$this->saveHandlerFactory = $saveHandlerFactory;
49+
$this->sessionConfig = $sessionConfig;
50+
$this->defaultHandler = $default;
51+
3552
/**
3653
* Session handler
3754
*
3855
* Save handler may be set to custom value in deployment config, which will override everything else.
3956
* Otherwise, try to read PHP settings for session.save_handler value. Otherwise, use 'files' as default.
4057
*/
41-
$saveMethod = $sessionConfig->getOption('session.save_handler') ?: $default;
58+
$saveMethod = $this->sessionConfig->getOption('session.save_handler') ?: $this->defaultHandler;
4259

4360
try {
44-
$this->saveHandlerAdapter = $saveHandlerFactory->create($saveMethod);
61+
$this->saveHandlerAdapter = $this->saveHandlerFactory->create($saveMethod);
4562
} catch (SessionException $e) {
46-
$this->saveHandlerAdapter = $saveHandlerFactory->create($default);
63+
$this->saveHandlerAdapter = $this->saveHandlerFactory->create($this->defaultHandler);
4764
}
4865
}
4966

@@ -56,7 +73,7 @@ public function __construct(
5673
*/
5774
public function open($savePath, $name)
5875
{
59-
return $this->saveHandlerAdapter->open($savePath, $name);
76+
return $this->callSafely('open', $savePath, $name);
6077
}
6178

6279
/**
@@ -66,7 +83,7 @@ public function open($savePath, $name)
6683
*/
6784
public function close()
6885
{
69-
return $this->saveHandlerAdapter->close();
86+
return $this->callSafely('close');
7087
}
7188

7289
/**
@@ -77,7 +94,7 @@ public function close()
7794
*/
7895
public function read($sessionId)
7996
{
80-
return $this->saveHandlerAdapter->read($sessionId);
97+
return $this->callSafely('read', $sessionId);
8198
}
8299

83100
/**
@@ -89,7 +106,7 @@ public function read($sessionId)
89106
*/
90107
public function write($sessionId, $data)
91108
{
92-
return $this->saveHandlerAdapter->write($sessionId, $data);
109+
return $this->callSafely('write', $sessionId, $data);
93110
}
94111

95112
/**
@@ -100,19 +117,38 @@ public function write($sessionId, $data)
100117
*/
101118
public function destroy($sessionId)
102119
{
103-
return $this->saveHandlerAdapter->destroy($sessionId);
120+
return $this->callSafely('destroy', $sessionId);
104121
}
105122

106123
/**
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)
109125
*
110126
* @param int $maxLifetime
111127
* @return bool
112128
* @SuppressWarnings(PHPMD.ShortMethodName)
113129
*/
114130
public function gc($maxLifetime)
115131
{
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+
}
117153
}
118154
}

lib/internal/Magento/Framework/Session/SaveHandler/Redis.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ class Redis implements \SessionHandlerInterface
4343
* @param ConfigInterface $config
4444
* @param LoggerInterface $logger
4545
* @param Filesystem $filesystem
46-
* @throws SessionException
4746
*/
4847
public function __construct(ConfigInterface $config, LoggerInterface $logger, Filesystem $filesystem)
4948
{
5049
$this->config = $config;
5150
$this->logger = $logger;
5251
$this->filesystem = $filesystem;
53-
$this->getConnection();
5452
}
5553

5654
/**

0 commit comments

Comments
 (0)