2 declare(strict_types = 1);
19 use Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\MiddlewareInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
45 public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
47 if (($site = $request->getAttribute(
'site',
null)) instanceof
Site &&
48 ($configuration = $site->getConfiguration()[
'routes'] ??
null)
50 $path = ltrim($request->getUri()->getPath(),
'/');
52 if (is_array($routeConfig)) {
54 [$content, $contentType] = $this->
resolveByType($request, $site, $routeConfig[
'type'], $routeConfig);
56 return new Response(
'Invalid route', 404, [
'Content-Type' =>
'text/plain']);
59 return new HtmlResponse($content, 200, [
'Content-Type' => $contentType]);
62 return $handler->handle($request);
77 $routeNames = array_map(
function (?
string $route) use ($site) {
78 if ($route ===
null || $route ===
'') {
81 return ltrim(trim($site->
getBase()->getPath(),
'/') .
'/' . ltrim($route,
'/'),
'/');
82 }, array_column($staticRouteConfiguration,
'route'));
84 $routeNames = array_filter($routeNames);
86 if (in_array($uriPath, $routeNames,
true)) {
87 $key = array_search($uriPath, $routeNames,
true);
89 if (isset($staticRouteConfiguration[$key][
'type'])) {
90 return $staticRouteConfiguration[$key];
104 return [$content, $contentType];
113 $requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
114 $response = $requestFactory->request($uri);
115 $contentType =
'text/plain; charset=utf-8';
117 if ($response->getStatusCode() === 200) {
118 $content = $response->getBody()->getContents();
119 $contentType = $response->getHeader(
'Content-Type');
122 return [$content, $contentType];
132 protected function getPageUri(ServerRequestInterface $request,
Site $site, array $urlParams): string
136 if (isset($urlParams[
'parameters'])) {
137 parse_str($urlParams[
'parameters'], $parameters);
139 $parameters[
'type'] = $urlParams[
'pagetype'] ?? 0;
140 $parameters[
'_language'] = $request->getAttribute(
'language',
null);
142 (
int)$urlParams[
'pageuid'],
158 protected function resolveByType(ServerRequestInterface $request,
Site $site,
string $type, array $routeConfig): array
162 $content = $routeConfig[
'content'];
163 $contentType =
'text/plain; charset=utf-8';
166 $linkService = GeneralUtility::makeInstance(LinkService::class);
167 $urlParams = $linkService->resolve($routeConfig[
'source']);
168 if ($urlParams[
'type'] ===
'url' || $urlParams[
'type'] ===
'page') {
169 $uri = $urlParams[
'url'] ?? $this->
getPageUri($request, $site, $urlParams);
170 [$content, $contentType] = $this->
getFromUri($uri);
171 } elseif ($urlParams[
'type'] ===
'file') {
172 [$content, $contentType] = $this->
getFromFile($urlParams[
'file']);
174 throw new \InvalidArgumentException(
'Can only handle URIs of type page, url or file.', 1537348076);
179 throw new \InvalidArgumentException(
180 'Can only handle static file configurations with type uri or staticText.',
184 return [$content, $contentType];