From 104e4f4c437376eb739dd3ef2de603855947a557 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 15 Sep 2018 17:24:59 +0200 Subject: Session: Added Symfony PDO backend --- src/Http/SessionServiceProvider.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src/Http') diff --git a/src/Http/SessionServiceProvider.php b/src/Http/SessionServiceProvider.php index 59121a3b..66ff18cc 100644 --- a/src/Http/SessionServiceProvider.php +++ b/src/Http/SessionServiceProvider.php @@ -2,8 +2,10 @@ namespace Engelsystem\Http; +use Engelsystem\Config\Config; use Engelsystem\Container\ServiceProvider; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; @@ -38,7 +40,35 @@ class SessionServiceProvider extends ServiceProvider return $this->app->make(MockArraySessionStorage::class); } - return $this->app->make(NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]); + /** @var Config $config */ + $config = $this->app->get('config'); + $sessionConfig = $config->get('session'); + + $handler = null; + $driver = $sessionConfig['driver']; + + switch ($driver) { + case 'pdo': + $handler = $this->app->make(PdoSessionHandler::class, [ + 'pdoOrDsn' => $this->app->get('db.pdo'), + 'options' => [ + 'db_table' => 'sessions', + 'db_id_col' => 'id', + 'db_data_col' => 'payload', + 'db_lifetime_col' => 'lifetime', + 'db_time_col' => 'last_activity', + ], + ]); + break; + } + + return $this->app->make(NativeSessionStorage::class, [ + 'options' => [ + 'cookie_httponly' => true, + 'name' => $sessionConfig['name'], + ], + 'handler' => $handler, + ]); } /** -- cgit v1.2.3-54-g00ecf