-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |