Skip to content

Commit d86fc37

Browse files
Merge pull request #197 from forfin/php-redis-session-abstract_v2
Upgrade php-redis-session-abstract to v2
2 parents 43db186 + 931f75b commit d86fc37

File tree

6 files changed

+153
-3
lines changed

6 files changed

+153
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.basedir
2+
vendor
3+
composer.lock

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ The Magento Compiler feature is currently not supported.
8484
<min_lifetime>60</min_lifetime> <!-- Set the minimum session lifetime -->
8585
<max_lifetime>2592000</max_lifetime> <!-- Set the maximum session lifetime -->
8686
<log_exceptions>0</log_exceptions> <!-- Log connection failure and concurrent connection exceptions in exception.log. -->
87+
<retries>0</retries> <!-- Connection retries for sentinels -->
88+
<username></username> <!-- Optional: username -->
89+
<!--
90+
Note for TLS options: If the cluster seeds start with "ssl:// or tls://",
91+
it will connect to the seeds via TLS, but the subsequent connections will connect without TLS if this value is null.
92+
So, if your nodes require TLS, this value must be an array, even if empty.
93+
https://github.com/phpredis/phpredis/blob/6.2.0/cluster.md#declaring-a-cluster-with-an-array-of-seeds
94+
-->
95+
<tls_options></tls_options> <!-- Optional: For example <tls_options><verify_peer>1</verify_peer></tls_options>, See https://www.php.net/manual/en/context.ssl.php -->
96+
<use_default_tls_options></use_default_tls_options> <!-- Optional: Always set tls_options to empty array if value is Truthy -->
97+
<sentinel_password></sentinel_password> <!-- Optional: Redis Sentinel password -->
98+
<cluster> <!-- Optional: Ignore <host/> and <port/> when set and change client to cluster mode -->
99+
<seeds></seeds> <!-- Optional: Comma separate host and port seed nodes, For example <seeds>tls://redis-1:6379,tls://redis-2:6379</seeds> -->
100+
<name></name> <!-- Optional: Name for cluster as read in redis.ini; See https://github.com/phpredis/phpredis/blob/6.2.0/cluster.md#loading-a-cluster-configuration-by-name -->
101+
<persistent></persistent> <!-- Optional: Should we use persistent connection? -->
102+
</cluster>
87103
</redis_session>
88104
...
89105
</global>

