Example #1
0
 public function getDispatcher()
 {
     static $dispatcher;
     if ($dispatcher) {
         return $dispatcher;
     }
     $routes = (include $this->config['config'] . '/routes.php');
     //TODO: Check if routes have been cached
     if (true) {
         $collector = new RouteCollector();
         foreach ($routes as $left => $right) {
             // $left is something like 'GET /'
             // $right is a callable
             $left = explode(" ", $left);
             if (sizeof($left) === 2) {
                 switch ($left[0]) {
                     case 'GET':
                         $collector->get($left[1], $right);
                         break;
                     case 'POST':
                         $collector->post($left[1], $right);
                         break;
                     case 'ANY':
                         $collector->any($left[1], $right);
                         break;
                     case 'HEAD':
                         $collector->head($left[1], $right);
                         break;
                     case 'OPTIONS':
                         $collector->options($left[1], $right);
                         break;
                     case 'DELETE':
                         $collector->delete($left[1], $right);
                         break;
                     default:
                         throw new ConfigException("Don't understand the HTTP method '" . $left[0] . "' in routes.php.");
                 }
             }
         }
         $routeData = $collector->getData();
     }
     return $dispatcher = new Dispatcher($routeData, $this->getHandlerResolver());
 }