1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Dash |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/DASPRiD/Dash For the canonical source repository |
6
|
|
|
* @copyright 2013-2015 Ben Scholzen 'DASPRiD' |
7
|
|
|
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Dash; |
11
|
|
|
|
12
|
|
|
use Dash\Exception\InvalidArgumentException; |
13
|
|
|
use Dash\Exception\RuntimeException; |
14
|
|
|
use Dash\Exception\UnexpectedValueException; |
15
|
|
|
use Dash\RouteCollection\RouteCollectionInterface; |
16
|
|
|
use Dash\RouteCollection\RouteCollectionMatcher; |
17
|
|
|
use Dash\RouterInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Psr\Http\Message\UriInterface; |
20
|
|
|
|
21
|
|
|
class Router implements RouterInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var RouteCollectionInterface |
25
|
|
|
*/ |
26
|
|
|
protected $routeCollection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $baseUri; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates a new router. |
35
|
|
|
* |
36
|
|
|
* @param RouteCollectionInterface $routeCollection |
37
|
|
|
* @param UriInterface|string $baseUri |
38
|
|
|
*/ |
39
|
|
|
public function __construct(RouteCollectionInterface $routeCollection, $baseUri) |
40
|
|
|
{ |
41
|
|
|
$this->routeCollection = $routeCollection; |
42
|
|
|
|
43
|
|
|
if ($baseUri instanceof UriInterface) { |
44
|
|
|
$this->setBaseUriFromObject($baseUri); |
45
|
|
|
} elseif (is_string($baseUri)) { |
46
|
|
|
$this->setBaseUriFromString($baseUri); |
47
|
|
|
} else { |
48
|
|
|
throw new InvalidArgumentException(sprintf( |
49
|
|
|
'Expected base URI of type string or %s, got %s', |
50
|
|
|
UriInterface::class, |
51
|
|
|
is_object($baseUri) ? get_class($baseUri) : gettype($baseUri) |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ('' === $this->baseUri['scheme'] || '' === $this->baseUri['host']) { |
56
|
|
|
throw new InvalidArgumentException(sprintf( |
57
|
|
|
'Base URI "%s" does not seem to be canonical', |
58
|
|
|
(string) $baseUri |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (null === $this->baseUri['port']) { |
63
|
|
|
$this->baseUri['port'] = ('http' === $this->baseUri['scheme'] ? 80 : 443); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* |
70
|
|
|
* @throws UnexpectedValueException |
71
|
|
|
*/ |
72
|
|
|
public function match(ServerRequestInterface $request) |
73
|
|
|
{ |
74
|
|
|
$basePathLength = strlen($this->baseUri['path']); |
75
|
|
|
|
76
|
|
|
return RouteCollectionMatcher::matchRouteCollection( |
77
|
|
|
$this->routeCollection, |
78
|
|
|
$request, |
79
|
|
|
$basePathLength, |
80
|
|
|
[] |
81
|
|
|
) ?: MatchResult::fromMatchFailure(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
* |
87
|
|
|
* @throws RuntimeException |
88
|
|
|
*/ |
89
|
|
|
public function assemble($routeName, array $params = [], array $options = []) |
90
|
|
|
{ |
91
|
|
|
$nameParts = explode('/', $routeName, 2); |
92
|
|
|
$parentName = $nameParts[0]; |
93
|
|
|
$childName = isset($nameParts[1]) ? $nameParts[1] : null; |
94
|
|
|
|
95
|
|
|
$assemblyResult = $this->routeCollection->get($parentName)->assemble($params, $childName); |
96
|
|
|
$assemblyResult->path = $this->baseUri['path'] . $assemblyResult->path; |
97
|
|
|
|
98
|
|
|
if (isset($options['query'])) { |
99
|
|
|
$assemblyResult->query = $options['query']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (isset($options['fragment'])) { |
103
|
|
|
$assemblyResult->fragment = $options['fragment']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $assemblyResult->generateUri( |
107
|
|
|
$this->baseUri['scheme'], |
108
|
|
|
$this->baseUri['host'], |
109
|
|
|
$this->baseUri['port'], |
110
|
|
|
(isset($options['enforce_absolute_uri']) && $options['enforce_absolute_uri']) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param UriInterface $baseUri |
116
|
|
|
*/ |
117
|
|
|
protected function setBaseUriFromObject(UriInterface $baseUri) |
118
|
|
|
{ |
119
|
|
|
$this->baseUri = [ |
120
|
|
|
'scheme' => $baseUri->getScheme(), |
121
|
|
|
'host' => $baseUri->getHost(), |
122
|
|
|
'port' => $baseUri->getPort(), |
123
|
|
|
'path' => rtrim($baseUri->getPath(), '/'), |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $baseUri |
129
|
|
|
* @throws InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
protected function setBaseUriFromString($baseUri) |
132
|
|
|
{ |
133
|
|
|
if (false === ($parts = parse_url($baseUri))) { |
134
|
|
|
throw new InvalidArgumentException(sprintf( |
135
|
|
|
'Base URI "%s" does not appear to be a valid URI', |
136
|
|
|
$baseUri |
137
|
|
|
)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->baseUri = [ |
141
|
|
|
'scheme' => (isset($parts['scheme']) ? $parts['scheme'] : ''), |
142
|
|
|
'host' => (isset($parts['host']) ? $parts['host'] : ''), |
143
|
|
|
'port' => (isset($parts['port']) ? $parts['port'] : null), |
144
|
|
|
'path' => (isset($parts['path']) ? rtrim($parts['path'], '/') : ''), |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|