Skip to content

Commit

Permalink
adds session support
Browse files Browse the repository at this point in the history
  • Loading branch information
tenmajkl committed Aug 20, 2022
1 parent abb12c4 commit d8bba32
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"workerman/workerman": "^4.0",
"lemon_framework/lemon": "^3.3"
"lemon_framework/lemon": "^3.4"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions src/Lemon/Squeezer/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Lemon\Lemon\Squeezer;

use Lemon\Contracts\Http\Session as SessionContract;
use Workerman\Protocols\Http\Session as HttpSession;

class Session implements SessionContract
{
private HttpSession $session;

public function __construct($id)
{
$this->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();
}
}
14 changes: 10 additions & 4 deletions src/Lemon/Squeezer/Squeezer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])
Expand All @@ -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));
Expand Down

0 comments on commit d8bba32

Please sign in to comment.