vendor/sulu/sulu/src/Sulu/Component/Webspace/Analyzer/RequestAnalyzer.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Component\Webspace\Analyzer;
  11. use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
  12. use Sulu\Component\Webspace\Analyzer\Attributes\RequestProcessorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. /**
  16. * Default request analyzer will be used for sulu-admin and extended for sulu-website.
  17. */
  18. class RequestAnalyzer implements RequestAnalyzerInterface
  19. {
  20. public const SULU_ATTRIBUTE = '_sulu';
  21. /**
  22. * @param iterable<RequestProcessorInterface> $requestProcessors
  23. */
  24. public function __construct(
  25. private RequestStack $requestStack,
  26. private iterable $requestProcessors
  27. ) {
  28. }
  29. public function analyze(Request $request)
  30. {
  31. if ($request->attributes->has(static::SULU_ATTRIBUTE)) {
  32. return;
  33. }
  34. $attributes = new RequestAttributes(['scheme' => $request->getScheme(), 'requestUri' => $request->getRequestUri()]);
  35. foreach ($this->requestProcessors as $requestProcessor) {
  36. $attributes = $attributes->merge($requestProcessor->process($request, $attributes));
  37. }
  38. $request->attributes->set(static::SULU_ATTRIBUTE, $attributes);
  39. }
  40. public function validate(Request $request)
  41. {
  42. $attributes = $request->attributes->get(static::SULU_ATTRIBUTE);
  43. foreach ($this->requestProcessors as $provider) {
  44. $provider->validate($attributes);
  45. }
  46. }
  47. public function getAttribute($name, $default = null)
  48. {
  49. $requestAttributes = $this->getAttributes();
  50. if (!$requestAttributes) {
  51. return $default;
  52. }
  53. return $requestAttributes->getAttribute($name, $default);
  54. }
  55. private function getAttributes()
  56. {
  57. $request = $this->requestStack->getCurrentRequest();
  58. if (null === $request) {
  59. return null;
  60. }
  61. if (!$request->attributes->has(static::SULU_ATTRIBUTE)) {
  62. return null;
  63. }
  64. return $request->attributes->get(static::SULU_ATTRIBUTE);
  65. }
  66. private function setAttributes(RequestAttributes $attributes)
  67. {
  68. $request = $this->requestStack->getCurrentRequest();
  69. $request->attributes->set(static::SULU_ATTRIBUTE, $attributes);
  70. }
  71. public function getMatchType()
  72. {
  73. return $this->getAttribute('matchType');
  74. }
  75. public function getDateTime()
  76. {
  77. return $this->getAttribute('dateTime');
  78. }
  79. public function getWebspace()
  80. {
  81. return $this->getAttribute('webspace');
  82. }
  83. public function getPortal()
  84. {
  85. return $this->getAttribute('portal');
  86. }
  87. public function getSegment()
  88. {
  89. return $this->getAttribute('segment');
  90. }
  91. public function changeSegment(string $segmentKey)
  92. {
  93. $segment = $this->getWebspace()->getSegment($segmentKey);
  94. $requestAttributes = (new RequestAttributes(['segment' => $segment]))->merge($this->getAttributes());
  95. $this->setAttributes($requestAttributes);
  96. }
  97. public function getCurrentLocalization()
  98. {
  99. return $this->getAttribute('localization');
  100. }
  101. public function getPortalUrl()
  102. {
  103. return $this->getAttribute('portalUrl');
  104. }
  105. public function getRedirect()
  106. {
  107. return $this->getAttribute('redirect');
  108. }
  109. public function getResourceLocator()
  110. {
  111. return $this->getAttribute('resourceLocator', false);
  112. }
  113. public function getResourceLocatorPrefix()
  114. {
  115. return $this->getAttribute('resourceLocatorPrefix', '');
  116. }
  117. public function getPortalInformation()
  118. {
  119. return $this->getAttribute('portalInformation');
  120. }
  121. }