Skip to content

Commit

Permalink
Pref: Session优化
Browse files Browse the repository at this point in the history
  • Loading branch information
bbuugg committed Aug 10, 2022
1 parent 1731318 commit 47574d9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 76 deletions.
6 changes: 1 addition & 5 deletions src/session/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
},
"require": {
"php": "^8.0",
"max/http-message": "dev-master",
"max/utils": "dev-master",
"max/redis": "dev-master"
},
"extra": {
"max": {
"config": "Max\\Session\\ConfigProvider"
}
}
}
28 changes: 0 additions & 28 deletions src/session/src/ConfigProvider.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/session/src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use SessionHandlerInterface;

use function ctype_alnum;
use function session_create_id;

class Session
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public function start(?string $id = null): void
if ($this->isStarted()) {
throw new SessionException('Cannot restart session.');
}
$this->id = ($id && $this->isValidId($id)) ? $id : \session_create_id();
$this->id = ($id && $this->isValidId($id)) ? $id : session_create_id();
if ($data = $this->sessionHandler->read($this->id)) {
$this->data = (array) (@\unserialize($data) ?: []);
}
Expand All @@ -68,7 +69,7 @@ public function save(): void
*/
public function regenerateId(): void
{
$this->id = \session_create_id();
$this->id = session_create_id();
}

/**
Expand Down
41 changes: 0 additions & 41 deletions src/session/src/SessionManager.php

This file was deleted.

66 changes: 66 additions & 0 deletions src/session/src/SessionMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/**
* This file is part of MaxPHP.
*
* @link https://github.com/marxphp
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Session;

use Max\Config\Contracts\ConfigInterface;
use Max\Http\Message\Cookie;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use SessionHandlerInterface;

class SessionMiddleware implements MiddlewareInterface
{
/**
* Cookie 过期时间【+9小时,实际1小时后过期,和时区有关】.
*/
protected int $expires = 9 * 3600;

protected string $name = 'MAXPHP_SESSION_ID';

protected bool $httponly = true;

protected string $path = '/';

protected string $domain = '';

protected bool $secure = true;

/**
* @var mixed|SessionHandlerInterface
*/
protected SessionHandlerInterface $handler;

public function __construct(ConfigInterface $config)
{
$config = $config->get('session');
$name = $config['default'];
$config = $config['stores'][$name];
$handler = $config['handler'];
$options = $config['options'];
$this->handler = new $handler($options);
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$session = new Session($this->handler);
$session->start($request->getCookieParams()[strtoupper($this->name)] ?? null);
$request = $request->withAttribute('Max\Session\Session', $session);
$response = $handler->handle($request);
$session->save();
$session->close();
$cookie = new Cookie($this->name, $session->getId(), time() + $this->expires, $this->path, $this->domain, $this->secure, $this->httponly);

return $response->withAddedHeader('Set-Cookie', $cookie->__toString());
}
}

0 comments on commit 47574d9

Please sign in to comment.