Example #1
0
 /**
  * Run the router, try to match current request path with registered route.
  */
 protected function dispatch()
 {
     $routeInfo = $this->dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $this->currentURL);
     switch ($routeInfo[0]) {
         case Dispatcher::NOT_FOUND:
             // Continue to WordPress
             break;
         case Dispatcher::METHOD_NOT_ALLOWED:
             $allowedMethods = $routeInfo[1];
             // Sample : ... 405 Method Not Allowed
             break;
         case Dispatcher::FOUND:
             if ($this->app->get('whoops') !== null) {
                 $this->app->get('whoops')->init();
             }
             // Hook WordPress Template
             $this->view->hook();
             // Get route parameter
             $routeHandler = $routeInfo[1];
             $vars = $routeInfo[2];
             // Protected route, check the token
             if (in_array($this->request->getMethod(), $this->protectedRoute) || $this->request->isXmlHttpRequest()) {
                 $this->request->checkToken();
             }
             // Check if route is deffered
             if (!$routeHandler->isDeferred()) {
                 $this->sendResponse($routeHandler, $vars);
                 return;
             }
             // Share routeHandler and vars to App
             $this->app->share('routeHandler', $routeHandler);
             $this->app->share('routeHandlerVars', $vars);
             // Defered route callback
             add_action($routeHandler->getRunLevel(), function () {
                 $routeHandler = $this->app->get('routeHandler');
                 $vars = $this->app->get('routeHandlerVars');
                 $this->sendResponse($routeHandler, $vars);
             }, $routeHandler->getPriority());
             break;
     }
 }