Example #1
0
 /**
  * Matches a user submitted path with a previously defined route.
  * Assigns and returns an array of defaults on a successful match.
  *
  * @param  EvHttp_Controller_Request_Http $request Request to get the path info from
  * @return array|false An array of assigned values or a false on a mismatch
  */
 public function match($request, $partial = null)
 {
     $path = trim($request->getPathInfo(), '/');
     $subPath = $path;
     $values = array();
     foreach ($this->_routes as $key => $route) {
         if ($key > 0 && $matchedPath !== null) {
             $separator = substr($subPath, 0, strlen($this->_separators[$key]));
             if ($separator !== $this->_separators[$key]) {
                 return false;
             }
             $subPath = substr($subPath, strlen($separator));
         }
         // TODO: Should be an interface method. Hack for 1.0 BC
         if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
             $match = $subPath;
         } else {
             $request->setPathInfo($subPath);
             $match = $request;
         }
         $res = $route->match($match, true);
         if ($res === false) {
             return false;
         }
         $matchedPath = $route->getMatchedPath();
         if ($matchedPath !== null) {
             $subPath = substr($subPath, strlen($matchedPath));
             $separator = substr($subPath, 0, strlen($this->_separators[$key]));
         }
         $values = $res + $values;
     }
     $request->setPathInfo($path);
     if ($subPath !== '' && $subPath !== false) {
         return false;
     }
     return $values;
 }
Example #2
0
 /**
  * Matches a user submitted path with parts defined by a map. Assigns and
  * returns an array of variables on a successful match.
  *
  * @param EvHttp_Controller_Request_Http $request Request to get the host from
  * @return array|false An array of assigned values or a false on a mismatch
  */
 public function match($request)
 {
     // Check the scheme if required
     if ($this->_scheme !== null) {
         $scheme = $request->getScheme();
         if ($scheme !== $this->_scheme) {
             return false;
         }
     }
     // Get the host and remove unnecessary port information
     $host = $request->getHttpHost();
     if (preg_match('#:\\d+$#', $host, $result) === 1) {
         $host = substr($host, 0, -strlen($result[0]));
     }
     $hostStaticCount = 0;
     $values = array();
     $host = trim($host, '.');
     if ($host != '') {
         $host = explode('.', $host);
         foreach ($host as $pos => $hostPart) {
             // Host is longer than a route, it's not a match
             if (!array_key_exists($pos, $this->_parts)) {
                 return false;
             }
             $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
             $hostPart = urldecode($hostPart);
             // If it's a static part, match directly
             if ($name === null && $this->_parts[$pos] != $hostPart) {
                 return false;
             }
             // If it's a variable with requirement, match a regex. If not - everything matches
             if ($this->_parts[$pos] !== null && !preg_match($this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu', $hostPart)) {
                 return false;
             }
             // If it's a variable store it's value for later
             if ($name !== null) {
                 $values[$name] = $hostPart;
             } else {
                 $hostStaticCount++;
             }
         }
     }
     // Check if all static mappings have been matched
     if ($this->_staticCount != $hostStaticCount) {
         return false;
     }
     $return = $values + $this->_defaults;
     // Check if all map variables have been initialized
     foreach ($this->_variables as $var) {
         if (!array_key_exists($var, $return)) {
             return false;
         }
     }
     $this->_values = $values;
     return $return;
 }