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
|
|
|
use KM\Saffron\RouteCompiler; |
19
|
|
|
use KM\Saffron\RouteCompiled; |
20
|
|
|
|
21
|
|
|
class Route |
22
|
|
|
{ |
23
|
|
|
const DEFAULT_REQUIREMENT = '.+'; |
24
|
|
|
|
25
|
|
|
protected $name; |
26
|
|
|
protected $uri; |
27
|
|
|
protected $domain; |
28
|
|
|
protected $https; |
29
|
|
|
protected $method = []; |
30
|
|
|
protected $requirements = []; |
31
|
|
|
protected $defaults = []; |
32
|
|
|
protected $target = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $name |
36
|
|
|
*/ |
37
|
|
|
public function __construct($name) |
38
|
|
|
{ |
39
|
|
|
$this->name = $name; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getName() |
46
|
|
|
{ |
47
|
|
|
return $this->name; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $uri |
52
|
|
|
* @return Route |
53
|
|
|
*/ |
54
|
|
|
public function setUri($uri) |
55
|
|
|
{ |
56
|
|
|
$this->uri = $uri; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getUri() |
64
|
|
|
{ |
65
|
|
|
return $this->uri; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $domain |
70
|
|
|
* @return Route |
71
|
|
|
*/ |
72
|
|
|
public function setDomain($domain) |
73
|
|
|
{ |
74
|
|
|
$this->domain = $domain; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getDomain() |
82
|
|
|
{ |
83
|
|
|
return $this->domain; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasDomain() |
90
|
|
|
{ |
91
|
|
|
return !empty($this->domain); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array|string $method |
96
|
|
|
* @return Route |
97
|
|
|
*/ |
98
|
|
|
public function setMethod($method) |
99
|
|
|
{ |
100
|
|
|
$this->method = (array)$method; |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function getMethod() |
108
|
|
|
{ |
109
|
|
|
return $this->method; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function hasMethod() |
116
|
|
|
{ |
117
|
|
|
return !empty($this->method); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param mixed $https True - only https is allowed, false - only non-https is allowed, null - doesn't matter |
122
|
|
|
* @return Route |
123
|
|
|
*/ |
124
|
|
|
public function setHttps($https) |
125
|
|
|
{ |
126
|
|
|
$this->https = $https; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function getHttps() |
134
|
|
|
{ |
135
|
|
|
return $this->https; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function hasHttps() |
142
|
|
|
{ |
143
|
|
|
return null !== $this->https; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param array $requirements |
148
|
|
|
* @return Route |
149
|
|
|
*/ |
150
|
|
|
public function setRequirements(array $requirements) |
151
|
|
|
{ |
152
|
|
|
$this->requirements = $requirements; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $name |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function getRequirement($name) |
161
|
|
|
{ |
162
|
|
|
return isset($this->requirements[$name]) ? $this->requirements[$name] : self::DEFAULT_REQUIREMENT; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function getRequirements() |
169
|
|
|
{ |
170
|
|
|
return $this->requirements; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param array $defaults |
175
|
|
|
* @return Route |
176
|
|
|
*/ |
177
|
|
|
public function setDefaults(array $defaults) |
178
|
|
|
{ |
179
|
|
|
$this->defaults = $defaults; |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function getDefaults() |
187
|
|
|
{ |
188
|
|
|
return $this->defaults; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $name |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function hasDefault($name) |
196
|
|
|
{ |
197
|
|
|
return array_key_exists($name, $this->defaults); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
public function hasDefaults() |
204
|
|
|
{ |
205
|
|
|
return !empty($this->defaults); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param string $controller |
210
|
|
|
* @param string $method |
211
|
|
|
* @return Route |
212
|
|
|
*/ |
213
|
|
|
public function setTarget($controller, $method = 'indexAction') |
214
|
|
|
{ |
215
|
|
|
$this->target = [$controller, $method]; |
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
public function getTarget() |
223
|
|
|
{ |
224
|
|
|
return $this->target; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return RouteCompiler |
229
|
|
|
*/ |
230
|
|
|
protected function getCompiler() |
231
|
|
|
{ |
232
|
|
|
static $compiler; |
233
|
|
|
|
234
|
|
|
if (!$compiler) { |
235
|
|
|
$compiler = new RouteCompiler(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $compiler; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return RouteCompiled |
243
|
|
|
*/ |
244
|
|
|
public function getCompiled() |
245
|
|
|
{ |
246
|
|
|
return $this->getCompiler()->compile($this); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|