/
StaticRouteResolver.php
174 lines (160 loc) · 5.94 KB
/
StaticRouteResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Frontend\Middleware;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException;
use TYPO3\CMS\Core\Routing\RouterInterface;
use TYPO3\CMS\Core\Site\Entity\Site;
/**
* Resolves static routes - can return configured content directly or load content from file / urls
*/
class StaticRouteResolver implements MiddlewareInterface
{
/**
* @var RequestFactory
*/
protected $requestFactory;
/**
* @var LinkService
*/
protected $linkService;
public function __construct(
RequestFactory $requestFactory,
LinkService $linkService
) {
$this->requestFactory = $requestFactory;
$this->linkService = $linkService;
}
/**
* Checks if there is a valid site with route configuration.
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (($site = $request->getAttribute('site', null)) instanceof Site &&
($configuration = $site->getConfiguration()['routes'] ?? null)
) {
$path = ltrim($request->getUri()->getPath(), '/');
$routeNames = array_map(function (string $route) use ($site) {
return ltrim(trim($site->getBase()->getPath(), '/') . '/' . ltrim($route, '/'), '/');
}, array_column($configuration, 'route'));
if (in_array($path, $routeNames, true)) {
$key = array_search($path, $routeNames, true);
$routeConfig = $configuration[$key];
try {
[$content, $contentType] = $this->resolveByType($request, $site, $routeConfig['type'], $routeConfig);
} catch (InvalidRouteArgumentsException $e) {
return new Response('Invalid route', 404, ['Content-Type' => 'text/plain']);
}
return new HtmlResponse($content, 200, ['Content-Type' => $contentType]);
}
}
return $handler->handle($request);
}
/**
* @param File $file
* @return array
*/
protected function getFromFile(File $file): array
{
$content = $file->getContents();
$contentType = $file->getMimeType();
return [$content, $contentType];
}
/**
* @param string $uri
* @return array
*/
protected function getFromUri(string $uri): array
{
$response = $this->requestFactory->request($uri);
$contentType = 'text/plain; charset=utf-8';
$content = '';
if ($response->getStatusCode() === 200) {
$content = $response->getBody()->getContents();
$contentType = $response->getHeader('Content-Type');
}
return [$content, $contentType];
}
/**
* @param ServerRequestInterface $request
* @param Site $site
* @param array $urlParams
* @return string
* @throws InvalidRouteArgumentsException
*/
protected function getPageUri(ServerRequestInterface $request, Site $site, array $urlParams): string
{
$parameters = [];
// Add additional parameters, if set via TypoLink
if (isset($urlParams['parameters'])) {
parse_str($urlParams['parameters'], $parameters);
}
$parameters['type'] = $urlParams['pagetype'] ?? 0;
$parameters['_language'] = $request->getAttribute('language', null);
$uri = $site->getRouter()->generateUri(
(int)$urlParams['pageuid'],
$parameters,
'',
RouterInterface::ABSOLUTE_URL
);
return (string)$uri;
}
/**
* @param ServerRequestInterface $request
* @param Site $site
* @param string $type
* @param array $routeConfig
* @return array
* @throws InvalidRouteArgumentsException
*/
protected function resolveByType(ServerRequestInterface $request, Site $site, string $type, array $routeConfig): array
{
switch ($type) {
case 'staticText':
$content = $routeConfig['content'];
$contentType = 'text/plain; charset=utf-8';
break;
case 'uri':
$urlParams = $this->linkService->resolve($routeConfig['source']);
if ($urlParams['type'] === 'url' || $urlParams['type'] === 'page') {
$uri = $urlParams['url'] ?? $this->getPageUri($request, $site, $urlParams);
[$content, $contentType] = $this->getFromUri($uri);
} elseif ($urlParams['type'] === 'file') {
[$content, $contentType] = $this->getFromFile($urlParams['file']);
} else {
throw new \InvalidArgumentException('Can only handle URIs of type page, url or file.', 1537348076);
}
break;
default:
throw new \InvalidArgumentException(
'Can only handle static file configurations with type uri or staticText.',
1537348083
);
}
return [$content, $contentType];
}
}