-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RouteParser.php
138 lines (118 loc) · 3.93 KB
/
RouteParser.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
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Routing;
use FastRoute\RouteParser\Std;
use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use Slim\Interfaces\RouteCollectorInterface;
use Slim\Interfaces\RouteParserInterface;
class RouteParser implements RouteParserInterface
{
/**
* @var RouteCollectorInterface
*/
private $routeCollector;
/**
* @var Std
*/
private $routeParser;
/**
* @param RouteCollectorInterface $routeCollector
*/
public function __construct(RouteCollectorInterface $routeCollector)
{
$this->routeCollector = $routeCollector;
$this->routeParser = new Std();
}
/**
* {@inheritdoc}
*/
public function relativeUrlFor(string $routeName, array $data = [], array $queryParams = []): string
{
$route = $this->routeCollector->getNamedRoute($routeName);
$pattern = $route->getPattern();
$segments = [];
$segmentName = '';
/*
* $routes is an associative array of expressions representing a route as multiple segments
* There is an expression for each optional parameter plus one without the optional parameters
* The most specific is last, hence why we reverse the array before iterating over it
*/
$expressions = array_reverse($this->routeParser->parse($pattern));
foreach ($expressions as $expression) {
foreach ($expression as $segment) {
/*
* Each $segment is either a string or an array of strings
* containing optional parameters of an expression
*/
if (is_string($segment)) {
$segments[] = $segment;
continue;
}
/*
* If we don't have a data element for this segment in the provided $data
* we cancel testing to move onto the next expression with a less specific item
*/
if (!array_key_exists($segment[0], $data)) {
$segments = [];
$segmentName = $segment[0];
break;
}
$segments[] = $data[$segment[0]];
}
/*
* If we get to this logic block we have found all the parameters
* for the provided $data which means we don't need to continue testing
* less specific expressions
*/
if (!empty($segments)) {
break;
}
}
if (empty($segments)) {
throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName);
}
$url = implode('', $segments);
if ($queryParams) {
$url .= '?' . http_build_query($queryParams);
}
return $url;
}
/**
* {@inheritdoc}
*/
public function urlFor(string $routeName, array $data = [], array $queryParams = []): string
{
$basePath = $this->routeCollector->getBasePath();
$url = $this->relativeUrlFor($routeName, $data, $queryParams);
if ($basePath) {
$url = $basePath . $url;
}
return $url;
}
/**
* {@inheritdoc}
*/
public function fullUrlFor(UriInterface $uri, string $routeName, array $data = [], array $queryParams = []): string
{
$path = $this->urlFor($routeName, $data, $queryParams);
$scheme = $uri->getScheme();
$authority = $uri->getAuthority();
$protocol = ($scheme ? $scheme . ':' : '') . ($authority ? '//' . $authority : '');
return $protocol . $path;
}
/**
* Get base path
*
* @return string
*/
public function getBasePath(): string
{
return $this->routeCollector->getBasePath();
}
}