|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Routing\Route\Matcher; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Http\HttpRequest; |
|
28
|
|
|
use Brickoo\Component\Routing\Route\HttpRoute; |
|
29
|
|
|
use Brickoo\Component\Routing\Route\Route; |
|
30
|
|
|
use Brickoo\Component\Routing\Route\RouteCollection; |
|
31
|
|
|
use Brickoo\Component\Routing\Route\RoutePathRegexGenerator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* HttpRouteMatcher |
|
35
|
|
|
* |
|
36
|
|
|
* Implementation of a http route matcher. |
|
37
|
|
|
* @author Celestino Diaz <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
class HttpRouteMatcher implements RouteMatcher { |
|
40
|
|
|
|
|
41
|
|
|
use CommonRouteMatcherStructure; |
|
42
|
|
|
|
|
43
|
|
|
/** @var \Brickoo\Component\Http\HttpRequest */ |
|
44
|
|
|
private $request; |
|
45
|
|
|
|
|
46
|
|
|
/** @var \Brickoo\Component\Routing\Route\RoutePathRegexGenerator */ |
|
47
|
|
|
private $regexGenerator; |
|
48
|
|
|
|
|
49
|
|
|
/** @var array */ |
|
50
|
|
|
private $routeParameters; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Class constructor |
|
54
|
|
|
* @param \Brickoo\Component\Http\HttpRequest $request |
|
55
|
|
|
* @param \Brickoo\Component\Routing\Route\RoutePathRegexGenerator $regexGenerator |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function __construct(HttpRequest $request, RoutePathRegexGenerator $regexGenerator) { |
|
58
|
1 |
|
$this->request = $request; |
|
59
|
1 |
|
$this->regexGenerator = $regexGenerator; |
|
60
|
1 |
|
$this->routeParameters = []; |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** {@inheritDoc} */ |
|
64
|
1 |
|
public function matchesCollection(RouteCollection $routeCollection) { |
|
65
|
1 |
|
return ((!$routeCollection->hasPath()) |
|
66
|
1 |
|
|| strpos($this->request->getUri()->getPath(), $routeCollection->getPath()) === 0); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** {@inheritDoc} */ |
|
70
|
2 |
|
public function matchesRoute(Route $route) { |
|
71
|
2 |
|
if ((!$route instanceof HttpRoute) || (!$this->isAllowedRoute($route))) { |
|
72
|
1 |
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$this->routeParameters = []; |
|
76
|
|
|
|
|
77
|
1 |
|
if (($doesMatch = $this->isMatchingRoute( |
|
78
|
1 |
|
$this->request->getUri()->getPath(), |
|
79
|
1 |
|
$this->regexGenerator->generate($route)))) { |
|
80
|
1 |
|
$this->routeParameters = $this->collectRouteParameters($route); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $doesMatch; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** {@inheritDoc} */ |
|
87
|
2 |
|
public function getRouteParameters() { |
|
88
|
2 |
|
return $this->routeParameters ?: []; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Checks if the Route is allowed to be executed. |
|
93
|
|
|
* @param \Brickoo\Component\Routing\Route\HttpRoute $route |
|
94
|
|
|
* @return boolean check result |
|
95
|
|
|
*/ |
|
96
|
2 |
|
private function isAllowedRoute(HttpRoute $route) { |
|
97
|
|
|
return ( |
|
98
|
2 |
|
$this->doesPropertyMatch($route->getMethod(), $this->request->getMethod()->toString()) |
|
99
|
2 |
|
&& $this->doesPropertyMatch($route->getHostname(), $this->request->getUri()->getHostname()) |
|
100
|
2 |
|
&& $this->doesPropertyMatch($route->getScheme(), $this->request->getUri()->getScheme()) |
|
101
|
2 |
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Check if the route property matches the request. |
|
106
|
|
|
* @param string $routeProperty |
|
107
|
|
|
* @param string $requestProperty |
|
108
|
|
|
* @return boolean check result |
|
109
|
|
|
*/ |
|
110
|
1 |
|
private function doesPropertyMatch($routeProperty, $requestProperty) { |
|
111
|
|
|
return $routeProperty === null |
|
112
|
1 |
|
|| (is_string($routeProperty) && preg_match("~^(".$routeProperty.")$~i", $requestProperty) == 1); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|