SessionValueResolver.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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\ValueResolverInterface;
  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 ValueResolverInterface
  21. {
  22. public function resolve(Request $request, ArgumentMetadata $argument): array
  23. {
  24. if (!$request->hasSession()) {
  25. return [];
  26. }
  27. $type = $argument->getType();
  28. if (SessionInterface::class !== $type && !is_subclass_of($type, SessionInterface::class)) {
  29. return [];
  30. }
  31. return $request->getSession() instanceof $type ? [$request->getSession()] : [];
  32. }
  33. }