1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Fuel\Routing |
4
|
|
|
* @version 2.0 |
5
|
|
|
* @author Fuel Development Team |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2010 - 2015 Fuel Development Team |
8
|
|
|
* @link http://fuelphp.com |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fuel\Routing; |
12
|
|
|
|
13
|
|
|
use Closure; |
14
|
|
|
|
15
|
|
|
abstract class Container |
16
|
|
|
{ |
17
|
|
|
public $router; |
18
|
|
|
|
19
|
|
|
public $prefix = ''; |
20
|
|
|
|
21
|
|
|
public $routes = array(); |
22
|
|
|
|
23
|
|
|
public $types = array(); |
24
|
|
|
|
25
|
1 |
|
public function __construct($prefix = null) |
26
|
|
|
{ |
27
|
1 |
|
if ($prefix) |
28
|
|
|
{ |
29
|
|
|
$this->prefix = trim($prefix, '/').'/'; |
30
|
|
|
} |
31
|
1 |
|
} |
32
|
|
|
|
33
|
1 |
|
public function route($methods, $resource, $translation = null, $name = null) |
34
|
|
|
{ |
35
|
1 |
|
if ($resource) |
36
|
|
|
{ |
37
|
1 |
|
$resource = $this->prefix.trim($resource, '/'); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
if (is_string($methods)) |
41
|
|
|
{ |
42
|
|
|
$methods = explode('|', strtoupper($methods)); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$router = $this->router ?: $this; |
46
|
1 |
|
$route = new Route($router, $methods, $resource, $translation, $name); |
47
|
|
|
|
48
|
1 |
|
if ($name) |
49
|
|
|
{ |
50
|
1 |
|
$this->routes[$name] = $route; |
51
|
|
|
} |
52
|
|
|
else |
53
|
|
|
{ |
54
|
1 |
|
$this->routes[] = $route; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return $route; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function unregister(Route $route) |
61
|
|
|
{ |
62
|
|
|
$position = $route->name; |
63
|
|
|
|
64
|
|
|
if ( ! $position) |
65
|
|
|
{ |
66
|
|
|
$position = array_search($route, $this->routes, true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (isset($this->routes[$position])) |
70
|
|
|
{ |
71
|
|
|
unset($this->routes[$position]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function all($resource, $translation = null, $name = null) |
78
|
|
|
{ |
79
|
|
|
return $this->route(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], $resource, $translation, $name); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function get($resource, $translation = null, $name = null) |
83
|
|
|
{ |
84
|
1 |
|
return $this->route(['GET'], $resource, $translation, $name); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function post($resource, $translation = null, $name = null) |
88
|
|
|
{ |
89
|
|
|
return $this->route(['POST'], $resource, $translation, $name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function patch($resource, $translation = null, $name = null) |
93
|
|
|
{ |
94
|
|
|
return $this->route(['PATCH'], $resource, $translation, $name); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function put($resource, $translation = null, $name = null) |
98
|
|
|
{ |
99
|
|
|
return $this->route(['PUT'], $resource, $translation, $name); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function delete($resource, $translation = null, $name = null) |
103
|
|
|
{ |
104
|
|
|
return $this->route(['DELETE'], $resource, $translation, $name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getRoute($name) |
108
|
|
|
{ |
109
|
|
|
if (isset($this->routes[$name])) |
110
|
|
|
{ |
111
|
|
|
return $this->routes[$name]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function inject(Route $route) |
116
|
|
|
{ |
117
|
|
|
if ( ! $route->name) |
118
|
|
|
{ |
119
|
|
|
$this->routes[] = $route; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->routes[$route->name] = $route; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function provide(Container $container, array $filters = array()) |
130
|
|
|
{ |
131
|
|
|
foreach ($this->routes as $route) |
132
|
|
|
{ |
133
|
|
|
$route->filters += $filters; |
134
|
|
|
|
135
|
|
|
$container->inject($route); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function group(Closure $callback, array $filters = array()) |
140
|
|
|
{ |
141
|
|
|
// Create a new route collection with the correct parent. |
142
|
|
|
$group = new Group($this->router ?: $this); |
143
|
|
|
|
144
|
|
|
// Execute the callback, supplying the group |
145
|
|
|
call_user_func($callback, $group); |
146
|
|
|
|
147
|
|
|
// Provider the routes to it's parent. |
148
|
|
|
$group->provide($this, $filters); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function addCollection(Collection $collection) |
154
|
|
|
{ |
155
|
|
|
$collection->provide($this); |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setType($type, $regex, $optional = false) |
161
|
|
|
{ |
162
|
|
|
$this->types[$type] = compact('regex', 'optional'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getType($type) |
166
|
|
|
{ |
167
|
|
|
if ( ! isset($this->types[$type])) |
168
|
|
|
{ |
169
|
|
|
throw new \LogicException('Could not fetch undefined route param type: ['.$type.']'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->types[$type]; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|