Advertisement
stuppid_bot

Untitled

Jun 5th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Core;
  4.  
  5. class Router {
  6.     protected $routes = array();
  7.  
  8.     public function __construct(array $routes = null) {
  9.         if (is_array($routes)) {
  10.             $this->setRoutes($routes);
  11.         }
  12.     }
  13.  
  14.     public function setRoutes($routes) {
  15.         foreach ($routes as $k => $v) {
  16.             $route = $k;
  17.             $controller = $v['controller'];
  18.             $action = isset($v['action']) ? $v['action'] : null;
  19.             $this->addRoute($route, $controller, $action);
  20.         }
  21.     }
  22.  
  23.     public function addRoute($route, $controller, $action = null) {
  24.         list($method, $route) = $this->parseRoute($route);
  25.         $pattern = $this->compileRoute($route);
  26.         $this->routes[$pattern] = array($method, $controller, $action);
  27.     }
  28.  
  29.     protected function parseRoute($route) {
  30.         if ($route[0] == '/') {
  31.             $method = null;
  32.         }
  33.         else {
  34.             $parts = explode(' ',$route);
  35.             $method = $parts[0];
  36.             $route = $parts[1];
  37.         }
  38.         return array($method, $route);
  39.     }
  40.  
  41.     protected function compileRoute($route) {
  42.         $replacements = array(
  43.             'num' => '\d+',
  44.             'id' => '[1-9]\d*',
  45.             'str' => '\w+',
  46.             'any' => '.+',
  47.         );
  48.         $parts = preg_split('/\<(.*)\>/', $route, -1, PREG_SPLIT_DELIM_CAPTURE);
  49.         $i = 0;
  50.         $count = count($parts);
  51.         $pattern = '|^';
  52.         for (;;) {
  53.             $pattern .= preg_quote($parts[$i++]);
  54.             if ($i == $count) {
  55.                 break;
  56.             }
  57.             $placeholder = $parts[$i++];
  58.             if (!array_key_exists($placeholder, $replacements)) {
  59.                 throw new \Exception("Unknown placeholder <$placeholder> in route \"$route\"");
  60.             }
  61.             $pattern .= "({$replacements[$placeholder]})";
  62.         }
  63.         $pattern .= '$|';
  64.         return $pattern;
  65.     }
  66.  
  67.     public function serve($path) {
  68.         foreach ($this->routes as $pattern => $route) {
  69.             list($method, $controller, $action) = $route;
  70.             if (preg_match($pattern, $path, $matches) &&
  71.                 ($method == null || $method == $_SERVER['REQUEST_METHOD'])) {
  72.                 $instance = new $controller();
  73.                 $action = is_null($action) ? 'index' : $action;
  74.                 return call_user_func_array(array($instance, $action), array_slice($matches, 1));
  75.             }
  76.         }
  77.         throw new NotFound();
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement