Example #1
0
 /**
  * Does this request matches the route pattern
  * @param  Request $request
  * @param  boolean $matchVerb - Whether you want to match the route using the request HTTP verb
  *                            - useful for OPTIONS requests
  * @param  string  $basePath  - add a base path to the route pattern
  * @return boolean $result
  */
 public function matches(Request $request, $matchVerb = true, $basePath = null)
 {
     if ($matchVerb && $this->usesHttpVerbs()) {
         try {
             $method = $request->getHttpMethod();
             if (!in_array($method, $this->verbs)) {
                 return false;
             }
         } catch (DrestException $e) {
             return false;
         }
     }
     //Convert URL params into regex patterns, construct a regex for this route, init params
     $routePattern = is_null($basePath) ? (string) $this->route_pattern : '/' . $basePath . '/' . ltrim((string) $this->route_pattern, '/');
     $patternAsRegex = preg_replace_callback('#:([\\w]+)\\+?#', array($this, 'matchesCallback'), str_replace(')', ')?', $routePattern));
     if (substr($this->route_pattern, -1) === '/') {
         $patternAsRegex .= '?';
     }
     //Cache URL params' names and values if this route matches the current HTTP request
     if (!preg_match('#^' . $patternAsRegex . '$#', $request->getPath(), $paramValues)) {
         return false;
     }
     foreach ($this->param_names as $name) {
         if (isset($paramValues[$name])) {
             if (isset($this->param_names_path[$name])) {
                 $parts = explode('/', urldecode($paramValues[$name]));
                 $this->route_params[$name] = array_shift($parts);
                 $this->unmapped_route_params = $parts;
             } else {
                 $this->route_params[$name] = urldecode($paramValues[$name]);
             }
         }
     }
     // Check the route conditions
     foreach ($this->route_conditions as $key => $condition) {
         if (!preg_match('/^' . $condition . '$/', $this->route_params[$key])) {
             $this->param_names_path = $this->route_params = $this->unmapped_route_params = array();
             return false;
         }
     }
     return true;
 }
Example #2
0
 /**
  * Does this request match the route pattern
  * @param  Request $request
  * @param  boolean $matchVerb - Whether you want to match the route using the request HTTP verb
  *                            - useful for OPTIONS requests to provide route info
  * @param  string  $basePath  - add a base path to the route pattern
  * @return boolean $result
  */
 public function matches(Request $request, $matchVerb = true, $basePath = null)
 {
     // If we're matching the verb and we've defined them, ensure the method used is in our list of registered verbs
     if ($matchVerb && $this->routeMetaData->usesHttpVerbs() && !$this->methodIsInOurListOfAllowedVerbs($request->getHttpMethod())) {
         return false;
     }
     $patternAsRegex = $this->getMatchRegexPattern($basePath);
     //Cache URL params' names and values if this route matches the current HTTP request
     if (!preg_match('#^' . $patternAsRegex . '$#', $request->getPath(), $paramValues)) {
         return false;
     }
     // Process the param names and save them on the route params
     $this->processRouteParams($paramValues);
     // Check the route conditions
     if (!$this->routeConditionsAreValid()) {
         return false;
     }
     return true;
 }