diff options
Diffstat (limited to 'src/Http')
-rw-r--r-- | src/Http/Request.php | 67 | ||||
-rw-r--r-- | src/Http/RequestServiceProvider.php | 14 | ||||
-rw-r--r-- | src/Http/SessionServiceProvider.php | 52 |
3 files changed, 133 insertions, 0 deletions
diff --git a/src/Http/Request.php b/src/Http/Request.php new file mode 100644 index 00000000..c6a9e5ad --- /dev/null +++ b/src/Http/Request.php @@ -0,0 +1,67 @@ +<?php + +namespace Engelsystem\Http; + +use Symfony\Component\HttpFoundation\Request as SymfonyRequest; + +class Request extends SymfonyRequest +{ + /** + * Get POST input + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function postData($key, $default = null) + { + return $this->request->get($key, $default); + } + + /** + * Get input data + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function input($key, $default = null) + { + return $this->get($key, $default); + } + + /** + * Checks if the input exists + * + * @param string $key + * @return bool + */ + public function has($key) + { + $value = $this->input($key); + + return !is_null($value); + } + + /** + * Get the requested path + * + * @return string + */ + public function path() + { + $pattern = trim($this->getPathInfo(), '/'); + + return $pattern == '' ? '/' : $pattern; + } + + /** + * Return the current URL + * + * @return string + */ + public function url() + { + return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/'); + } +} 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'; + } +} |