1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Slim Framework (https://slimframework.com)
|
|
|
5 |
*
|
|
|
6 |
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
declare(strict_types=1);
|
|
|
10 |
|
|
|
11 |
namespace Slim\Routing;
|
|
|
12 |
|
|
|
13 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
14 |
use RuntimeException;
|
|
|
15 |
use Slim\Interfaces\RouteInterface;
|
|
|
16 |
use Slim\Interfaces\RouteParserInterface;
|
|
|
17 |
|
|
|
18 |
final class RouteContext
|
|
|
19 |
{
|
|
|
20 |
public const ROUTE = '__route__';
|
|
|
21 |
|
|
|
22 |
public const ROUTE_PARSER = '__routeParser__';
|
|
|
23 |
|
|
|
24 |
public const ROUTING_RESULTS = '__routingResults__';
|
|
|
25 |
|
|
|
26 |
public const BASE_PATH = '__basePath__';
|
|
|
27 |
|
|
|
28 |
public static function fromRequest(ServerRequestInterface $serverRequest): self
|
|
|
29 |
{
|
|
|
30 |
$route = $serverRequest->getAttribute(self::ROUTE);
|
|
|
31 |
$routeParser = $serverRequest->getAttribute(self::ROUTE_PARSER);
|
|
|
32 |
$routingResults = $serverRequest->getAttribute(self::ROUTING_RESULTS);
|
|
|
33 |
$basePath = $serverRequest->getAttribute(self::BASE_PATH);
|
|
|
34 |
|
|
|
35 |
if ($routeParser === null || $routingResults === null) {
|
|
|
36 |
throw new RuntimeException('Cannot create RouteContext before routing has been completed');
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/** @var RouteInterface|null $route */
|
|
|
40 |
/** @var RouteParserInterface $routeParser */
|
|
|
41 |
/** @var RoutingResults $routingResults */
|
|
|
42 |
/** @var string|null $basePath */
|
|
|
43 |
return new self($route, $routeParser, $routingResults, $basePath);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
private ?RouteInterface $route;
|
|
|
47 |
|
|
|
48 |
private RouteParserInterface $routeParser;
|
|
|
49 |
|
|
|
50 |
private RoutingResults $routingResults;
|
|
|
51 |
|
|
|
52 |
private ?string $basePath;
|
|
|
53 |
|
|
|
54 |
private function __construct(
|
|
|
55 |
?RouteInterface $route,
|
|
|
56 |
RouteParserInterface $routeParser,
|
|
|
57 |
RoutingResults $routingResults,
|
|
|
58 |
?string $basePath = null
|
|
|
59 |
) {
|
|
|
60 |
$this->route = $route;
|
|
|
61 |
$this->routeParser = $routeParser;
|
|
|
62 |
$this->routingResults = $routingResults;
|
|
|
63 |
$this->basePath = $basePath;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public function getRoute(): ?RouteInterface
|
|
|
67 |
{
|
|
|
68 |
return $this->route;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function getRouteParser(): RouteParserInterface
|
|
|
72 |
{
|
|
|
73 |
return $this->routeParser;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function getRoutingResults(): RoutingResults
|
|
|
77 |
{
|
|
|
78 |
return $this->routingResults;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function getBasePath(): string
|
|
|
82 |
{
|
|
|
83 |
if ($this->basePath === null) {
|
|
|
84 |
throw new RuntimeException('No base path defined.');
|
|
|
85 |
}
|
|
|
86 |
return $this->basePath;
|
|
|
87 |
}
|
|
|
88 |
}
|