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