1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Controlabs\Routify; |
4
|
|
|
|
5
|
|
|
class Routify implements RoutifyInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The route groups. |
9
|
|
|
* |
10
|
|
|
* @var \Controlabs\Routify\RouteGroup |
11
|
|
|
*/ |
12
|
|
|
protected $routeGroup; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The route collection. |
16
|
|
|
* |
17
|
|
|
* @var \Controlabs\Routify\RouteCollection |
18
|
|
|
*/ |
19
|
|
|
protected $routeCollection; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
) { |
23
|
|
|
$this->routeCollection = new RouteCollection(); |
24
|
|
|
$this->routeGroup = new RouteGroup(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function group(string $group) |
31
|
|
|
{ |
32
|
|
|
$this->startGroup($group); |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function startGroup(string $group) |
40
|
|
|
{ |
41
|
|
|
$this->routeGroup->startGroup($group); |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function endGroup() |
49
|
|
|
{ |
50
|
|
|
$this->routeGroup->endGroup(); |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function routes() |
58
|
|
|
{ |
59
|
|
|
return $this->routeCollection; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public function delete(string $route, string $handler, string $action) |
66
|
|
|
{ |
67
|
|
|
$this->addRoute(Route::DELETE, $route, $handler, $action); |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
|
|
public function get(string $route, string $handler, string $action) |
75
|
|
|
{ |
76
|
|
|
$this->addRoute(Route::GET, $route, $handler, $action); |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function head(string $route, string $handler, string $action) |
84
|
|
|
{ |
85
|
|
|
$this->addRoute(Route::HEAD, $route, $handler, $action); |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
|
|
public function options(string $route, string $handler, string $action) |
93
|
|
|
{ |
94
|
|
|
$this->addRoute(Route::OPTIONS, $route, $handler, $action); |
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function patch(string $route, string $handler, string $action) |
102
|
|
|
{ |
103
|
|
|
$this->addRoute(Route::PATCH, $route, $handler, $action); |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
|
|
public function post(string $route, string $handler, string $action) |
111
|
|
|
{ |
112
|
|
|
$this->addRoute(Route::POST, $route, $handler, $action); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
*/ |
119
|
|
|
public function put(string $route, string $handler, string $action) |
120
|
|
|
{ |
121
|
|
|
$this->addRoute(Route::PUT, $route, $handler, $action); |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function addRoute(string $httpMethod, string $route, string $handler, string $action) |
126
|
|
|
{ |
127
|
|
|
$route = new Route($httpMethod, $this->formatRoute($route), $handler, $action); |
128
|
|
|
$this->routeCollection->add($route); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function formatRoute(string $route) |
132
|
|
|
{ |
133
|
|
|
return $this->routeGroup->path() . $route; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|