1
|
|
|
<?php namespace Limoncello\Core\Routing; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Core\Contracts\Routing\GroupInterface; |
20
|
|
|
use Limoncello\Core\Contracts\Routing\RouteInterface; |
21
|
|
|
use Limoncello\Core\Routing\Traits\CallableTrait; |
22
|
|
|
use Limoncello\Core\Routing\Traits\HasConfiguratorsTrait; |
23
|
|
|
use Limoncello\Core\Routing\Traits\HasMiddlewareTrait; |
24
|
|
|
use Limoncello\Core\Routing\Traits\HasRequestFactoryTrait; |
25
|
|
|
use Limoncello\Core\Routing\Traits\UriTrait; |
26
|
|
|
use LogicException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @package Limoncello\Core |
30
|
|
|
*/ |
31
|
|
|
class Route implements RouteInterface |
32
|
|
|
{ |
33
|
|
|
use CallableTrait, UriTrait, HasConfiguratorsTrait, HasMiddlewareTrait, HasRequestFactoryTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var GroupInterface |
37
|
|
|
*/ |
38
|
|
|
private $group; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $method; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $uriPath; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var callable |
52
|
|
|
*/ |
53
|
|
|
private $handler; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
private $isGroupRequestFactory = true; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string|null |
62
|
|
|
*/ |
63
|
|
|
private $name; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param GroupInterface $group |
67
|
|
|
* @param string $method |
68
|
|
|
* @param string $uriPath |
69
|
|
|
*/ |
70
|
17 |
|
public function __construct(GroupInterface $group, $method, $uriPath) |
71
|
|
|
{ |
72
|
17 |
|
$this->group = $group; |
73
|
17 |
|
$this->method = $method; |
74
|
17 |
|
$this->uriPath = $uriPath; |
75
|
17 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param callable $handler |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
14 |
|
public function setHandler(callable $handler) |
83
|
|
|
{ |
84
|
14 |
|
if ($this->isCallableToCache($handler) === false) { |
85
|
1 |
|
throw new LogicException($this->getCallableToCacheMessage()); |
86
|
|
|
} |
87
|
13 |
|
$this->handler = $handler; |
88
|
|
|
|
89
|
13 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string|null $name |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
13 |
|
public function setName($name) |
98
|
|
|
{ |
99
|
13 |
|
$this->name = $name; |
100
|
|
|
|
101
|
13 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
13 |
|
public function getGroup() |
108
|
|
|
{ |
109
|
13 |
|
return $this->group; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
12 |
|
public function getMethod() |
116
|
|
|
{ |
117
|
12 |
|
return $this->method; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
13 |
|
public function getUriPath() |
124
|
|
|
{ |
125
|
13 |
|
$uriPath = $this->concatUri($this->getGroup()->getUriPrefix(), $this->uriPath); |
126
|
|
|
|
127
|
13 |
|
return $this->normalizeUri($uriPath, $this->getGroup()->hasTrailSlash()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
12 |
|
public function getMiddleware() |
134
|
|
|
{ |
135
|
12 |
|
return array_merge($this->getGroup()->getMiddleware(), $this->middleware); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritdoc |
140
|
|
|
*/ |
141
|
12 |
|
public function getHandler() |
142
|
|
|
{ |
143
|
12 |
|
return $this->handler; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritdoc |
148
|
|
|
*/ |
149
|
12 |
|
public function getContainerConfigurators() |
150
|
|
|
{ |
151
|
12 |
|
return array_merge($this->getGroup()->getContainerConfigurators(), $this->configurators); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
12 |
|
public function getRequestFactory() |
158
|
|
|
{ |
159
|
12 |
|
if ($this->isUseGroupRequestFactory() === true) { |
160
|
12 |
|
return $this->getGroup()->getRequestFactory(); |
161
|
|
|
} |
162
|
|
|
|
163
|
10 |
|
return $this->isRequestFactorySet() === true ? $this->requestFactory : $this->getDefaultRequestFactory(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
12 |
|
public function isUseGroupRequestFactory() |
170
|
|
|
{ |
171
|
12 |
|
return $this->isGroupRequestFactory; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param bool $isGroupFactory |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
17 |
|
public function setUseGroupRequestFactory($isGroupFactory) |
180
|
|
|
{ |
181
|
17 |
|
$this->isGroupRequestFactory = $isGroupFactory; |
182
|
|
|
|
183
|
17 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritdoc |
188
|
|
|
*/ |
189
|
11 |
|
public function getName() |
190
|
|
|
{ |
191
|
11 |
|
$result = $this->name !== null ? $this->getGroup()->getName() . $this->name : null; |
192
|
|
|
|
193
|
11 |
|
return $result; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|