|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Kambo\Router\Route; |
|
5
|
|
|
|
|
6
|
|
|
// spl |
|
7
|
|
|
use IteratorAggregate; |
|
8
|
|
|
use ArrayIterator; |
|
9
|
|
|
|
|
10
|
|
|
// Kambo\Router\Enum |
|
11
|
|
|
use Kambo\Router\Enum\Method; |
|
12
|
|
|
|
|
13
|
|
|
// Kambo\Router\Route |
|
14
|
|
|
use Kambo\Router\Route\Route; |
|
15
|
|
|
use Kambo\Router\Route\Builder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Collection of all defined routes. |
|
19
|
|
|
* |
|
20
|
|
|
* @package Kambo\Router\Route |
|
21
|
|
|
* @author Bohuslav Simek <[email protected]> |
|
22
|
|
|
* @license MIT |
|
23
|
|
|
*/ |
|
24
|
|
|
class Collection implements IteratorAggregate |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Contains all routes |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $routes = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Contains all routes |
|
35
|
|
|
* |
|
36
|
|
|
* @var \Kambo\Router\Route\Builder |
|
37
|
|
|
*/ |
|
38
|
|
|
private $routeBuilder; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Route constructor |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Kambo\Router\Route\Builder $routeBuilder Builder which will be used for creating |
|
44
|
|
|
* instance of the route object. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(Builder $routeBuilder) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->routeBuilder = $routeBuilder; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* IteratorAggregate: returns the iterator object. |
|
53
|
|
|
* |
|
54
|
|
|
* @return ArrayIterator |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getIterator() : ArrayIterator |
|
57
|
|
|
{ |
|
58
|
|
|
return new ArrayIterator($this->routes); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Add route matched with GET method. |
|
63
|
|
|
* Shortcut for createRoute function with preset GET method. |
|
64
|
|
|
* |
|
65
|
|
|
* @param mixed $route route definition |
|
66
|
|
|
* @param mixed $handler handler which will be executed if the url match |
|
67
|
|
|
* the route |
|
68
|
|
|
* |
|
69
|
|
|
* @return \Kambo\Router\Route\Route Created route |
|
70
|
|
|
*/ |
|
71
|
|
|
public function get(string $route, $handler) : Route |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->createRoute(Method::GET, $route, $handler); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Add route matched with POST method. |
|
78
|
|
|
* Shortcut for createRoute function with preset POST method. |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed $route route definition |
|
81
|
|
|
* @param mixed $handler handler which will be executed if the url match |
|
82
|
|
|
* the route |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Kambo\Router\Route\Route Created route |
|
85
|
|
|
*/ |
|
86
|
|
|
public function post(string $route, $handler) : Route |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->createRoute(Method::POST, $route, $handler); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Add route matched with DELETE method. |
|
93
|
|
|
* Shortcut for createRoute function with preset DELETE method. |
|
94
|
|
|
* |
|
95
|
|
|
* @param mixed $route route definition |
|
96
|
|
|
* @param mixed $handler handler which will be executed if the url match |
|
97
|
|
|
* the route |
|
98
|
|
|
* |
|
99
|
|
|
* @return \Kambo\Router\Route\Route Created route |
|
100
|
|
|
*/ |
|
101
|
|
|
public function delete(string $route, $handler) : Route |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->createRoute(Method::DELETE, $route, $handler); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Add route matched with PUT method. |
|
108
|
|
|
* Shortcut for createRoute function with preset PUT method. |
|
109
|
|
|
* |
|
110
|
|
|
* @param mixed $route route definition |
|
111
|
|
|
* @param mixed $handler handler which will be executed if the url match |
|
112
|
|
|
* the route |
|
113
|
|
|
* |
|
114
|
|
|
* @return \Kambo\Router\Route\Route Created route |
|
115
|
|
|
*/ |
|
116
|
|
|
public function put(string $route, $handler) : Route |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->createRoute(Method::PUT, $route, $handler); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Add route which will be matched to any method. |
|
123
|
|
|
* Shortcut for createRoute function with preset ANY method. |
|
124
|
|
|
* |
|
125
|
|
|
* @param mixed $route route definition |
|
126
|
|
|
* @param mixed $handler handler which will be executed if the url match |
|
127
|
|
|
* the route |
|
128
|
|
|
* |
|
129
|
|
|
* @return \Kambo\Router\Route\Route Created route |
|
130
|
|
|
*/ |
|
131
|
|
|
public function any(string $route, $handler) : Route |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->createRoute(Method::ANY, $route, $handler); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Create a route in the collection. |
|
138
|
|
|
* The data structure used in the $handler depends on the used dispatcher. |
|
139
|
|
|
* |
|
140
|
|
|
* @param mixed $method HTTP method which will be used for binding |
|
141
|
|
|
* @param mixed $route route definition |
|
142
|
|
|
* @param mixed $handler handler which will be executed if the |
|
143
|
|
|
* url matchs the route |
|
144
|
|
|
* |
|
145
|
|
|
* @return \Kambo\Router\Route\Route Created route |
|
146
|
|
|
*/ |
|
147
|
|
|
public function createRoute(string $method, string $route, $handler) : Route |
|
148
|
|
|
{ |
|
149
|
|
|
$createdRoute = $this->routeBuilder->build($method, $route, $handler); |
|
150
|
|
|
$this->routes[] = $createdRoute; |
|
151
|
|
|
|
|
152
|
|
|
return $createdRoute; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Add a route to the collection. |
|
157
|
|
|
* |
|
158
|
|
|
* @param Kambo\Router\Route\Route $route route which will be added into |
|
159
|
|
|
* collection |
|
160
|
|
|
* |
|
161
|
|
|
* @return self for fluent interface |
|
162
|
|
|
*/ |
|
163
|
|
|
public function addRoute(Route $route) : Collection |
|
164
|
|
|
{ |
|
165
|
|
|
$this->routes[] = $route; |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|