예제 #1
0
 /**
  * Attempts to parse a request object and determine its execution details.
  *
  * @param object $request A Library\HttpUrl object
  *        containing the details of the request to be routed.
  * @param array $options Used to determine the operation of the method, and override certain
  *              values in the `Request` object:
  *              - `'url'` _string_: If present, will be used to match in place of the `$url`
  *                 property of `$request`.
  * @return mixed If this route matches `$request`, returns an array of the execution details
  *         contained in the route, otherwise returns false.
  */
 public function parse(Library\HttpUrl $request, array $options = array())
 {
     $defaults = array('url' => $request->toString(Library\HttpUrl::PATH));
     $options += $defaults;
     $url = '/' . trim($options['url'], '/');
     $pattern = $this->_pattern;
     //Run regex template match against url
     if (!preg_match($pattern, $url, $match)) {
         return false;
     }
     //The meta array contains additional request meta data in the KRequest format (eg, request.varname)
     //The value of this key must match those present in the relevant global variable
     foreach ($this->_meta as $key => $compare) {
         $value = $request->query->get($key, 'raw');
         if (!($compare == $value || is_array($compare) && in_array($value, $compare))) {
             return false;
         }
     }
     $result = array_intersect_key($match, $this->_keys) + $this->_params + $this->_defaults;
     $request->path = array();
     $request->query = (array) $request->query + $result;
     //Merge the request query into the result
     if ($this->_handler) {
         call_user_func_array($this->_handler, array($request));
     }
     return $request->query;
 }