ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Route.php
Go to the documentation of this file.
1 <?php
40 class Slim_Route {
41 
45  protected $pattern;
46 
50  protected $callable;
51 
55  protected $conditions = array();
56 
60  protected static $defaultConditions = array();
61 
65  protected $name;
66 
70  protected $params = array();
71 
75  protected $methods = array();
76 
80  protected $router;
81 
85  protected $middleware = array();
86 
92  public function __construct( $pattern, $callable ) {
93  $this->setPattern($pattern);
94  $this->setCallable($callable);
95  $this->setConditions(self::getDefaultConditions());
96  }
97 
103  public static function setDefaultConditions( array $defaultConditions ) {
104  self::$defaultConditions = $defaultConditions;
105  }
106 
111  public static function getDefaultConditions() {
112  return self::$defaultConditions;
113  }
114 
119  public function getPattern() {
120  return $this->pattern;
121  }
122 
128  public function setPattern( $pattern ) {
129  $this->pattern = str_replace(')', ')?', (string)$pattern);
130  }
131 
136  public function getCallable() {
137  return $this->callable;
138  }
139 
145  public function setCallable($callable) {
146  $this->callable = $callable;
147  }
148 
153  public function getConditions() {
154  return $this->conditions;
155  }
156 
162  public function setConditions( array $conditions ) {
163  $this->conditions = $conditions;
164  }
165 
170  public function getName() {
171  return $this->name;
172  }
173 
179  public function setName( $name ) {
180  $this->name = (string)$name;
181  $this->router->cacheNamedRoute($this->name, $this);
182  }
183 
188  public function getParams() {
189  return $this->params;
190  }
191 
196  public function setHttpMethods() {
197  $args = func_get_args();
198  $this->methods = $args;
199  }
200 
205  public function getHttpMethods() {
206  return $this->methods;
207  }
208 
213  public function appendHttpMethods() {
214  $args = func_get_args();
215  $this->methods = array_merge($this->methods, $args);
216  }
217 
222  public function via() {
223  $args = func_get_args();
224  $this->methods = array_merge($this->methods, $args);
225  return $this;
226  }
227 
232  public function supportsHttpMethod( $method ) {
233  return in_array($method, $this->methods);
234  }
235 
240  public function getRouter() {
241  return $this->router;
242  }
243 
249  public function setRouter( Slim_Router $router ) {
250  $this->router = $router;
251  }
252 
257  public function getMiddleware() {
258  return $this->middleware;
259  }
260 
276  public function setMiddleware( $middleware ) {
277  if ( is_callable($middleware) ) {
278  $this->middleware[] = $middleware;
279  } else if ( is_array($middleware) ) {
280  $this->middleware = array_merge($this->middleware, $middleware);
281  } else {
282  throw new InvalidArgumentException('Route middleware must be callable or an array of callables');
283  }
284  return $this;
285  }
286 
298  public function matches( $resourceUri ) {
299  //Extract URL params
300  preg_match_all('@:([\w]+)@', $this->pattern, $paramNames, PREG_PATTERN_ORDER);
301  $paramNames = $paramNames[0];
302 
303  //Convert URL params into regex patterns, construct a regex for this route
304  $patternAsRegex = preg_replace_callback('@:[\w]+@', array($this, 'convertPatternToRegex'), $this->pattern);
305  if ( substr($this->pattern, -1) === '/' ) {
306  $patternAsRegex = $patternAsRegex . '?';
307  }
308  $patternAsRegex = '@^' . $patternAsRegex . '$@';
309 
310  //Cache URL params' names and values if this route matches the current HTTP request
311  if ( preg_match($patternAsRegex, $resourceUri, $paramValues) ) {
312  array_shift($paramValues);
313  foreach ( $paramNames as $index => $value ) {
314  $val = substr($value, 1);
315  if ( isset($paramValues[$val]) ) {
316  $this->params[$val] = urldecode($paramValues[$val]);
317  }
318  }
319  return true;
320  } else {
321  return false;
322  }
323  }
324 
330  protected function convertPatternToRegex( $matches ) {
331  $key = str_replace(':', '', $matches[0]);
332  if ( array_key_exists($key, $this->conditions) ) {
333  return '(?P<' . $key . '>' . $this->conditions[$key] . ')';
334  } else {
335  return '(?P<' . $key . '>[a-zA-Z0-9_\-\.\!\~\*\\\'\(\)\:\@\&\=\$\+,%]+)';
336  }
337  }
338 
344  public function name( $name ) {
345  $this->setName($name);
346  return $this;
347  }
348 
354  public function conditions( array $conditions ) {
355  $this->conditions = array_merge($this->conditions, $conditions);
356  return $this;
357  }
358 
380  public function dispatch() {
381  if ( substr($this->pattern, -1) === '/' && substr($this->router->getRequest()->getResourceUri(), -1) !== '/' ) {
382  throw new Slim_Exception_RequestSlash();
383  }
384  //Invoke middleware
385  foreach ( $this->middleware as $mw ) {
386  if ( is_callable($mw) ) {
387  call_user_func($mw);
388  }
389  }
390  //Invoke callable
391  if ( is_callable($this->getCallable()) ) {
392  call_user_func_array($this->callable, array_values($this->params));
393  return true;
394  }
395  return false;
396  }
397 
398 }
setCallable($callable)
Set route callable.
Definition: Route.php:145
$conditions
Definition: Route.php:55
getMiddleware()
Get middleware.
Definition: Route.php:257
getPattern()
Get route pattern.
Definition: Route.php:119
conditions(array $conditions)
Merge route conditions.
Definition: Route.php:354
getConditions()
Get route conditions.
Definition: Route.php:153
setMiddleware( $middleware)
Set middleware.
Definition: Route.php:276
setPattern( $pattern)
Set route pattern.
Definition: Route.php:128
getCallable()
Get route callable.
Definition: Route.php:136
Add rich text string
The name of the decorator.
$middleware
Definition: Route.php:85
getParams()
Get route parameters.
Definition: Route.php:188
__construct( $pattern, $callable)
Constructor.
Definition: Route.php:92
via()
Append supported HTTP methods (alias for Route::appendHttpMethods)
Definition: Route.php:222
static getDefaultConditions()
Get default route conditions for all instances.
Definition: Route.php:111
setHttpMethods()
Add supported HTTP method(s)
Definition: Route.php:196
supportsHttpMethod( $method)
Detect support for an HTTP method.
Definition: Route.php:232
static $defaultConditions
Definition: Route.php:60
matches( $resourceUri)
Matches URI?
Definition: Route.php:298
getName()
Get route name.
Definition: Route.php:170
convertPatternToRegex( $matches)
Convert a URL parameter (ie.
Definition: Route.php:330
appendHttpMethods()
Append supported HTTP methods.
Definition: Route.php:213
Create styles array
The data for the language used.
setName( $name)
Set route name.
Definition: Route.php:179
getRouter()
Get router.
Definition: Route.php:240
getHttpMethods()
Get supported HTTP methods.
Definition: Route.php:205
name( $name)
Set route name.
Definition: Route.php:344
setConditions(array $conditions)
Set route conditions.
Definition: Route.php:162
setRouter(Slim_Router $router)
Set router.
Definition: Route.php:249
dispatch()
Dispatch route.
Definition: Route.php:380
static setDefaultConditions(array $defaultConditions)
Set default route conditions for all instances.
Definition: Route.php:103