SessionValueResolver.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\ControllerMetadata\ArgumentMetadata;
  15. /**
  16. * Yields the Session.
  17. *
  18. * @author Iltar van der Berg <kjarli@gmail.com>
  19. */
  20. final class SessionValueResolver implements ArgumentValueResolverInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function supports(Request $request, ArgumentMetadata $argument): bool
  26. {
  27. if (!$request->hasSession()) {
  28. return false;
  29. }
  30. $type = $argument->getType();
  31. if (SessionInterface::class !== $type && !is_subclass_of($type, SessionInterface::class)) {
  32. return false;
  33. }
  34. return $request->getSession() instanceof $type;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function resolve(Request $request, ArgumentMetadata $argument): iterable
  40. {
  41. yield $request->getSession();
  42. }
  43. }