Skip to content

Commit 7230e37

Browse files
makraznicolas-grekas
authored andcommitted
[HttpFoundation] allow setting session options via DSN
1 parent 96b7965 commit 7230e37

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Deprecate passing `null` as `$requestIp` to `IpUtils::__checkIp()`, `IpUtils::__checkIp4()` or `IpUtils::__checkIp6()`, pass an empty string instead.
88
* Add the `litespeed_finish_request` method to work with Litespeed
99
* Deprecate `upload_progress.*` and `url_rewriter.tags` session options
10+
* Allow setting session options via DSN
1011

1112
5.3
1213
---

Session/Storage/Handler/MemcachedSessionHandler.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ class MemcachedSessionHandler extends AbstractSessionHandler
3838
*
3939
* List of available options:
4040
* * prefix: The prefix to use for the memcached keys in order to avoid collision
41-
* * expiretime: The time to live in seconds.
41+
* * ttl: The time to live in seconds.
4242
*
4343
* @throws \InvalidArgumentException When unsupported options are passed
4444
*/
4545
public function __construct(\Memcached $memcached, array $options = [])
4646
{
4747
$this->memcached = $memcached;
4848

49-
if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime'])) {
49+
if ($diff = array_diff(array_keys($options), ['prefix', 'expiretime', 'ttl'])) {
5050
throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode(', ', $diff)));
5151
}
5252

53-
$this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400;
53+
$this->ttl = $options['expiretime'] ?? $options['ttl'] ?? null;
5454
$this->prefix = $options['prefix'] ?? 'sf2s';
5555
}
5656

@@ -77,7 +77,7 @@ protected function doRead(string $sessionId)
7777
#[\ReturnTypeWillChange]
7878
public function updateTimestamp($sessionId, $data)
7979
{
80-
$this->memcached->touch($this->prefix.$sessionId, time() + $this->ttl);
80+
$this->memcached->touch($this->prefix.$sessionId, time() + (int) ($this->ttl ?? ini_get('session.gc_maxlifetime')));
8181

8282
return true;
8383
}
@@ -87,7 +87,7 @@ public function updateTimestamp($sessionId, $data)
8787
*/
8888
protected function doWrite(string $sessionId, string $data)
8989
{
90-
return $this->memcached->set($this->prefix.$sessionId, $data, time() + $this->ttl);
90+
return $this->memcached->set($this->prefix.$sessionId, $data, time() + (int) ($this->ttl ?? ini_get('session.gc_maxlifetime')));
9191
}
9292

9393
/**

Session/Storage/Handler/SessionHandlerFactory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static function createHandler($connection): AbstractSessionHandler
3030
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, get_debug_type($connection)));
3131
}
3232

33+
if ($options = parse_url($connection)) {
34+
parse_str($options['query'] ?? '', $options);
35+
}
36+
3337
switch (true) {
3438
case $connection instanceof \Redis:
3539
case $connection instanceof \RedisArray:
@@ -61,7 +65,7 @@ public static function createHandler($connection): AbstractSessionHandler
6165
$handlerClass = str_starts_with($connection, 'memcached:') ? MemcachedSessionHandler::class : RedisSessionHandler::class;
6266
$connection = AbstractAdapter::createConnection($connection, ['lazy' => true]);
6367

64-
return new $handlerClass($connection);
68+
return new $handlerClass($connection, array_intersect_key($options ?: [], ['prefix' => 1, 'ttl' => 1]));
6569

6670
case str_starts_with($connection, 'pdo_oci://'):
6771
if (!class_exists(DriverManager::class)) {
@@ -79,7 +83,7 @@ public static function createHandler($connection): AbstractSessionHandler
7983
case str_starts_with($connection, 'sqlsrv://'):
8084
case str_starts_with($connection, 'sqlite://'):
8185
case str_starts_with($connection, 'sqlite3://'):
82-
return new PdoSessionHandler($connection);
86+
return new PdoSessionHandler($connection, $options ?: []);
8387
}
8488

8589
throw new \InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection));

0 commit comments

Comments
 (0)