Completed
Pull Request — master (#19)
by
unknown
19:27
created
Classes/ViewHelpers/RequestViewHelper.php 2 patches
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -19,134 +19,134 @@
 block discarded – undo
19 19
  */
20 20
 class RequestViewHelper extends AbstractViewHelper
21 21
 {
22
-    /**
23
-     * @Flow\Inject(lazy=false)
24
-     * @var Bootstrap
25
-     */
26
-    protected $bootstrap;
27
-
28
-    /**
29
-     * @Flow\Inject
30
-     * @var ContentDimensionPresetSourceInterface
31
-     */
32
-    protected $contentDimensionPresetSource;
33
-
34
-    /**
35
-     * @Flow\Inject
36
-     * @var ServerRequestFactory
37
-     */
38
-    protected $serverRequestFactory;
39
-
40
-    /**
41
-     * @Flow\InjectConfiguration(path="routing.supportEmptySegmentForDimensions", package="Neos.Neos")
42
-     * @var boolean
43
-     */
44
-    protected $supportEmptySegmentForDimensions;
45
-
46
-    /**
47
-     * @return void
48
-     * @throws ViewHelperException
49
-     * @api
50
-     */
51
-    public function initializeArguments()
52
-    {
53
-        parent::initializeArguments();
54
-        $this->registerArgument('path', 'string', 'Path', false, '404');
55
-    }
56
-
57
-    /**
58
-     * @return string
59
-     * @throws \RuntimeException
60
-     * @throws CurlEngineException
61
-     * @throws NoMatchingRouteException
62
-     */
63
-    public function render() : string
64
-    {
65
-        $path = $this->arguments['path'];
66
-        $this->appendFirstUriPartIfValidDimension($path);
67
-
68
-        $request = $this->bootstrap->getActiveRequestHandler()->getHttpRequest();
69
-        \assert($request instanceof ServerRequestInterface);
70
-
71
-        $userAgent = $request->getHeader('User-Agent');
72
-        if (isset($userAgent[0]) && strncmp($userAgent[0], 'Flow', 4) === 0) {
73
-            // To prevent a request loop, requests from Flow will be ignored.
74
-            return '';
75
-        }
76
-
77
-        $uri = RequestInformationHelper::generateBaseUri($request)->withPath($path);
78
-        // By default, the ServerRequestFactory sets the User-Agent header to "Flow/" followed by the version branch.
79
-        $serverRequest = $this->serverRequestFactory->createServerRequest('GET', $uri);
80
-        $response = (new CurlEngine())->sendRequest($serverRequest);
81
-
82
-        if ($response->getStatusCode() === 404) {
83
-            throw new NoMatchingRouteException(sprintf('Uri with path "%s" could not be found.', $uri), 1426446160);
84
-        }
85
-
86
-        return $response->getBody()->getContents();
87
-    }
88
-
89
-    /**
90
-     * @param string $path
91
-     * @return void
92
-     */
93
-    protected function appendFirstUriPartIfValidDimension(&$path)
94
-    {
95
-        $requestPath = ltrim($this->controllerContext->getRequest()->getHttpRequest()->getUri()->getPath(), '/');
96
-        $matches = [];
97
-        preg_match(FrontendNodeRoutePartHandler::DIMENSION_REQUEST_PATH_MATCHER, $requestPath, $matches);
98
-        if (!isset($matches['firstUriPart']) && !isset($matches['dimensionPresetUriSegments'])) {
99
-            return;
100
-        }
101
-
102
-        $dimensionPresets = $this->contentDimensionPresetSource->getAllPresets();
103
-        if (count($dimensionPresets) === 0) {
104
-            return;
105
-        }
106
-
107
-        $firstUriPartExploded = explode('_', $matches['firstUriPart'] ?: $matches['dimensionPresetUriSegments']);
108
-        if ($this->supportEmptySegmentForDimensions) {
109
-            foreach ($firstUriPartExploded as $uriSegment) {
110
-                $uriSegmentIsValid = false;
111
-                foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
112
-                    $preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment);
113
-                    if ($preset !== null) {
114
-                        $uriSegmentIsValid = true;
115
-                        break;
116
-                    }
117
-                }
118
-                if (!$uriSegmentIsValid) {
119
-                    return;
120
-                }
121
-            }
122
-        } else {
123
-            if (count($firstUriPartExploded) !== count($dimensionPresets)) {
124
-                $this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path);
125
-                return;
126
-            }
127
-            foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
128
-                $uriSegment = array_shift($firstUriPartExploded);
129
-                $preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment);
130
-                if ($preset === null) {
131
-                    $this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path);
132
-                    return;
133
-                }
134
-            }
135
-        }
136
-
137
-        $path = $matches['firstUriPart'] . '/' . $path;
138
-    }
139
-
140
-    /**
141
-     * @param array $dimensionPresets
142
-     * @param string $path
143
-     * @return void
144
-     */
145
-    protected function appendDefaultDimensionPresetUriSegments(array $dimensionPresets, &$path) {
146
-        $defaultDimensionPresetUriSegments = [];
147
-        foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
148
-            $defaultDimensionPresetUriSegments[] = $dimensionPreset['presets'][$dimensionPreset['defaultPreset']]['uriSegment'];
149
-        }
150
-        $path = implode('_', $defaultDimensionPresetUriSegments) . '/' . $path;
151
-    }
22
+	/**
23
+	 * @Flow\Inject(lazy=false)
24
+	 * @var Bootstrap
25
+	 */
26
+	protected $bootstrap;
27
+
28
+	/**
29
+	 * @Flow\Inject
30
+	 * @var ContentDimensionPresetSourceInterface
31
+	 */
32
+	protected $contentDimensionPresetSource;
33
+
34
+	/**
35
+	 * @Flow\Inject
36
+	 * @var ServerRequestFactory
37
+	 */
38
+	protected $serverRequestFactory;
39
+
40
+	/**
41
+	 * @Flow\InjectConfiguration(path="routing.supportEmptySegmentForDimensions", package="Neos.Neos")
42
+	 * @var boolean
43
+	 */
44
+	protected $supportEmptySegmentForDimensions;
45
+
46
+	/**
47
+	 * @return void
48
+	 * @throws ViewHelperException
49
+	 * @api
50
+	 */
51
+	public function initializeArguments()
52
+	{
53
+		parent::initializeArguments();
54
+		$this->registerArgument('path', 'string', 'Path', false, '404');
55
+	}
56
+
57
+	/**
58
+	 * @return string
59
+	 * @throws \RuntimeException
60
+	 * @throws CurlEngineException
61
+	 * @throws NoMatchingRouteException
62
+	 */
63
+	public function render() : string
64
+	{
65
+		$path = $this->arguments['path'];
66
+		$this->appendFirstUriPartIfValidDimension($path);
67
+
68
+		$request = $this->bootstrap->getActiveRequestHandler()->getHttpRequest();
69
+		\assert($request instanceof ServerRequestInterface);
70
+
71
+		$userAgent = $request->getHeader('User-Agent');
72
+		if (isset($userAgent[0]) && strncmp($userAgent[0], 'Flow', 4) === 0) {
73
+			// To prevent a request loop, requests from Flow will be ignored.
74
+			return '';
75
+		}
76
+
77
+		$uri = RequestInformationHelper::generateBaseUri($request)->withPath($path);
78
+		// By default, the ServerRequestFactory sets the User-Agent header to "Flow/" followed by the version branch.
79
+		$serverRequest = $this->serverRequestFactory->createServerRequest('GET', $uri);
80
+		$response = (new CurlEngine())->sendRequest($serverRequest);
81
+
82
+		if ($response->getStatusCode() === 404) {
83
+			throw new NoMatchingRouteException(sprintf('Uri with path "%s" could not be found.', $uri), 1426446160);
84
+		}
85
+
86
+		return $response->getBody()->getContents();
87
+	}
88
+
89
+	/**
90
+	 * @param string $path
91
+	 * @return void
92
+	 */
93
+	protected function appendFirstUriPartIfValidDimension(&$path)
94
+	{
95
+		$requestPath = ltrim($this->controllerContext->getRequest()->getHttpRequest()->getUri()->getPath(), '/');
96
+		$matches = [];
97
+		preg_match(FrontendNodeRoutePartHandler::DIMENSION_REQUEST_PATH_MATCHER, $requestPath, $matches);
98
+		if (!isset($matches['firstUriPart']) && !isset($matches['dimensionPresetUriSegments'])) {
99
+			return;
100
+		}
101
+
102
+		$dimensionPresets = $this->contentDimensionPresetSource->getAllPresets();
103
+		if (count($dimensionPresets) === 0) {
104
+			return;
105
+		}
106
+
107
+		$firstUriPartExploded = explode('_', $matches['firstUriPart'] ?: $matches['dimensionPresetUriSegments']);
108
+		if ($this->supportEmptySegmentForDimensions) {
109
+			foreach ($firstUriPartExploded as $uriSegment) {
110
+				$uriSegmentIsValid = false;
111
+				foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
112
+					$preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment);
113
+					if ($preset !== null) {
114
+						$uriSegmentIsValid = true;
115
+						break;
116
+					}
117
+				}
118
+				if (!$uriSegmentIsValid) {
119
+					return;
120
+				}
121
+			}
122
+		} else {
123
+			if (count($firstUriPartExploded) !== count($dimensionPresets)) {
124
+				$this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path);
125
+				return;
126
+			}
127
+			foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
128
+				$uriSegment = array_shift($firstUriPartExploded);
129
+				$preset = $this->contentDimensionPresetSource->findPresetByUriSegment($dimensionName, $uriSegment);
130
+				if ($preset === null) {
131
+					$this->appendDefaultDimensionPresetUriSegments($dimensionPresets, $path);
132
+					return;
133
+				}
134
+			}
135
+		}
136
+
137
+		$path = $matches['firstUriPart'] . '/' . $path;
138
+	}
139
+
140
+	/**
141
+	 * @param array $dimensionPresets
142
+	 * @param string $path
143
+	 * @return void
144
+	 */
145
+	protected function appendDefaultDimensionPresetUriSegments(array $dimensionPresets, &$path) {
146
+		$defaultDimensionPresetUriSegments = [];
147
+		foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
148
+			$defaultDimensionPresetUriSegments[] = $dimensionPreset['presets'][$dimensionPreset['defaultPreset']]['uriSegment'];
149
+		}
150
+		$path = implode('_', $defaultDimensionPresetUriSegments) . '/' . $path;
151
+	}
152 152
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
             }
135 135
         }
136 136
 
137
-        $path = $matches['firstUriPart'] . '/' . $path;
137
+        $path = $matches['firstUriPart'].'/'.$path;
138 138
     }
139 139
 
140 140
     /**
@@ -147,6 +147,6 @@  discard block
 block discarded – undo
147 147
         foreach ($dimensionPresets as $dimensionName => $dimensionPreset) {
148 148
             $defaultDimensionPresetUriSegments[] = $dimensionPreset['presets'][$dimensionPreset['defaultPreset']]['uriSegment'];
149 149
         }
150
-        $path = implode('_', $defaultDimensionPresetUriSegments) . '/' . $path;
150
+        $path = implode('_', $defaultDimensionPresetUriSegments).'/'.$path;
151 151
     }
152 152
 }
Please login to merge, or discard this patch.