app/code/community/Cm/RedisSession/Model/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Cm_RedisSession_Model_Session implements SessionHandlerInterface
4848
public function __construct($config = array())
4949
{
5050
$this->sessionHandler = new \Cm\RedisSession\Handler(
51-
new Cm_RedisSession_Model_Session_Config($config['session_name'] ?? 'default'),
51+
Cm_RedisSession_Model_Session_Config::create($config['session_name'] ?? 'default'),
5252
new Cm_RedisSession_Model_Session_Logger(),
5353
Mage::registry('controller')
5454
&& Mage::app()->getFrontController()->getAction()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/*
3+
==New BSD License==
4+
5+
Copyright (c) 2013, Colin Mollenhour
6+
All rights reserved.
7+
8+
Redistribution and use in source and binary forms, with or without
9+
modification, are permitted provided that the following conditions are met:
10+
11+
* Redistributions of source code must retain the above copyright
12+
notice, this list of conditions and the following disclaimer.
13+
* Redistributions in binary form must reproduce the above copyright
14+
notice, this list of conditions and the following disclaimer in the
15+
documentation and/or other materials provided with the distribution.
16+
* The name of Colin Mollenhour may not be used to endorse or promote products
17+
derived from this software without specific prior written permission.
18+
* Redistributions in any form must not change the Cm_RedisSession namespace.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
24+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
class Cm_RedisSession_Model_Session_ClusterConfig extends Cm_RedisSession_Model_Session_Config implements \Cm\RedisSession\Handler\ClusterConfigInterface
33+
{
34+
35+
/**
36+
* @var Varien_Object
37+
*/
38+
protected $clusterConfig = null;
39+
40+
public function __construct(string $sessionName, Mage_Core_Model_Config_Element $clusterConfig)
41+
{
42+
parent::__construct($sessionName);
43+
44+
$seeds = preg_split('/\s*,\s*/', trim((string) $clusterConfig->descend('seeds')), -1, PREG_SPLIT_NO_EMPTY);
45+
$this->clusterConfig = new Varien_Object([
46+
'name' => $clusterConfig->descend('name') ? $clusterConfig->descend('name') : null,
47+
'seeds' => $seeds,
48+
'persistent' => (bool) $clusterConfig->descend('persistent'),
49+
]);
50+
}
51+
52+
/**
53+
* {@inheritDoc}
54+
*/
55+
public function isCluster(): bool
56+
{
57+
return true;
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function getClusterName(): ?string
64+
{
65+
return $this->clusterConfig->getData('name');
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function getClusterSeeds(): ?array
72+
{
73+
return $this->clusterConfig->getData('seeds');
74+
}
75+
76+
/**
77+
* {@inheritDoc}
78+
*/
79+
public function getClusterUsePersistentConnection(): bool
80+
{
81+
return $this->clusterConfig->getData('persistent');
82+
}
83+
}

app/code/community/Cm/RedisSession/Model/Session/Config.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
class Cm_RedisSession_Model_Session_Config implements \Cm\RedisSession\Handler\ConfigInterface
32+
class Cm_RedisSession_Model_Session_Config implements
33+
\Cm\RedisSession\Handler\ConfigInterface,
34+
\Cm\RedisSession\Handler\UsernameConfigInterface,
35+
\Cm\RedisSession\Handler\TlsOptionsConfigInterface,
36+
\Cm\RedisSession\Handler\ConfigSentinelPasswordInterface
3337
{
3438
/**
3539
* @var Varien_Object
@@ -64,9 +68,22 @@ public function __construct(string $sessionName)
6468
'sentinel_verify_master' => $config->is('sentinel_verify_master'),
6569
'sentinel_connect_retries' => (int) $config->descend('sentinel_connect_retries'),
6670
'break_after' => (int) $config->descend('break_after_' . $sessionName),
71+
'retries' => (int) $config->descend('retries'),
72+
'username' => $config->descend('username') ? (string) $config->descend('username') : null,
73+
'tls_options' => $config->descend('use_default_tls_options') ? [] : ($config->descend('tls_options') ? (array) $config->descend('tls_options') : null),
74+
'sentinel_password' => $config->descend('sentinel_password') ? (string) $config->descend('sentinel_password') : (string) $config->descend('password'),
6775
]);
6876
}
6977

78+
public static function create(string $sessionName)
79+
{
80+
$clusterConfig = Mage::getConfig()->getNode('global/redis_session/cluster') ?: new Mage_Core_Model_Config_Element('<root></root>');
81+
if ($clusterConfig->hasChildren()) {
82+
return new Cm_RedisSession_Model_Session_ClusterConfig($sessionName, $clusterConfig);
83+
}
84+
return new self($sessionName);
85+
}
86+
7087
/**
7188
* {@inheritDoc}
7289
*/
@@ -252,4 +269,36 @@ public function getSentinelConnectRetries()
252269
{
253270
return $this->config->getData('sentinel_connect_retries');
254271
}
272+
273+
/**
274+
* {@inheritDoc}
275+
*/
276+
public function getRetries()
277+
{
278+
return $this->config->getData('retries');
279+
}
280+
281+
/**
282+
* {@inheritDoc}
283+
*/
284+
public function getUsername(): ?string
285+
{
286+
return $this->config->getData('username');
287+
}
288+
289+
/**
290+
* {@inheritDoc}
291+
*/
292+
public function getTlsOptions(): ?array
293+
{
294+
return $this->config->getData('tls_options');
295+
}
296+
297+
/**
298+
* {@inheritDoc}
299+
*/
300+
public function getSentinelPassword(): ?string
301+
{
302+
return $this->config->getData('sentinel_password');
303+
}
255304
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"colinmollenhour/credis": "*",
14-
"colinmollenhour/php-redis-session-abstract": "~1.4"
14+
"colinmollenhour/php-redis-session-abstract": "~2.1"
1515
},
1616
"suggest":{
1717
"magento-hackathon/magento-composer-installer":"Makes it possible to manage this package as a dependency"

0 commit comments

Comments
 (0)