61 'a' =>
'[0-9A-Za-z]++',
62 'h' =>
'[0-9A-Fa-f]++',
123 $name = strtolower($name);
130 'delete' =>
'DELETE',
131 'options' =>
'OPTIONS'
134 if (!isset($validTypes[$name])) {
143 call_user_func_array(
214 && !
$routes instanceof \Traversable
217 =
'Routes should be an array or an instance of Traversable';
218 if (!defined(
'HHVM_VERSION')) {
219 $msg = _(
'Routes should be an array or an instance of Traversable');
221 throw new \Exception(
$msg);
224 call_user_func_array(array($this,
'map'), $route);
263 $this->matchTypes = array_merge(
278 $this->defaultParams = array_merge(
279 $this->defaultParams,
295 $this->transformers[$matchType] = $transformer;
322 foreach (explode(
'|', $method) as $method) {
323 if (!isset($this->routes[$method])) {
324 $this->routes[$method] = array();
326 $this->routes[$method][] = array($route, $target, $name);
330 if (isset($this->namedRoutes[$name])) {
333 'Can not redeclare route',
336 if (!defined(
'HHVM_VERSION')) {
339 _(
'Can not redeclare route'),
343 throw new \Exception(
$msg);
345 $this->namedRoutes[$name] = $route;
363 array $params = array()
366 if (!isset($this->namedRoutes[$routeName])) {
367 throw new \Exception(
368 "Route '{$routeName}' does not exist."
372 $route = $this->namedRoutes[$routeName];
374 $url = $this->basePath . $route;
376 $params = array_merge(
377 $this->defaultParams,
380 $pattern =
'`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`';
381 if (preg_match_all($pattern, $route, $matches, PREG_SET_ORDER)) {
382 foreach ($matches as $index => $match) {
391 $block = substr($block, 1);
393 if (isset($this->transformers[$type])) {
394 $params[$param] = $this->transformers[$type]
395 ->toUrl($params[$param]);
397 if (isset($params[$param])) {
404 } elseif ($optional && $index !== 0) {
433 $requestMethod =
null
438 if (
null === $requestUrl) {
442 $requestUrl = substr(
444 strlen($this->basePath)
447 if (
false !== ($strpos = strpos($requestUrl,
'?'))) {
448 $requestUrl = substr($requestUrl, 0, $strpos);
451 if (
null === $requestMethod) {
454 if (empty($this->routes[$requestMethod])) {
457 foreach ($this->routes[$requestMethod] as $handler) {
464 if (
'*' === $route) {
467 } elseif (isset($route[0])
474 . ($this->ignoreCase ?
'i' :
null);
475 $match = (1 === preg_match($pattern, $requestUrl, $params));
476 } elseif (
false === ($position = strpos($route,
'['))) {
478 $match = 0 === strcmp($requestUrl, $route);
481 if (0 !== strncmp($requestUrl, $route, $position)) {
485 $match = (1 === preg_match($regex[
'regex'], $requestUrl, $params));
489 $routeisarr = is_array($route);
490 foreach ($params as $key => $value) {
491 if (is_numeric($key)) {
492 unset($params[$key]);
498 $type = $route[
'types'][$key];
499 if (isset($this->transformers[$type])) {
501 = $this->transformers[$type]->fromUrl($value);
515 $params[
'method'] = $requestMethod;
538 $pattern =
'`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`';
543 ($this->ignoreCase ?
'i' :
'')
547 if (preg_match_all($pattern, $route[
'regex'], $matches, PREG_SET_ORDER)) {
549 foreach ($matches as $match) {
558 $optional = (
'' !== $optional ?
'?' :
null);
559 $route[
'types'][$param] = $type;
568 . (
'' !== $pre ? $pre.
'+' :
null)
570 . (
'' !== $param ?
"?P<$param>" :
null)
576 $route[
'regex'] = str_replace($block, $pattern, $route[
'regex']);
588 return filter_input(INPUT_SERVER,
'REQUEST_URI');
597 return filter_input(INPUT_SERVER,
'REQUEST_METHOD');