SessionValueResolver.php 1.8 KB

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