/** * preFilterRoutes * * @param RouteCollectionInterface $routes * * @return RouteCollectionInterface */ protected function preFilterRoutes(Request $request, RouteCollectionInterface $routes) { if ($routes instanceof CachedCollection && ($sRoutes = $routes->findByStaticPath($request->getPath()))) { $routes = 0 !== count($sRoutes->all()) ? $sRoutes : $routes; } return $routes->findByMethod($request->getMethod()); }
/** * matchRequest * * @param Request $request * * @return MatchContext */ public function matchRequest(Request $request, RouteCollectionInterface $routes) { $path = $request->getPath(); $filtered = $this->filterByMethodAndScheme($routes, $request); if ($filtered instanceof CachedCollectionInterface && 0 !== count($r = $route->findByStaticPath($path))) { $filtered = $r; } $nomatch = array_diff_key($routes->all(), $filtered->all()); foreach ($filtered->all() as $name => $route) { $rctx = $route->getContext(); // does it match host? if (!$this->matchHost($rctx, $request, $route->getHost())) { $nomatch[$name] = $route; continue; } // does it match static path? if (0 !== strpos($path, $rctx->getStaticPath())) { continue; } // does it match pattern described on the route? if ((bool) preg_match_all($rctx->getRegex(), $path, $matches)) { $vars = $this->getMatchedVars($route, $matches); $handler = $route->getHandler(); return new MatchContext(self::MATCH, $name, $request, $handler, $vars); } $nomatch[$name] = $route; } return new MatchContext($this->getMatchFailureReason($nomatch, $request), null, $request, null); }
/** * getMatchFailureReason * * @param array $nomatch * @param Request $request * * @return int */ private function getMatchFailureReason(array $nomatch, Request $request) { $path = $request->getPath(); $reduce = array_filter($nomatch, function ($route) use($path) { return (bool) preg_match_all($route->getContext()->getRegex(), $path); }); foreach ($reduce as $name => $route) { if (!$this->matchHost($route->getContext(), $request, $route->getHost())) { return RequestMatcherInterface::NOMATCH_HOST; } if (!$route->hasScheme($request->getScheme())) { return RequestMatcherInterface::NOMATCH_SCHEME; } if (!$route->hasMethod($request->getMethod())) { return RequestMatcherInterface::NOMATCH_METHOD; } } return RequestMatcherInterface::NOMATCH; }
/** * {@inheritdoc} */ public function getPath() { return $this->request->getPath(); }
/** * getReqPath * * @param RequestContextInterface $req * * @return string */ private function getReqPath(RequestContextInterface $req) { return $req->getPath(); }