Example #1
0
 /**
  * {@inheritdoc}
  */
 public function match(ServerRequestInterface $request) : RoutingResult
 {
     $requestPath = $request->getUri()->getPath();
     $this->logger->debug(sprintf('Analysing request path "%s"', $requestPath));
     $candidates = [];
     /** @var array $routeDefinition */
     foreach ($this->routes as $routeDefinition) {
         $route = $routeDefinition['route'];
         $identifier = $this->getRouteIdentifier($route);
         $this->logger->debug(sprintf('Trying to match requested path to route "%s"', $identifier));
         $urlVars = [];
         if (preg_match_all($routeDefinition['pathMatcher'], $requestPath, $urlVars)) {
             $method = strtoupper(trim($request->getMethod()));
             if (!in_array($method, $route->getMethods())) {
                 $candidates[] = ['route' => $route, 'failure' => RoutingResult::FAILED_METHOD_NOT_ALLOWED];
                 continue;
             }
             // remove all elements which should not be set in the request,
             // e.g. the matching url string as well as all numeric items
             $params = $this->mapParams($urlVars);
             if (!$this->matchParams($route, $params)) {
                 $candidates[] = ['route' => $route, 'failure' => RoutingResult::FAILED_BAD_REQUEST];
                 continue;
             }
             $this->logger->debug(sprintf('Route "%s" matches. Applying its target...', $identifier));
             return RoutingResult::forSuccess($route, $params);
         }
     }
     $this->logger->debug('No matching route found.');
     if (count($candidates)) {
         $candidate = $candidates[0];
         return RoutingResult::forFailure($candidate['failure'], $candidate['route']);
     }
     return RoutingResult::forFailure(RoutingResult::FAILED_NOT_FOUND);
 }