| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
15 |
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
|
16 |
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
|
|
|
17 |
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
|
|
|
18 |
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Yields the Session.
|
|
|
22 |
*
|
|
|
23 |
* @author Iltar van der Berg <kjarli@gmail.com>
|
|
|
24 |
*/
|
|
|
25 |
final class SessionValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface
|
|
|
26 |
{
|
|
|
27 |
/**
|
|
|
28 |
* @deprecated since Symfony 6.2, use resolve() instead
|
|
|
29 |
*/
|
|
|
30 |
public function supports(Request $request, ArgumentMetadata $argument): bool
|
|
|
31 |
{
|
|
|
32 |
@trigger_deprecation('symfony/http-kernel', '6.2', 'The "%s()" method is deprecated, use "resolve()" instead.', __METHOD__);
|
|
|
33 |
|
|
|
34 |
if (!$request->hasSession()) {
|
|
|
35 |
return false;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
$type = $argument->getType();
|
|
|
39 |
if (SessionInterface::class !== $type && !is_subclass_of($type, SessionInterface::class)) {
|
|
|
40 |
return false;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
return $request->getSession() instanceof $type;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function resolve(Request $request, ArgumentMetadata $argument): array
|
|
|
47 |
{
|
|
|
48 |
if (!$request->hasSession()) {
|
|
|
49 |
return [];
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
$type = $argument->getType();
|
|
|
53 |
if (SessionInterface::class !== $type && !is_subclass_of($type, SessionInterface::class)) {
|
|
|
54 |
return [];
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
return $request->getSession() instanceof $type ? [$request->getSession()] : [];
|
|
|
58 |
}
|
|
|
59 |
}
|