This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RedisSessionHandler added for Symfony Session
- Loading branch information
Showing
3 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...Adapters/Symfony/Component/HttpFoundation/Session/Storage/Handler/RedisSessionHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Selami\Factories\Adapters\Symfony\Component\HttpFoundation\Session\Storage\Handler; | ||
|
||
use Symfony\Component\HttpFoundation\Session\Storage\Handler; | ||
use Redis; | ||
|
||
class RedisSessionHandler implements \SessionHandlerInterface | ||
{ | ||
private $redis; | ||
private $ttl; | ||
private $prefix; | ||
|
||
public function __construct(Redis $redis, array $options = array()) | ||
{ | ||
$this->redis = $redis; | ||
if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'The following options are not supported "%s"', implode(', ', $diff) | ||
)); | ||
} | ||
$this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400; | ||
$this->prefix = $options['prefix'] ?? 'selami'; | ||
} | ||
|
||
public function open($savePath, $sessionName) : bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function close() : bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function read($sessionId) : string | ||
{ | ||
return $this->redis->get($this->prefix.$sessionId) ?: ''; | ||
} | ||
|
||
public function write($sessionId, $data) : bool | ||
{ | ||
return $this->redis->set($this->prefix.$sessionId, $data, time() + $this->ttl); | ||
} | ||
|
||
public function destroy($sessionId) | ||
{ | ||
return $this->redis->delete($this->prefix.$sessionId); | ||
} | ||
|
||
public function gc($maxlifetime) : bool | ||
{ | ||
// not required here because redis will auto expire the records anyhow. | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Selami\Factories; | ||
|
||
use Zend\ServiceManager\Factory\FactoryInterface; | ||
use Interop\Container\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Memcached; | ||
|
||
class SymfonySessionRedisFactory implements FactoryInterface | ||
{ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) : Session | ||
{ | ||
ini_set('session.use_cookies', '1'); | ||
ini_set('session.use_only_cookies', '1'); | ||
ini_set('session.cookie_httponly', '1'); | ||
ini_set('session.name', 'SELAMISESSID'); | ||
$storage = new NativeSessionStorage(array(), new MemcachedSessionHandler($container->get(Memcached::class))); | ||
return new Session($storage); | ||
} | ||
} |