1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kambo\Router\Route; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A container for all defined routes. |
7
|
|
|
* |
8
|
|
|
* @author Bohuslav Simek <[email protected]> |
9
|
|
|
* @version GIT $Id$ |
10
|
|
|
* @license Apache-2.0 |
11
|
|
|
* @category Route |
12
|
|
|
* @package Router |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use Kambo\Router\Enum\Method; |
17
|
|
|
use Kambo\Router\Route\Route; |
18
|
|
|
|
19
|
|
|
class RouteCollection |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Contains all routes |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $_routes = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Add route matched with GET method. |
30
|
|
|
* Shortcut for createRoute function with preset GET method. |
31
|
|
|
* |
32
|
|
|
* @param mixed $route route definition |
33
|
|
|
* @param mixed $handler handler that will be executed if the url will match the route |
34
|
|
|
* |
35
|
|
|
* @return self for fluent interface |
36
|
|
|
*/ |
37
|
|
|
public function get($route, $handler) { |
38
|
|
|
$this->createRoute(Method::GET, $route, $handler); |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add route matched with POST method. |
45
|
|
|
* Shortcut for createRoute function with preset POST method. |
46
|
|
|
* |
47
|
|
|
* @param mixed $route route definition |
48
|
|
|
* @param mixed $handler handler that will be executed if the url will match the route |
49
|
|
|
* |
50
|
|
|
* @return self for fluent interface |
51
|
|
|
*/ |
52
|
|
|
public function post($route, $handler) { |
53
|
|
|
$this->createRoute(Method::POST, $route, $handler); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add route matched with DELETE method. |
60
|
|
|
* Shortcut for createRoute function with preset DELETE method. |
61
|
|
|
* |
62
|
|
|
* @param mixed $route route definition |
63
|
|
|
* @param mixed $handler handler that will be executed if the url will match the route |
64
|
|
|
* |
65
|
|
|
* @return self for fluent interface |
66
|
|
|
*/ |
67
|
|
|
public function delete($route, $handler) { |
68
|
|
|
$this->createRoute(Method::DELETE, $route, $handler); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add route matched with PUT method. |
75
|
|
|
* Shortcut for createRoute function with preset PUT method. |
76
|
|
|
* |
77
|
|
|
* @param mixed $route route definition |
78
|
|
|
* @param mixed $handler handler that will be executed if the url will match the route |
79
|
|
|
* |
80
|
|
|
* @return self for fluent interface |
81
|
|
|
*/ |
82
|
|
|
public function put($route, $handler) { |
83
|
|
|
$this->createRoute(Method::PUT, $route, $handler); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add route that will be matched to any method. |
90
|
|
|
* Shortcut for createRoute function with preset ANY method. |
91
|
|
|
* |
92
|
|
|
* @param mixed $route route definition |
93
|
|
|
* @param mixed $handler handler that will be executed if the url will match the route |
94
|
|
|
* |
95
|
|
|
* @return self for fluent interface |
96
|
|
|
*/ |
97
|
|
|
public function any($route, $handler) { |
98
|
|
|
$this->createRoute(Method::ANY, $route, $handler); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create a route to the collection. |
105
|
|
|
* The data structure used in the $handler depends on the used dispatcher. |
106
|
|
|
* |
107
|
|
|
* @param mixed $method HTTP method that will be used for binding |
108
|
|
|
* @param mixed $route route definition |
109
|
|
|
* @param mixed $handler handler that will be executed if the url will match the route |
110
|
|
|
* |
111
|
|
|
* @return self for fluent interface |
112
|
|
|
*/ |
113
|
|
|
public function createRoute($method, $route, $handler) { |
114
|
|
|
$this->_routes[] = new Route($method, $route, $handler); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Add a route to the collection. |
121
|
|
|
* |
122
|
|
|
* @param Kambo\Router\Route\Route $route route that will be added into collection |
123
|
|
|
* |
124
|
|
|
* @return self for fluent interface |
125
|
|
|
*/ |
126
|
|
|
public function addRoute(Route $route) { |
127
|
|
|
$this->_routes[] = $route; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get all defines routes in collection. |
134
|
|
|
* |
135
|
|
|
* @return Route[] |
136
|
|
|
*/ |
137
|
|
|
public function getRoutes() { |
138
|
|
|
return $this->_routes; |
139
|
|
|
} |
140
|
|
|
} |