1
|
|
|
<?php namespace Limoncello\Application\Packages\Session; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [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\Contracts\Application\MiddlewareInterface; |
22
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
23
|
|
|
use Psr\Container\ContainerInterface; |
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
25
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
26
|
|
|
use Limoncello\Application\Packages\Session\SessionSettings as C; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @package Limoncello\Application |
30
|
|
|
*/ |
31
|
|
|
class SessionMiddleware implements MiddlewareInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @param ServerRequestInterface $request |
35
|
|
|
* @param Closure $next |
36
|
|
|
* @param ContainerInterface $container |
37
|
|
|
* |
38
|
|
|
* @return ResponseInterface |
39
|
|
|
*/ |
40
|
|
|
public static function handle( |
41
|
|
|
ServerRequestInterface $request, |
42
|
|
|
Closure $next, |
43
|
|
|
ContainerInterface $container |
44
|
|
|
): ResponseInterface { |
45
|
|
|
|
46
|
|
|
$sessionFunctions = static::getSessionFunction($container); |
47
|
|
|
|
48
|
|
|
call_user_func($sessionFunctions->getStartCallable(), static::getSessionSettings($container)); |
49
|
|
|
|
50
|
|
|
$response = $next($request); |
51
|
|
|
|
52
|
|
|
call_user_func($sessionFunctions->getWriteCloseCallable()); |
53
|
|
|
|
54
|
|
|
return $response; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ContainerInterface $container |
59
|
|
|
* |
60
|
|
|
* @return SessionFunctionsInterface |
61
|
|
|
*/ |
62
|
|
|
protected static function getSessionFunction(ContainerInterface $container): SessionFunctionsInterface |
63
|
|
|
{ |
64
|
|
|
/** @var SessionFunctionsInterface $sessionFunctions */ |
65
|
|
|
$sessionFunctions = $container->get(SessionFunctionsInterface::class); |
66
|
|
|
|
67
|
|
|
return $sessionFunctions; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ContainerInterface $container |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected static function getSessionSettings(ContainerInterface $container): array |
76
|
|
|
{ |
77
|
|
|
/** @var SettingsProviderInterface $provider */ |
78
|
|
|
$provider = $container->get(SettingsProviderInterface::class); |
79
|
|
|
$settings = $provider->get(C::class); |
80
|
|
|
|
81
|
|
|
return $settings; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|