-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionMiddleware.php
executable file
·40 lines (31 loc) · 1.09 KB
/
SessionMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Session;
use Koded\Stdlib\Interfaces\ConfigurationFactory;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
class SessionMiddleware implements MiddlewareInterface
{
public const SESSION_STARTED = 'sessionStarted';
public function __construct(ConfigurationFactory $settings)
{
session_start(session_register_custom_handler($settings)->sessionParameters());
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$request = $request->withAttribute(self::SESSION_STARTED, PHP_SESSION_ACTIVE === session_status());
$response = $handler->handle($request);
if (500 !== $response->getStatusCode()) {
session_write_close();
}
return $response;
}
}