Example #1
0
 /**
  * Evaluate a route and return the parameters.
  * @param Route $route
  * @return array|bool
  */
 public function parseRoute(Route $route)
 {
     if (preg_match_all('#^' . $route->getRoute() . '$#', $this->getUrl(), $matches, PREG_OFFSET_CAPTURE)) {
         // Rework matches to only contain the matches, not the orig string
         $matches = array_slice($matches, 1);
         // Extract the matched URL parameters (and only the parameters)
         $params = array_map(function ($match, $index) use($matches) {
             // We have a following parameter: take the substring from the current param position until the next one's position (thank you PREG_OFFSET_CAPTURE)
             if (isset($matches[$index + 1]) && isset($matches[$index + 1][0]) && is_array($matches[$index + 1][0])) {
                 return trim(substr($match[0][0], 0, $matches[$index + 1][0][1] - $match[0][1]), '/');
             } else {
                 return isset($match[0][0]) ? trim($match[0][0], '/') : null;
             }
         }, $matches, array_keys($matches));
         if (empty($params)) {
             return true;
         } else {
             return $params;
         }
     }
     return false;
 }
Example #2
0
 /**
  * @param Route $route
  * @param $params
  * @throws InvalidParameter
  */
 private function handleMatch(Route $route, $params)
 {
     $function = $route->getFunction();
     // Check for additional parameters
     foreach ($route->getParameters() as $v) {
         $params[] = $v;
     }
     // First handle the filters
     foreach ($route->getFilters() as $filter) {
         // Check if exist
         if (!isset($this->filters[$filter->getName()])) {
             throw new InvalidParameter("Filter " . $filter->getName() . " is not registered in the router.");
         }
         $filter->setRequest($this->request);
         $response = $filter->check($this->filters[$filter->getName()]);
         $filter->clearRequest();
         // If output was not TRUE, handle the filter return value as output.
         if ($response !== true) {
             $this->output($response);
             return;
         }
     }
     if (is_callable($function)) {
         $response = call_user_func_array($function, $params);
     } else {
         if (strpos($function, '@')) {
             $param = explode('@', $function);
             if (count($param) !== 2) {
                 throw new InvalidParameter("Controller@method syntax not valid for " . $function);
             }
             $response = $this->handleController($param[0], $param[1], $params, $route->getModule());
         } else {
             throw new InvalidParameter("Method not found.");
         }
     }
     $this->output($response);
 }