1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Packages\Cookies; |
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\Cookie\CookieFunctionsInterface; |
23
|
|
|
use Limoncello\Contracts\Application\MiddlewareInterface; |
24
|
|
|
use Limoncello\Contracts\Cookies\CookieInterface; |
25
|
|
|
use Limoncello\Contracts\Cookies\CookieJarInterface; |
26
|
|
|
use Psr\Container\ContainerInterface; |
27
|
|
|
use Psr\Http\Message\ResponseInterface; |
28
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
29
|
|
|
use function call_user_func; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @package Limoncello\Application |
33
|
|
|
*/ |
34
|
|
|
class CookieMiddleware implements MiddlewareInterface |
35
|
|
|
{ |
36
|
|
|
/** Middleware handler */ |
37
|
|
|
const CALLABLE_HANDLER = [self::class, self::MIDDLEWARE_METHOD_NAME]; |
38
|
|
|
|
39
|
1 |
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public static function handle( |
43
|
|
|
ServerRequestInterface $request, |
44
|
|
|
Closure $next, |
45
|
1 |
|
ContainerInterface $container |
46
|
|
|
): ResponseInterface { |
47
|
1 |
|
/** @var ResponseInterface $response */ |
48
|
|
|
$response = $next($request); |
49
|
1 |
|
|
50
|
|
|
if ($container->has(CookieJarInterface::class) === true) { |
51
|
1 |
|
/** @var CookieJarInterface $cookieJar */ |
52
|
1 |
|
$cookieJar = $container->get(CookieJarInterface::class); |
53
|
|
|
/** @var CookieFunctionsInterface $cookieFunctions */ |
54
|
|
|
$cookieFunctions = $container->get(CookieFunctionsInterface::class); |
55
|
|
|
foreach ($cookieJar->getAll() as $cookie) { |
56
|
|
|
/** @var CookieInterface $cookie */ |
57
|
|
|
|
58
|
|
|
// The reason why methods for setting cookies are called with call_user_func and their names |
59
|
|
|
// is that calling `setcookie` is not possible during testing. It can't add any headers |
60
|
|
|
// in a console app which produces some output. |
61
|
|
|
// |
62
|
1 |
|
// Using functions by names allows replace them with test mocks and check input values. |
63
|
1 |
|
// |
64
|
|
|
// Notice constants are used with `static::`. Their values can and will be replaced in tests. |
65
|
1 |
|
$setCookieFunction = $cookie->isNotRaw() === true ? |
66
|
1 |
|
$cookieFunctions->getWriteCookieCallable() : $cookieFunctions->getWriteRawCookieCallable(); |
67
|
1 |
|
|
68
|
1 |
|
call_user_func( |
69
|
1 |
|
$setCookieFunction, |
70
|
1 |
|
$cookie->getName(), |
71
|
1 |
|
$cookie->getValue(), |
72
|
1 |
|
$cookie->getExpiresAtUnixTime(), |
73
|
1 |
|
$cookie->getPath(), |
74
|
|
|
$cookie->getDomain(), |
75
|
|
|
$cookie->isSendOnlyOverSecureConnection(), |
76
|
|
|
$cookie->isAccessibleOnlyThroughHttp() |
77
|
|
|
); |
78
|
1 |
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $response; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|