@@ -24,157 +24,157 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class RequestViewHelper extends AbstractViewHelper |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * @Flow\Inject |
|
| 29 | - * @var Dispatcher |
|
| 30 | - */ |
|
| 31 | - protected $dispatcher; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @Flow\Inject(lazy=false) |
|
| 35 | - * @var Bootstrap |
|
| 36 | - */ |
|
| 37 | - protected $bootstrap; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @Flow\Inject(lazy=false) |
|
| 41 | - * @var Router |
|
| 42 | - */ |
|
| 43 | - protected $router; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @Flow\Inject(lazy=false) |
|
| 47 | - * @var SecurityContext |
|
| 48 | - */ |
|
| 49 | - protected $securityContext; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @Flow\Inject |
|
| 53 | - * @var ConfigurationManager |
|
| 54 | - */ |
|
| 55 | - protected $configurationManager; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @Flow\InjectConfiguration(path="routing.supportEmptySegmentForDimensions", package="Neos.Neos") |
|
| 59 | - * @var boolean |
|
| 60 | - */ |
|
| 61 | - protected $supportEmptySegmentForDimensions; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @Flow\Inject |
|
| 65 | - * @var ContentDimensionPresetSourceInterface |
|
| 66 | - */ |
|
| 67 | - protected $contentDimensionPresetSource; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Initialize this engine |
|
| 71 | - * |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - public function initializeObject() |
|
| 75 | - { |
|
| 76 | - $this->router->setRoutesConfiguration($this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_ROUTES)); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @param string $path |
|
| 81 | - * @return string |
|
| 82 | - * @throws \Exception |
|
| 83 | - */ |
|
| 84 | - public function render($path = null) |
|
| 85 | - { |
|
| 86 | - $this->appendFirstUriPartIfValidDimension($path); |
|
| 87 | - /** @var RequestHandler $activeRequestHandler */ |
|
| 88 | - $activeRequestHandler = $this->bootstrap->getActiveRequestHandler(); |
|
| 89 | - $parentHttpRequest = $activeRequestHandler->getHttpRequest(); |
|
| 90 | - $uri = rtrim($parentHttpRequest->getBaseUri(), '/') . '/' . $path; |
|
| 91 | - $httpRequest = Request::create(new Uri($uri)); |
|
| 92 | - $routeContext = new RouteContext($httpRequest, RouteParameters::createEmpty()); |
|
| 93 | - try { |
|
| 94 | - $matchingRoute = $this->router->route($routeContext); |
|
| 95 | - } catch (NoMatchingRouteException $exception) { |
|
| 96 | - $matchingRoute = null; |
|
| 97 | - } |
|
| 98 | - if (!$matchingRoute) { |
|
| 99 | - $exception = new \Exception(sprintf('Uri with path "%s" could not be found.', $uri), 1426446160); |
|
| 100 | - $exceptionHandler = set_exception_handler(null)[0]; |
|
| 101 | - $exceptionHandler->handleException($exception); |
|
| 102 | - exit(); |
|
| 103 | - } |
|
| 104 | - $request = new ActionRequest($parentHttpRequest); |
|
| 105 | - foreach ($matchingRoute as $argumentName => $argumentValue) { |
|
| 106 | - $request->setArgument($argumentName, $argumentValue); |
|
| 107 | - } |
|
| 108 | - $response = new Response($activeRequestHandler->getHttpResponse()); |
|
| 109 | - |
|
| 110 | - $this->securityContext->withoutAuthorizationChecks(function () use ($request, $response) { |
|
| 111 | - $this->dispatcher->dispatch($request, $response); |
|
| 112 | - }); |
|
| 113 | - |
|
| 114 | - return $response->getContent(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @param string $path |
|
| 119 | - * @return void |
|
| 120 | - */ |
|
| 121 | - protected function appendFirstUriPartIfValidDimension(&$path) |
|
| 122 | - { |
|
| 123 | - $requestPath = ltrim($this->controllerContext->getRequest()->getHttpRequest()->getUri()->getPath(), '/'); |
|
| 124 | - $matches = []; |
|
| 125 | - preg_match(FrontendNodeRoutePartHandler::DIMENSION_REQUEST_PATH_MATCHER, $requestPath, $matches); |
|
| 126 | - if (!isset($matches['firstUriPart']) && !isset($matches['dimensionPresetUriSegments'])) { |
|
| 127 | - return; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $dimensionPresets = $this->contentDimensionPresetSource->getAllPresets(); |
|
| 131 | - if (count($dimensionPresets) === 0) { |
|
| 132 | - return; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $firstUriPartExploded = explode('_', $matches['firstUriPart'] ?: $matches['dimensionPresetUriSegments']); |
|
| 136 | - if ($this->supportEmptySegmentForDimensions) { |
|
| 137 | - foreach ($firstUriPartExploded as $uriSegment) { |
|
| 138 | - $uriSegmentIsValid = false; |
|
| 139 | - foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
|
| 140 | - $preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment); |
|
| 141 | - if ($preset !== null) { |
|
| 142 | - $uriSegmentIsValid = true; |
|
| 143 | - break; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - if (!$uriSegmentIsValid) { |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - } else { |
|
| 151 | - if (count($firstUriPartExploded) !== count($dimensionPresets)) { |
|
| 152 | - $this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path); |
|
| 153 | - return; |
|
| 154 | - } |
|
| 155 | - foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
|
| 156 | - $uriSegment = array_shift($firstUriPartExploded); |
|
| 157 | - $preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment); |
|
| 158 | - if ($preset === null) { |
|
| 159 | - $this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path); |
|
| 160 | - return; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $path = $matches['firstUriPart'] . '/' . $path; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @param array $dimensionPresets |
|
| 170 | - * @param string $path |
|
| 171 | - * @return void |
|
| 172 | - */ |
|
| 173 | - protected function appendDefaultDimensionPresetUriSegments(array $dimensionPresets, &$path) { |
|
| 174 | - $defaultDimensionPresetUriSegments = []; |
|
| 175 | - foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
|
| 176 | - $defaultDimensionPresetUriSegments[] = $dimensionPreset['presets'][$dimensionPreset['defaultPreset']]['uriSegment']; |
|
| 177 | - } |
|
| 178 | - $path = implode('_', $defaultDimensionPresetUriSegments) . '/' . $path; |
|
| 179 | - } |
|
| 27 | + /** |
|
| 28 | + * @Flow\Inject |
|
| 29 | + * @var Dispatcher |
|
| 30 | + */ |
|
| 31 | + protected $dispatcher; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @Flow\Inject(lazy=false) |
|
| 35 | + * @var Bootstrap |
|
| 36 | + */ |
|
| 37 | + protected $bootstrap; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @Flow\Inject(lazy=false) |
|
| 41 | + * @var Router |
|
| 42 | + */ |
|
| 43 | + protected $router; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @Flow\Inject(lazy=false) |
|
| 47 | + * @var SecurityContext |
|
| 48 | + */ |
|
| 49 | + protected $securityContext; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @Flow\Inject |
|
| 53 | + * @var ConfigurationManager |
|
| 54 | + */ |
|
| 55 | + protected $configurationManager; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @Flow\InjectConfiguration(path="routing.supportEmptySegmentForDimensions", package="Neos.Neos") |
|
| 59 | + * @var boolean |
|
| 60 | + */ |
|
| 61 | + protected $supportEmptySegmentForDimensions; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @Flow\Inject |
|
| 65 | + * @var ContentDimensionPresetSourceInterface |
|
| 66 | + */ |
|
| 67 | + protected $contentDimensionPresetSource; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Initialize this engine |
|
| 71 | + * |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + public function initializeObject() |
|
| 75 | + { |
|
| 76 | + $this->router->setRoutesConfiguration($this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_ROUTES)); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @param string $path |
|
| 81 | + * @return string |
|
| 82 | + * @throws \Exception |
|
| 83 | + */ |
|
| 84 | + public function render($path = null) |
|
| 85 | + { |
|
| 86 | + $this->appendFirstUriPartIfValidDimension($path); |
|
| 87 | + /** @var RequestHandler $activeRequestHandler */ |
|
| 88 | + $activeRequestHandler = $this->bootstrap->getActiveRequestHandler(); |
|
| 89 | + $parentHttpRequest = $activeRequestHandler->getHttpRequest(); |
|
| 90 | + $uri = rtrim($parentHttpRequest->getBaseUri(), '/') . '/' . $path; |
|
| 91 | + $httpRequest = Request::create(new Uri($uri)); |
|
| 92 | + $routeContext = new RouteContext($httpRequest, RouteParameters::createEmpty()); |
|
| 93 | + try { |
|
| 94 | + $matchingRoute = $this->router->route($routeContext); |
|
| 95 | + } catch (NoMatchingRouteException $exception) { |
|
| 96 | + $matchingRoute = null; |
|
| 97 | + } |
|
| 98 | + if (!$matchingRoute) { |
|
| 99 | + $exception = new \Exception(sprintf('Uri with path "%s" could not be found.', $uri), 1426446160); |
|
| 100 | + $exceptionHandler = set_exception_handler(null)[0]; |
|
| 101 | + $exceptionHandler->handleException($exception); |
|
| 102 | + exit(); |
|
| 103 | + } |
|
| 104 | + $request = new ActionRequest($parentHttpRequest); |
|
| 105 | + foreach ($matchingRoute as $argumentName => $argumentValue) { |
|
| 106 | + $request->setArgument($argumentName, $argumentValue); |
|
| 107 | + } |
|
| 108 | + $response = new Response($activeRequestHandler->getHttpResponse()); |
|
| 109 | + |
|
| 110 | + $this->securityContext->withoutAuthorizationChecks(function () use ($request, $response) { |
|
| 111 | + $this->dispatcher->dispatch($request, $response); |
|
| 112 | + }); |
|
| 113 | + |
|
| 114 | + return $response->getContent(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @param string $path |
|
| 119 | + * @return void |
|
| 120 | + */ |
|
| 121 | + protected function appendFirstUriPartIfValidDimension(&$path) |
|
| 122 | + { |
|
| 123 | + $requestPath = ltrim($this->controllerContext->getRequest()->getHttpRequest()->getUri()->getPath(), '/'); |
|
| 124 | + $matches = []; |
|
| 125 | + preg_match(FrontendNodeRoutePartHandler::DIMENSION_REQUEST_PATH_MATCHER, $requestPath, $matches); |
|
| 126 | + if (!isset($matches['firstUriPart']) && !isset($matches['dimensionPresetUriSegments'])) { |
|
| 127 | + return; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $dimensionPresets = $this->contentDimensionPresetSource->getAllPresets(); |
|
| 131 | + if (count($dimensionPresets) === 0) { |
|
| 132 | + return; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $firstUriPartExploded = explode('_', $matches['firstUriPart'] ?: $matches['dimensionPresetUriSegments']); |
|
| 136 | + if ($this->supportEmptySegmentForDimensions) { |
|
| 137 | + foreach ($firstUriPartExploded as $uriSegment) { |
|
| 138 | + $uriSegmentIsValid = false; |
|
| 139 | + foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
|
| 140 | + $preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment); |
|
| 141 | + if ($preset !== null) { |
|
| 142 | + $uriSegmentIsValid = true; |
|
| 143 | + break; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + if (!$uriSegmentIsValid) { |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + } else { |
|
| 151 | + if (count($firstUriPartExploded) !== count($dimensionPresets)) { |
|
| 152 | + $this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path); |
|
| 153 | + return; |
|
| 154 | + } |
|
| 155 | + foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
|
| 156 | + $uriSegment = array_shift($firstUriPartExploded); |
|
| 157 | + $preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment); |
|
| 158 | + if ($preset === null) { |
|
| 159 | + $this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path); |
|
| 160 | + return; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $path = $matches['firstUriPart'] . '/' . $path; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @param array $dimensionPresets |
|
| 170 | + * @param string $path |
|
| 171 | + * @return void |
|
| 172 | + */ |
|
| 173 | + protected function appendDefaultDimensionPresetUriSegments(array $dimensionPresets, &$path) { |
|
| 174 | + $defaultDimensionPresetUriSegments = []; |
|
| 175 | + foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
|
| 176 | + $defaultDimensionPresetUriSegments[] = $dimensionPreset['presets'][$dimensionPreset['defaultPreset']]['uriSegment']; |
|
| 177 | + } |
|
| 178 | + $path = implode('_', $defaultDimensionPresetUriSegments) . '/' . $path; |
|
| 179 | + } |
|
| 180 | 180 | } |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | /** @var RequestHandler $activeRequestHandler */ |
| 88 | 88 | $activeRequestHandler = $this->bootstrap->getActiveRequestHandler(); |
| 89 | 89 | $parentHttpRequest = $activeRequestHandler->getHttpRequest(); |
| 90 | - $uri = rtrim($parentHttpRequest->getBaseUri(), '/') . '/' . $path; |
|
| 90 | + $uri = rtrim($parentHttpRequest->getBaseUri(), '/').'/'.$path; |
|
| 91 | 91 | $httpRequest = Request::create(new Uri($uri)); |
| 92 | 92 | $routeContext = new RouteContext($httpRequest, RouteParameters::createEmpty()); |
| 93 | 93 | try { |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | } |
| 108 | 108 | $response = new Response($activeRequestHandler->getHttpResponse()); |
| 109 | 109 | |
| 110 | - $this->securityContext->withoutAuthorizationChecks(function () use ($request, $response) { |
|
| 110 | + $this->securityContext->withoutAuthorizationChecks(function() use ($request, $response) { |
|
| 111 | 111 | $this->dispatcher->dispatch($request, $response); |
| 112 | 112 | }); |
| 113 | 113 | |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - $path = $matches['firstUriPart'] . '/' . $path; |
|
| 165 | + $path = $matches['firstUriPart'].'/'.$path; |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -175,6 +175,6 @@ discard block |
||
| 175 | 175 | foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { |
| 176 | 176 | $defaultDimensionPresetUriSegments[] = $dimensionPreset['presets'][$dimensionPreset['defaultPreset']]['uriSegment']; |
| 177 | 177 | } |
| 178 | - $path = implode('_', $defaultDimensionPresetUriSegments) . '/' . $path; |
|
| 178 | + $path = implode('_', $defaultDimensionPresetUriSegments).'/'.$path; |
|
| 179 | 179 | } |
| 180 | 180 | } |