SessionValueResolver.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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)
  26. {
  27. $type = $argument->getType();
  28. if (SessionInterface::class !== $type && !is_subclass_of($type, SessionInterface::class)) {
  29. return false;
  30. }
  31. return $request->getSession() instanceof $type;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function resolve(Request $request, ArgumentMetadata $argument)
  37. {
  38. yield $request->getSession();
  39. }
  40. }