Example #1
0
 /**
  * Init the controller of a route
  *
  * @return null|*
  */
 public function initRouteController()
 {
     $this->routeController = new $this->controllerClass();
     if (true == $this->routeController instanceof \Fewlines\Core\Controller\View) {
         $template = Template::getInstance();
         $this->routeController->init($template);
         return $this->callRouteMethod($this->activeRoute->getToMethod(), $this->activeRoute->getVarsRecursive());
     } else {
         throw new View\Exception\ControllerInitialisationGoneWrongException('The route controller could not be initialized.
             Must be instance of \\Fewlines\\Controller\\View');
     }
     return null;
 }
Example #2
0
 /**
  * Checks if the route is active
  *
  * @param  Route $route
  * @return boolean
  */
 private function checkRoute(Route $route)
 {
     if ($route->hasVars()) {
         $vars = $route->getVarsRecursive();
         $parts = ArrayHelper::clean($route->getParts());
         $diff = ArrayHelper::clean($this->getUrlParts());
         if (count($parts) != count($diff)) {
             for ($i = 0, $len = count($parts); $i < $len; $i++) {
                 if (preg_match(Route::VAR_MASK, trim($parts[$i])) && !array_key_exists($i, $diff)) {
                     if (Variable::isStringOptional($parts[$i])) {
                         unset($parts[$i]);
                     } else {
                         return false;
                     }
                 }
             }
             $parts = ArrayHelper::clean($parts);
         }
         $varValues = array();
         for ($i = 0, $len = count($parts); $i < $len; $i++) {
             /**
              * Check if the url is matching the url
              * from the given route, otherwise
              * this route was not found in the url
              */
             if (array_key_exists($i, $diff) && trim($diff[$i]) == trim($parts[$i])) {
                 unset($diff[$i]);
             } else {
                 if (array_key_exists($i, $diff) && preg_match(Route::VAR_MASK, trim($parts[$i]))) {
                     $varValues[] = $diff[$i];
                     unset($diff[$i]);
                 } else {
                     return false;
                 }
             }
         }
         $diff = ArrayHelper::clean($diff);
         if (count($diff) > 0) {
             return false;
         }
         /**
          * Validate given var values
          */
         for ($i = 0, $len = count($vars); $i < $len; $i++) {
             /**
              * Deactivate route if the required variables were
              * not found in the current url (Ignore if they
              * are declared as optional).
              *
              * Otherwise assign the value to the variable
              */
             if (!array_key_exists($i, $varValues)) {
                 if ($vars[$i]->isOptional()) {
                     $vars[$i]->setValue('');
                 } else {
                     return false;
                 }
             } else {
                 $vars[$i]->setValue($varValues[$i]);
             }
         }
     } else {
         $routeParts = ArrayHelper::clean($route->getParts());
         $urlParts = ArrayHelper::clean($this->getUrlParts());
         /**
          * Check if the length of the urlparts
          * matches the route parts
          */
         if (count($urlParts) > count($routeParts)) {
             return false;
         }
         /**
          * Check if the url corresponds to
          * the parts given in the route
          */
         for ($i = 0, $len = count($routeParts); $i < $len; $i++) {
             if (!array_key_exists($i, $urlParts) || trim($urlParts[$i]) != trim($routeParts[$i])) {
                 if (!preg_match(Route::VAR_MASK, $routeParts[$i])) {
                     return false;
                 }
             }
         }
     }
     return true;
 }