From d8bba32d851d9b2fb1504620c66dbcc9c40b48c7 Mon Sep 17 00:00:00 2001 From: TENMAJKL Date: Sat, 20 Aug 2022 13:49:48 +0200 Subject: [PATCH] adds session support --- composer.json | 2 +- composer.lock | 22 ++++++------ src/Lemon/Squeezer/Session.php | 59 +++++++++++++++++++++++++++++++++ src/Lemon/Squeezer/Squeezer.php | 14 +++++--- 4 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 src/Lemon/Squeezer/Session.php diff --git a/composer.json b/composer.json index ed839fa..eb8024b 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "workerman/workerman": "^4.0", - "lemon_framework/lemon": "^3.3" + "lemon_framework/lemon": "^3.4" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 735b5ac..524a88b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b618c3a8d43113bd34c407fa28fa72b7", + "content-hash": "fa8155aeea4ccc9fc6d9edb6e5d1840b", "packages": [ { "name": "lemon_framework/lemon", @@ -12,12 +12,12 @@ "source": { "type": "git", "url": "https://github.com/Lemon-Framework/Lemon.git", - "reference": "9c30024062f291414f9f635998e03f975d3a16d8" + "reference": "7c07aec955fd71d4187622f3723b4899fe379d6d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Lemon-Framework/Lemon/zipball/9c30024062f291414f9f635998e03f975d3a16d8", - "reference": "9c30024062f291414f9f635998e03f975d3a16d8", + "url": "https://api.github.com/repos/Lemon-Framework/Lemon/zipball/7c07aec955fd71d4187622f3723b4899fe379d6d", + "reference": "7c07aec955fd71d4187622f3723b4899fe379d6d", "shasum": "" }, "require": { @@ -56,7 +56,7 @@ "issues": "https://github.com/Lemon-Framework/Lemon/issues", "source": "https://github.com/Lemon-Framework/Lemon/tree/master" }, - "time": "2022-08-18T20:47:43+00:00" + "time": "2022-08-20T10:36:15+00:00" }, { "name": "psr/container", @@ -214,20 +214,20 @@ }, { "name": "workerman/workerman", - "version": "v4.0.42", + "version": "v4.1.0", "source": { "type": "git", "url": "https://github.com/walkor/workerman.git", - "reference": "83f09b50eaf7412504604030daa9e1f9c767e6c3" + "reference": "88ddf517e5c35bee072b2e453c4fcca0c6f1e59a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/walkor/workerman/zipball/83f09b50eaf7412504604030daa9e1f9c767e6c3", - "reference": "83f09b50eaf7412504604030daa9e1f9c767e6c3", + "url": "https://api.github.com/repos/walkor/workerman/zipball/88ddf517e5c35bee072b2e453c4fcca0c6f1e59a", + "reference": "88ddf517e5c35bee072b2e453c4fcca0c6f1e59a", "shasum": "" }, "require": { - "php": ">=5.4" + "php": ">=7.0" }, "suggest": { "ext-event": "For better performance. " @@ -273,7 +273,7 @@ "type": "patreon" } ], - "time": "2022-07-29T09:06:29+00:00" + "time": "2022-08-20T10:16:22+00:00" } ], "packages-dev": [], diff --git a/src/Lemon/Squeezer/Session.php b/src/Lemon/Squeezer/Session.php new file mode 100644 index 0000000..8b74f25 --- /dev/null +++ b/src/Lemon/Squeezer/Session.php @@ -0,0 +1,59 @@ +session = new HttpSession($id); + HttpSession::$httpOnly = true; + } + + /** + * Sets expiration. + */ + public function expireAt(int $seconds): static + { + HttpSession::$lifetime = $seconds; + return $this; + } + + /** + * Returns value of given key. + */ + public function get(string $key): string + { + return $this->session->get($key); + } + + /** + * Sets value for given key. + */ + public function set(string $key, mixed $value): static + { + $this->session->set($key, $value); + return $this; + } + + /** + * Determins whenever key exists. + */ + public function has(string $key): bool + { + return $this->session->has($key); + } + + /** + * Clears session. + */ + public function clear(): void + { + $this->session->flush(); + } +} diff --git a/src/Lemon/Squeezer/Squeezer.php b/src/Lemon/Squeezer/Squeezer.php index 643dfd2..f41eec2 100644 --- a/src/Lemon/Squeezer/Squeezer.php +++ b/src/Lemon/Squeezer/Squeezer.php @@ -5,6 +5,7 @@ namespace Lemon\Lemon\Squeezer; use Closure; +use Lemon\Contracts\Http\Session as SessionContract; use Lemon\Debug\Handling\Reporter; use Lemon\Http\Request as LemonRequest; use Lemon\Http\Response as LemonResponse; @@ -64,9 +65,9 @@ public function boot(string $host, string $port) $worker->run(); } - public function handleIncomming(ConnectionInterface $connection, Request $request) + public function handleIncomming(ConnectionInterface $connection, Request $worker_request) { - $path = $this->application->directory.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.$request->path(); + $path = $this->application->directory.DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.$worker_request->path(); if (file_exists($path) && !is_dir($path)) { $extension = explode('.', $path)[1]; $content_type = isset(self::CONTENT_TYPES[$extension]) @@ -80,11 +81,16 @@ public function handleIncomming(ConnectionInterface $connection, Request $reques return; } - $request = $this->captureRequest($request); + $request = $this->captureRequest($worker_request); $this->application->add(LemonRequest::class, $request); - if (!$this->application->has('request')) { + $this->application->add(Session::class, new Session($worker_request->sessionId())); + + if (!$this->application->hasAlias('request')) { $this->application->alias('request', LemonRequest::class); + $this->application->alias('session', Session::class); + $this->application->alias(SessionContract::class, Session::class); } + try { $response = $this->application->get('routing')->dispatch($request); $connection->send($this->convertResponse($response));