summaryrefslogtreecommitdiff
path: root/src/Http/SessionServiceProvider.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-11-12 13:25:56 +0100
committerGitHub <noreply@github.com>2017-11-12 13:25:56 +0100
commitebc973bf221bfbde327868975221e62704e4cb99 (patch)
tree9011a160f3d6f9cae37fc02836e7d6e8c25242ca /src/Http/SessionServiceProvider.php
parent801c17aa6cef91be988a25a90442be8d2078a70d (diff)
parentad948bdd3201e922b626a736b0122533bdd37cae (diff)
Merge pull request #349 from MyIgel/master
Changed container to Illuminate/Container and added service providers
Diffstat (limited to 'src/Http/SessionServiceProvider.php')
-rw-r--r--src/Http/SessionServiceProvider.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/Http/SessionServiceProvider.php b/src/Http/SessionServiceProvider.php
new file mode 100644
index 00000000..55e3f48b
--- /dev/null
+++ b/src/Http/SessionServiceProvider.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Engelsystem\Http;
+
+use Engelsystem\Container\ServiceProvider;
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
+use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
+use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
+
+class SessionServiceProvider extends ServiceProvider
+{
+ public function register()
+ {
+ $sessionStorage = $this->getSessionStorage();
+ $this->app->instance('session.storage', $sessionStorage);
+ $this->app->bind(SessionStorageInterface::class, 'session.storage');
+
+ $session = $this->app->make(Session::class);
+ $this->app->instance('session', $session);
+
+ /** @var Request $request */
+ $request = $this->app->get('request');
+ $request->setSession($session);
+
+ $session->start();
+ }
+
+ /**
+ * Returns the session storage
+ *
+ * @return SessionStorageInterface
+ */
+ protected function getSessionStorage()
+ {
+ if ($this->isCli()) {
+ return $this->app->make(MockArraySessionStorage::class);
+ }
+
+ return $this->app->make(NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]);
+ }
+
+ /**
+ * Test if is called from cli
+ *
+ * @return bool
+ */
+ protected function isCli()
+ {
+ return PHP_SAPI == 'cli';
+ }
+}