Example #1
0
 /**
  * @param   \Phphilosophy\Router\Route  $route  A route instance
  *
  * @return  string  The pattern as a regex
  */
 private function parse(Route $route)
 {
     // The parameter token namespace
     $tokens = array_keys($this->tokens);
     // The route pattern
     $pattern = $route->getPattern();
     // Search for all kinds of parameters and translate them
     foreach ($tokens as $token) {
         $pattern = $this->translate($token, $pattern);
     }
     // The parsed regular expression ready for matching
     return '#^' . $pattern . '$#D';
 }
Example #2
0
 /**
  * @access  private
  * @param   \Phphilosophy\Router\Route  $route  A route instance
  * @param   string                      $uri    The request URI
  * @param   string                      $method The request method
  * @return  mixed
  */
 private function match($route, $uri, $method)
 {
     // The pattern given by the user
     $pattern = $route->getPattern();
     // The regular expression
     $regex = $this->patternToRegex($pattern);
     // The methods of this route
     $methods = $route->getMethods();
     // Run the callable
     if (preg_match($regex, $uri) && in_array($method, $methods)) {
         $params = $this->getParams($uri, $pattern);
         $this->match = true;
         call_user_func_array($route->getAction(), $params);
     }
 }
Example #3
0
 /**
  * @param   string          $pattern    The route pattern
  * @param   mixed           $action     The route action
  * @param   array|string    $methods    The route method(s)
  *
  * @retun   void
  */
 private static function addRoute($pattern, $action, $methods)
 {
     // Create new route entity
     $route = new Route($pattern, $action, $methods);
     // If a guard is present, add it to the route object
     if (!is_null(self::$guard) && !is_null(self::$redirect)) {
         $route->setGuard(self::$guard, self::$redirect);
     }
     // Add route entity to the internal route collection
     self::$router->addRoute($route);
 }
Example #4
0
 /**
  * @param   \Phphilosophy\Router\Route  $route  A route instance
  *
  * @return  void
  */
 private function match(Route $route)
 {
     // The pattern given by the user
     $pattern = $route->getPattern();
     // Run the callable
     if ($this->matcher->match($route) && $this->guard($route)) {
         $params = $this->parser->parse($pattern);
         $this->match = true;
         call_user_func_array($route->getAction(), $params);
     }
 }