|
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
|
|
|
use Brickoo\Component\Common\Assert; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* BasicRouteMatcher |
|
36
|
|
|
* |
|
37
|
|
|
* Implementation of a basic route matcher. |
|
38
|
|
|
* Matches the route collection and route against a path. |
|
39
|
|
|
* @author Celestino Diaz <[email protected]> |
|
40
|
|
|
*/ |
|
41
|
|
|
class BasicRouteMatcher implements RouteMatcher { |
|
42
|
|
|
|
|
43
|
|
|
use CommonRouteMatcherStructure; |
|
44
|
|
|
|
|
45
|
|
|
/** @var string */ |
|
46
|
|
|
private $matchingPath; |
|
47
|
|
|
|
|
48
|
|
|
/** @var \Brickoo\Component\Routing\Route\RoutePathRegexGenerator */ |
|
49
|
|
|
private $regexGenerator; |
|
50
|
|
|
|
|
51
|
|
|
/** @var array */ |
|
52
|
|
|
private $routeParameters; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Class constructor |
|
56
|
|
|
* @param string $matchingPath |
|
57
|
|
|
* @param \Brickoo\Component\Routing\Route\RoutePathRegexGenerator $regexGenerator |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function __construct($matchingPath, RoutePathRegexGenerator $regexGenerator) { |
|
60
|
1 |
|
Assert::isString($matchingPath); |
|
61
|
1 |
|
$this->matchingPath = $matchingPath; |
|
62
|
1 |
|
$this->regexGenerator = $regexGenerator; |
|
63
|
1 |
|
$this->routeParameters = []; |
|
64
|
1 |
|
$this->pathParameters = []; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** {@inheritDoc} */ |
|
68
|
1 |
|
public function matchesCollection(RouteCollection $routeCollection) { |
|
69
|
1 |
|
return ((!$routeCollection->hasPath()) |
|
70
|
1 |
|
|| strpos($this->matchingPath, $routeCollection->getPath()) === 0); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** {@inheritDoc} */ |
|
74
|
1 |
|
public function matchesRoute(Route $route) { |
|
75
|
1 |
|
$this->routeParameters = []; |
|
76
|
|
|
|
|
77
|
1 |
|
if (($doesMatch = $this->isMatchingRoute( |
|
78
|
1 |
|
$this->matchingPath, |
|
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
|
|
|
|