|
4 | 4 |
|
5 | 5 | namespace Flowpack\SeoRouting; |
6 | 6 |
|
| 7 | +use Flowpack\SeoRouting\Helper\BlocklistHelper; |
| 8 | +use Flowpack\SeoRouting\Helper\ConfigurationHelper; |
| 9 | +use Flowpack\SeoRouting\Helper\LowerCaseHelper; |
| 10 | +use Flowpack\SeoRouting\Helper\TrailingSlashHelper; |
7 | 11 | use Neos\Flow\Annotations as Flow; |
8 | 12 | use Psr\Http\Message\ResponseFactoryInterface; |
9 | 13 | use Psr\Http\Message\ResponseInterface; |
10 | 14 | use Psr\Http\Message\ServerRequestInterface; |
11 | | -use Psr\Http\Message\UriFactoryInterface; |
12 | | -use Psr\Http\Message\UriInterface; |
13 | 15 | use Psr\Http\Server\MiddlewareInterface; |
14 | 16 | use Psr\Http\Server\RequestHandlerInterface; |
15 | 17 |
|
16 | | -final class RoutingMiddleware implements MiddlewareInterface |
| 18 | +class RoutingMiddleware implements MiddlewareInterface |
17 | 19 | { |
18 | 20 | #[Flow\Inject] |
19 | 21 | protected ResponseFactoryInterface $responseFactory; |
20 | 22 |
|
21 | 23 | #[Flow\Inject] |
22 | | - protected UriFactoryInterface $uriFactory; |
| 24 | + protected ConfigurationHelper $configurationHelper; |
23 | 25 |
|
24 | | - /** @var array{enable: array{trailingSlash: bool, toLowerCase: bool}, statusCode?: int} */ |
25 | | - #[Flow\InjectConfiguration(path: 'redirect')] |
26 | | - protected array $configuration; |
| 26 | + #[Flow\Inject] |
| 27 | + protected BlocklistHelper $blocklistHelper; |
| 28 | + |
| 29 | + #[Flow\Inject] |
| 30 | + protected TrailingSlashHelper $trailingSlashHelper; |
27 | 31 |
|
28 | | - /** @var array{string: bool} */ |
29 | | - #[Flow\InjectConfiguration(path: 'blocklist')] |
30 | | - protected array $blocklist; |
| 32 | + #[Flow\Inject] |
| 33 | + protected LowerCaseHelper $lowerCaseHelper; |
31 | 34 |
|
32 | 35 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
33 | 36 | { |
34 | | - $isTrailingSlashEnabled = $this->configuration['enable']['trailingSlash'] ?? false; |
35 | | - $isToLowerCaseEnabled = $this->configuration['enable']['toLowerCase'] ?? false; |
| 37 | + $isTrailingSlashEnabled = $this->configurationHelper->isTrailingSlashEnabled(); |
| 38 | + $isToLowerCaseEnabled = $this->configurationHelper->isToLowerCaseEnabled(); |
36 | 39 |
|
37 | 40 | $uri = $request->getUri(); |
38 | 41 |
|
39 | 42 | if (! $isTrailingSlashEnabled && ! $isToLowerCaseEnabled) { |
40 | 43 | return $handler->handle($request); |
41 | 44 | } |
42 | 45 |
|
43 | | - if ($this->matchesBlocklist($uri)) { |
| 46 | + if ($this->blocklistHelper->isUriInBlocklist($uri)) { |
44 | 47 | return $handler->handle($request); |
45 | 48 | } |
46 | 49 |
|
47 | 50 | $oldPath = $uri->getPath(); |
48 | 51 |
|
49 | 52 | if ($isTrailingSlashEnabled) { |
50 | | - $uri = $this->handleTrailingSlash($uri); |
| 53 | + $uri = $this->trailingSlashHelper->appendTrailingSlash($uri); |
51 | 54 | } |
52 | 55 |
|
53 | 56 | if ($isToLowerCaseEnabled) { |
54 | | - $uri = $this->handleToLowerCase($uri); |
| 57 | + $uri = $this->lowerCaseHelper->convertPathToLowerCase($uri); |
55 | 58 | } |
56 | 59 |
|
57 | 60 | if ($uri->getPath() === $oldPath) { |
58 | 61 | return $handler->handle($request); |
59 | 62 | } |
60 | 63 |
|
61 | | - $response = $this->responseFactory->createResponse($this->configuration['statusCode'] ?? 301); |
| 64 | + $response = $this->responseFactory->createResponse($this->configurationHelper->getStatusCode()); |
62 | 65 |
|
63 | 66 | return $response->withAddedHeader('Location', (string)$uri); |
64 | 67 | } |
65 | | - |
66 | | - private function handleTrailingSlash(UriInterface $uri): UriInterface |
67 | | - { |
68 | | - if (strlen($uri->getPath()) === 0) { |
69 | | - return $uri; |
70 | | - } |
71 | | - |
72 | | - if (array_key_exists('extension', pathinfo($uri->getPath()))) { |
73 | | - return $uri; |
74 | | - } |
75 | | - |
76 | | - return $uri->withPath(rtrim($uri->getPath(), '/') . '/') |
77 | | - ->withQuery($uri->getQuery()) |
78 | | - ->withFragment($uri->getFragment()); |
79 | | - } |
80 | | - |
81 | | - private function handleToLowerCase(UriInterface $uri): UriInterface |
82 | | - { |
83 | | - $loweredPath = strtolower($uri->getPath()); |
84 | | - |
85 | | - if ($uri->getPath() === $loweredPath) { |
86 | | - return $uri; |
87 | | - } |
88 | | - |
89 | | - $newUri = str_replace($uri->getPath(), $loweredPath, (string)$uri); |
90 | | - |
91 | | - return $this->uriFactory->createUri($newUri); |
92 | | - } |
93 | | - |
94 | | - private function matchesBlocklist(UriInterface $uri): bool |
95 | | - { |
96 | | - $path = $uri->getPath(); |
97 | | - foreach ($this->blocklist as $rawPattern => $active) { |
98 | | - $pattern = '/' . str_replace('/', '\/', $rawPattern) . '/'; |
99 | | - |
100 | | - if (! $active) { |
101 | | - continue; |
102 | | - } |
103 | | - |
104 | | - if (preg_match($pattern, $path) === 1) { |
105 | | - return true; |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - return false; |
110 | | - } |
111 | 68 | } |
0 commit comments