diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2017-11-01 14:47:09 +0100 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2017-11-01 16:12:01 +0100 |
commit | ad948bdd3201e922b626a736b0122533bdd37cae (patch) | |
tree | 9011a160f3d6f9cae37fc02836e7d6e8c25242ca /src/Http | |
parent | e727b367cc77452b58bc2d9360bcde97b7572288 (diff) |
Added RequestServiceProvider and SessionServiceProvider
Diffstat (limited to 'src/Http')
-rw-r--r-- | src/Http/RequestServiceProvider.php | 14 | ||||
-rw-r--r-- | src/Http/SessionServiceProvider.php | 52 |
2 files changed, 66 insertions, 0 deletions
diff --git a/src/Http/RequestServiceProvider.php b/src/Http/RequestServiceProvider.php new file mode 100644 index 00000000..077e9ecc --- /dev/null +++ b/src/Http/RequestServiceProvider.php @@ -0,0 +1,14 @@ +<?php + +namespace Engelsystem\Http; + +use Engelsystem\Container\ServiceProvider; + +class RequestServiceProvider extends ServiceProvider +{ + public function register() + { + $request = $this->app->call([Request::class, 'createFromGlobals']); + $this->app->instance('request', $request); + } +} 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'; + } +} |