Find the route matching the given request.

@param Contracts\Request $request
@return object|null
497660
private function find(Contracts\Request $request)
    {
        $method = $request->method();
        $path   = $request->path();
        foreach ($this->routes as $route) {
            if ($route->method == $method) {
                $pattern = $this->pattern($route);
                if (preg_match($pattern, $path)) {
                    return $route;
                }
            }
        }

        return null;
    }
1074099
public function matches(RequestInterface $request): ?RouteMatchInterface
    {
        $path = $request->getUrl()->getPath();
        $directoryParts = $path->getDirectoryParts();
        $filename = $path->getFilename() ?? '';

        if (count($this->path) === 0) {
            // My path is empty, i.e. should only match root path.
            if (count($directoryParts) !== 0) {
                return null;
            }

            return new RouteMatch($this->getControllerClassName(), $filename);
        }

        if (count($this->path) > count($directoryParts)) {
            // My path contains more than the path in request, e.g. path /foo/ should not match /bar
            return null;
        }

        // Check each parts.
        $index = 0;
        foreach ($this->path as $pathPart) {
            // Part of path does not match.
            if ($pathPart !== $directoryParts[$index]) {
                return null;
            }

            $index++;
        }

        if (count($directoryParts) > $index) {
            $action = $directoryParts[$index];
            $parameters = array_slice($directoryParts, $index + 1);
            $parameters[] = $filename;
        } else {
            $action = $filename;
            $parameters = [];
        }

        return new RouteMatch($this->getControllerClassName(), $action, $parameters);
    }
908012
public function execute (Request $request)
		{
			foreach ($this->routes as $route) {
				if ($route->isValid($request)) {
					return $route->execute($request);
				}
			}

			throw new NoMatchingRouteException('No matching route found.');
		}
213393
public function findByRequest(Request $request): ?PageInterface
    {
        if (null !== $route = $request->attributes->get('_route', null)) {
            return $this->findByRoute($route);
        }

        return null;
    }
1357436
protected function getRouteFromRequest(Request $request)
    {
        $attributes = $request->attributes->get('_route_params');

        if (isset($attributes[RouteObjectInterface::ROUTE_OBJECT])
            && ($attributes[RouteObjectInterface::ROUTE_OBJECT] instanceof RouteInterface)
        ) {
            return $attributes[RouteObjectInterface::ROUTE_OBJECT];
        }

        return null;
    }
1287452
public function getMatchingRoute($request)
    {
        // Get matching route
        $this->currentRoute = $route = $this->routes->match($request);

        return $route;
    }
224465
public function matchRequest(Request $request)
    {
        $defaultSiteroot = null;

        $hostname = $request->getHost();

        foreach ($this->siterootManager->findAll() as $siteroot) {
            foreach ($siteroot->getUrls() as $siterootUrl) {
                $siterootHostname = $siterootUrl->getHostname();
                if ($siterootHostname === $hostname) {
                    return $siteroot;
                }
                if (isset($this->urlMappings[$siterootHostname])) {
                    if (in_array($hostname, $this->urlMappings[$siterootHostname])) {
                        return $siteroot;
                    }
                }
                if ($siterootHostname === $hostname) {
                    return $siteroot;
                }
                if ($siteroot->isDefault()) {
                    $defaultSiteroot = $siteroot;
                }
            }
        }

        if ($defaultSiteroot) {
            return $defaultSiteroot;
        }

        return null;
    }
1344986
public function execute( $request ) {
		/** @var $routes \WPEmerge\Routing\RouteInterface[] */
		$routes = $this->getRoutes();

		foreach ( $routes as $route ) {
			if ( $route->isSatisfied( $request ) ) {
				$this->setCurrentRoute( $route );
				return $route;
			}
		}

		return null;
	}
472435
public function getRoute(Request $request): Route
    {
        if ($this->configuration->isRestRouting()) {
            $route = RestRouter::getInstance($this->configuration, $request)->resolveRoute();

            if ($route instanceof Route) {
                return $route;
            }
        }

        $query = $request->query->get('route');
        $this->routeParams = explode('/', trim($query, '/'));

        $route = (new Route())
            ->setController($this->resolveController())
            ->setAction($this->resolveAction())
            ->setActionParams($this->resolveActionParams());

        return $route;
    }
1240697
private function getParameters(Contracts\Request $request, $route)
    {
        $path    = $request->path();
        $pattern = $this->pattern($route);
        if (preg_match_all($pattern, $path, $matches, PREG_SET_ORDER) === false) {
            return []; // @codeCoverageIgnore
        }

        unset($matches[0][0]);
        $matches = array_values($matches[0]);

        return $matches;
    }
813867
protected function gatherRouteMiddlewares( Request $request )
	{
		if ( $route = $request->route() )
			return $this->router()->gatherRouteMiddlewares( $route );
	}
264037
public function handle(Request $request)
    {
        $pathInfo = $request->getPathInfo();

        if (0 === strpos($pathInfo, $this->basePath)) {
            foreach ($this->routes as $config) {
                list($method, $pattern, $controller) = $config;

                if ($request->getMethod() === strtoupper($method)
                    && preg_match('!^' . preg_quote($this->basePath . $pattern, '!') . '$!', $request->getPathInfo())
                ) {
                    return call_user_func($controller, $request);
                }
            }
        }

        return null;
    }
701086
protected function determineTargetUrl(Request $request)
    {
        if ($this->options['always_use_default_target_path']) {
            return $this->options['default_target_path'];
        }

        if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
            return $targetUrl;
        }

        if (null !== $this->providerKey && $targetUrl = $this->getTargetPath($request->getSession(), $this->providerKey)) {
            $this->removeTargetPath($request->getSession(), $this->providerKey);

            return $targetUrl;
        }

        if ($this->options['use_referer'] && $targetUrl = $request->headers->get('Referer')) {
            if (false !== $pos = strpos($targetUrl, '?')) {
                $targetUrl = substr($targetUrl, 0, $pos);
            }
            if ($targetUrl && $targetUrl !== $this->httpUtils->generateUri($request, $this->options['login_path'])) {
                return $targetUrl;
            }
        }

        return $this->options['default_target_path'];
    }
217474
public static function get($route, $contract)
    {
        $contract = explode('@', $contract);
        if (!empty($contract[0]) && !empty($contract[1])) {
            $controller = $contract[0];
            $action = $contract[1];
            self::$routes[$route.'@get'] = ['method' => 'get', 'controller' => $controller, 'action' => $action];
        }
    }
1042099
public function match(Request $request)
    {
        // Get all the routes
        $routes = $this->getByMethod($request->getMethod());

        // Get the matching route out of the collection.
        $route = $this->getMatchingRoute($routes, $request);
        if (!is_null($route)) {
            return $route->bind($request);
        }

        // If no route was found we will now check if a matching route is specified by
        // another HTTP verb. If it is we will need to throw a MethodNotAllowed and
        // inform the user agent of which HTTP verb it should use for this route.
        /*$others = $this->checkForAlternateVerbs($request);
        if (count($others) > 0) {
            return $this->getRouteForMethods($request, $others);
        }*/

        throw new NotFoundHttpException('No route was found for the given request.');
    }
135690
public function matches(Route $route, Request $request)
    {
        return in_array($request->getMethod(), $route->methods());
    }
165824
public function match(Request $request)
    {
        try {
            return parent::match($request);
        } catch (NotFoundHttpException $e) {
            $route = $this->findMatchingWordpressPost();

            if ( ! is_null($route)) {
                return $route->bind($request);
            }

            throw $e;
        }
    }