Example #1
0
 /**
  * Attempts to match a route to a request method and URI.
  * 
  * If matched, sets the route's parameters from the regex matches.
  * 
  * @param \xpl\Routing\RouteInterface $route
  * @return boolean True if the route matched, otherwise false.
  */
 public function matchRoute(RouteInterface $route)
 {
     if ($route->getMethod() === $this->method) {
         if (preg_match('#^/?' . $route->getCompiledUri() . '/?$#i', $this->uri, $params)) {
             unset($params[0]);
             if (!empty($params)) {
                 $route->setParams($params);
             }
             return true;
         }
     }
     return false;
 }
Example #2
0
 /**
  * Compiles a route URI path.
  * 
  * Parses the path for tokens and replaces with regex. The matched tokens
  * are added to the route as the "$tokens" property. The route then knows
  * what to accept as parameters if and when it is matched.
  * 
  * @param \xpl\Routing\RouteInterface $route
  */
 public function compile(RouteInterface $route)
 {
     $uri = $route->getUri();
     $tokens = $search = $replace = array();
     if (preg_match_all('#\\{(\\w+)\\}(\\?)?#', $uri, $vars)) {
         foreach ($vars[1] as $i => $token) {
             if (!($regex = $this->tokens->get($token))) {
                 throw new \InvalidArgumentException("Unknown route token: '{$token}'.");
             }
             $regex = "/?({$regex})";
             if (!empty($vars[2][$i])) {
                 // Optional parameter
                 $regex .= '?';
             }
             $search[] = '/' . $vars[0][$i];
             $replace[] = $regex;
             $tokens[$token] = $token;
         }
         $uri = str_replace($search, $replace, $uri);
     }
     $route->setCompiledUri($uri);
     $route->setTokens($tokens);
 }
Example #3
0
 /**
  * Returns the callback for a route.
  * 
  * @param \xpl\Routing\RouteInterface $route
  * @return callable Route callback.
  */
 protected function getRouteCallback(Route $route, Bundle $app)
 {
     $callback = $route->getAction();
     if (is_string($callback) && false !== strpos($callback, '::')) {
         list($class, $method) = explode('::', $callback);
         $controller = $this->di->resolve($class, $this->request, $this->response, $route, $app);
         if ($controller) {
             $callback = array($controller, $method);
         }
     }
     return $callback;
 }