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