1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Packages\Session; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Closure; |
22
|
|
|
use Limoncello\Application\Contracts\Session\SessionFunctionsInterface; |
23
|
|
|
use Limoncello\Application\Packages\Session\SessionSettings as C; |
24
|
|
|
use Limoncello\Contracts\Application\MiddlewareInterface; |
25
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
26
|
|
|
use Psr\Container\ContainerExceptionInterface; |
27
|
|
|
use Psr\Container\ContainerInterface; |
28
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
29
|
|
|
use Psr\Http\Message\ResponseInterface; |
30
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
31
|
|
|
use function call_user_func; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @package Limoncello\Application |
35
|
|
|
*/ |
36
|
|
|
class SessionMiddleware implements MiddlewareInterface |
37
|
|
|
{ |
38
|
|
|
/** Middleware handler */ |
39
|
|
|
const CALLABLE_HANDLER = [self::class, self::MIDDLEWARE_METHOD_NAME]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
* |
44
|
1 |
|
* @throws ContainerExceptionInterface |
45
|
|
|
* @throws NotFoundExceptionInterface |
46
|
|
|
*/ |
47
|
|
|
public static function handle( |
48
|
|
|
ServerRequestInterface $request, |
49
|
|
|
Closure $next, |
50
|
1 |
|
ContainerInterface $container |
51
|
|
|
): ResponseInterface { |
52
|
1 |
|
|
53
|
1 |
|
$sessionFunctions = static::getSessionFunctions($container); |
54
|
1 |
|
|
55
|
|
|
$couldBeStarted = call_user_func($sessionFunctions->getCouldBeStartedCallable()); |
56
|
|
|
if ($couldBeStarted === true) { |
57
|
1 |
|
call_user_func($sessionFunctions->getStartCallable(), static::getSessionSettings($container)); |
58
|
|
|
} |
59
|
1 |
|
|
60
|
|
|
$response = $next($request); |
61
|
1 |
|
|
62
|
|
|
call_user_func($sessionFunctions->getWriteCloseCallable()); |
63
|
|
|
|
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param ContainerInterface $container |
69
|
|
|
* |
70
|
|
|
* @return SessionFunctionsInterface |
71
|
|
|
* |
72
|
1 |
|
* @throws ContainerExceptionInterface |
73
|
|
|
* @throws NotFoundExceptionInterface |
74
|
|
|
*/ |
75
|
1 |
|
protected static function getSessionFunctions(ContainerInterface $container): SessionFunctionsInterface |
76
|
|
|
{ |
77
|
1 |
|
/** @var SessionFunctionsInterface $sessionFunctions */ |
78
|
|
|
$sessionFunctions = $container->get(SessionFunctionsInterface::class); |
79
|
|
|
|
80
|
|
|
return $sessionFunctions; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param ContainerInterface $container |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
* |
88
|
1 |
|
* @throws ContainerExceptionInterface |
89
|
|
|
* @throws NotFoundExceptionInterface |
90
|
|
|
*/ |
91
|
1 |
|
protected static function getSessionSettings(ContainerInterface $container): array |
92
|
1 |
|
{ |
93
|
|
|
/** @var SettingsProviderInterface $provider */ |
94
|
1 |
|
$provider = $container->get(SettingsProviderInterface::class); |
95
|
|
|
$settings = $provider->get(C::class); |
96
|
|
|
|
97
|
|
|
return $settings; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|