|
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
|
|
|
use LogicException; |
|
15
|
|
|
|
|
16
|
|
|
class Router extends Container |
|
17
|
|
|
{ |
|
18
|
|
|
const MATCH_ANY = '.+'; |
|
19
|
|
|
const MATCH_NUM = '[[:digit:]]+'; |
|
20
|
|
|
const MATCH_ALNUM = '[[:alnum:]]+'; |
|
21
|
|
|
const MATCH_ALPHA = '[[:alpha:]]+'; |
|
22
|
|
|
const MATCH_SEGMENT = '[^/]*'; |
|
23
|
|
|
|
|
24
|
|
|
protected $filters = array(); |
|
25
|
|
|
|
|
26
|
|
|
protected $autoFilter = null; |
|
27
|
|
|
|
|
28
|
|
|
public function setAutoFilter($filter) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->autoFilter = $filter; |
|
31
|
|
|
|
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function filter($filter, Closure $callback) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->filters[$filter] = $callback; |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function applyFilters(Match $match, array $filters) |
|
43
|
|
|
{ |
|
44
|
|
|
foreach ($filters as $filter => $parameters) |
|
45
|
|
|
{ |
|
46
|
|
|
array_unshift($parameters, $match); |
|
47
|
|
|
$filter = $this->resolveFilter($filter); |
|
48
|
|
|
$result = call_user_func_array($filter, $parameters); |
|
49
|
|
|
|
|
50
|
|
|
if ($result === null or $result === true) |
|
51
|
|
|
{ |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($result === false) |
|
56
|
|
|
{ |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$match->response = $result; |
|
61
|
|
|
|
|
62
|
|
|
break; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $match; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function applyAutoFilter(Match $match) |
|
69
|
|
|
{ |
|
70
|
|
|
return call_user_func($this->autoFilter, $match); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function resolveFilter($name) |
|
74
|
|
|
{ |
|
75
|
|
|
if (isset($this->filters[$name])) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->filters[$name]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$method = 'filter'.ucfirst($name); |
|
81
|
|
|
|
|
82
|
|
|
if (method_exists($this, $method)) |
|
83
|
|
|
{ |
|
84
|
|
|
return array($this, $method); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
throw new LogicException('Could not find filter: '.$name); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function filterDomain(Match $match, $domain) |
|
91
|
|
|
{ |
|
92
|
|
|
return $_SERVER['SERVER_NAME'] === $domain; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function filterNamespace(Match $match, $namespace) |
|
96
|
|
|
{ |
|
97
|
|
|
$match->setNamespace($namespace); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function filterController(Match $match, $controller) |
|
101
|
|
|
{ |
|
102
|
|
|
$match->setController($controller); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function filterAction(Match $match, $action) |
|
106
|
|
|
{ |
|
107
|
|
|
$match->setAction($action); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function filterTo(Match $match, $to) |
|
111
|
|
|
{ |
|
112
|
|
|
list($controller, $action) = explode('@', $to); |
|
113
|
|
|
|
|
114
|
|
|
$match->setController($controller); |
|
115
|
|
|
$match->setAction($action); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function translate($uri, $method) |
|
119
|
|
|
{ |
|
120
|
|
|
$uri = trim($uri, '/'); |
|
121
|
|
|
|
|
122
|
|
|
foreach ($this->routes as $route) |
|
123
|
|
|
{ |
|
124
|
|
|
if ($match = $this->match($route, $uri, $method)) |
|
125
|
|
|
{ |
|
126
|
|
|
return $match; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return new Match(null, $method, $uri, $uri); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function match(Route $route, $uri, $method = '*') |
|
134
|
|
|
{ |
|
135
|
|
|
if ($match = $route->match($uri, $method)) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($route->filters) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
return $this->applyFilters($match, $route->filters); |
|
140
|
|
|
} |
|
141
|
|
|
elseif ($this->autoFilter) |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->applyAutoFilter($match); |
|
144
|
|
|
} |
|
145
|
|
|
return $match; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.