1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2014 Krzysztof Magosa |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
* |
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
* See the License for the specific language governing permissions and |
14
|
|
|
* limitations under the License. |
15
|
|
|
*/ |
16
|
|
|
namespace KM\Saffron; |
17
|
|
|
|
18
|
|
|
class Router |
19
|
|
|
{ |
20
|
|
|
protected $factory; |
21
|
|
|
protected $urlMatcher; |
22
|
|
|
protected $urlBuilder; |
23
|
|
|
|
24
|
|
|
public function __construct(RouterFactory $factory) |
25
|
|
|
{ |
26
|
|
|
$this->factory = $factory; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return UrlMatcher\Base |
31
|
|
|
*/ |
32
|
|
|
protected function getUrlMatcher() |
33
|
|
|
{ |
34
|
|
|
if (!$this->urlMatcher) { |
35
|
|
|
$this->urlMatcher = $this->factory->getUrlMatcher(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->urlMatcher; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return UrlBuilder\Base |
43
|
|
|
*/ |
44
|
|
|
protected function getUrlBuilder() |
45
|
|
|
{ |
46
|
|
|
if (!$this->urlBuilder) { |
47
|
|
|
$this->urlBuilder = $this->factory->getUrlBuilder(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->urlBuilder; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Match request against routes. |
55
|
|
|
* Returns RoutingResult if request matches, null otherwise. |
56
|
|
|
* |
57
|
|
|
* @param Request $request |
58
|
|
|
* @return RoutingResult |
59
|
|
|
*/ |
60
|
|
|
public function match(Request $request) |
61
|
|
|
{ |
62
|
|
|
return $this->getUrlMatcher()->match($request); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Assembles links based on given name and parameters. |
67
|
|
|
* |
68
|
|
|
* @param string $name Name of route |
69
|
|
|
* @param array $parameters Parameters |
70
|
|
|
* @return string Built link |
71
|
|
|
*/ |
72
|
|
|
public function assemble($name, array $parameters = [], $fullUrl = false) |
73
|
|
|
{ |
74
|
|
|
return $this->getUrlBuilder()->assemble($name, $parameters, $fullUrl); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|