RequestStackContext.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Asset\Context;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. /**
  13. * Uses a RequestStack to populate the context.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class RequestStackContext implements ContextInterface
  18. {
  19. private $requestStack;
  20. private $basePath;
  21. private $secure;
  22. public function __construct(RequestStack $requestStack, string $basePath = '', bool $secure = false)
  23. {
  24. $this->requestStack = $requestStack;
  25. $this->basePath = $basePath;
  26. $this->secure = $secure;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getBasePath()
  32. {
  33. if (!$request = $this->requestStack->getMasterRequest()) {
  34. return $this->basePath;
  35. }
  36. return $request->getBasePath();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function isSecure()
  42. {
  43. if (!$request = $this->requestStack->getMasterRequest()) {
  44. return $this->secure;
  45. }
  46. return $request->isSecure();
  47. }
  48. }