Пример #1
0
 /**
  * Attempts to parse a request object and determine its execution details.
  *
  * @see lithium\net\http\Request
  * @see lithium\net\http\Request::$params
  * @see lithium\net\http\Route::$_handler
  * @param \lithium\net\http\Request $request A request 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 object|boolean If this route matches `$request`, returns the request with
  *         execution details attached to it (inside `Request::$params`). Alternatively when
  *         a route handler function was used, returns the result of its invocation. Returns
  *         `false` if the route never matched.
  */
 public function parse($request, array $options = array())
 {
     $defaults = array('url' => $request->url);
     $options += $defaults;
     $url = '/' . trim($options['url'], '/');
     $pattern = $this->_pattern;
     if (!preg_match($pattern, $url, $match)) {
         return false;
     }
     foreach ($this->_meta as $key => $compare) {
         $value = $request->get($key);
         if (!($compare == $value || is_array($compare) && in_array($value, $compare))) {
             return false;
         }
     }
     foreach ($this->_config['modifiers'] as $key => $modifier) {
         if (isset($match[$key])) {
             $match[$key] = $modifier($match[$key]);
         }
     }
     $result = array_intersect_key($match + array('args' => array()), $this->_keys);
     foreach ($result as $key => $value) {
         if ($value === '') {
             unset($result[$key]);
         }
     }
     $result += $this->_params + $this->_defaults;
     $request->params = $result + (array) $request->params;
     $request->persist = array_unique(array_merge($request->persist, $this->_persist));
     if ($this->_handler) {
         $handler = $this->_handler;
         return $handler($request);
     }
     return $request;
 }