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