/** * @inheritDoc */ public function toRoute(string $routeName, array $variables = [], array $query = []) : UriInterface { $route = $this->routes->findByName($routeName); if ($route === null) { throw new RouteNotFoundException(sprintf('Unable to generate an URL for the named route "%s" as such route does not exist.', $routeName)); } return $this->buildRouteUri($route, $variables, $query); }
function it_throws_not_found_exception(ServerRequestInterface $request, RouteCollection $routeCollection, Route $route, UriInterface $uri, Dispatcher $dispatcher) { $routeCollection->getRoutes()->willReturn([$route]); $request->getUri()->willReturn($uri); $uri->getPath()->willReturn('/url'); $request->getMethod()->willReturn('GET'); $dispatcher->dispatch('GET', '/url')->willReturn([Dispatcher::NOT_FOUND]); $this->shouldThrow(NotFoundException::class)->match($request, $routeCollection); }
/** * @inheritDoc */ public function getRoutes() : array { $routes = []; foreach ($this->routes->getRoutes() as $route) { if ((!$route->getHost() || $route->getHost() === $this->request->getUri()->getHost()) && (!$route->getScheme() || $route->getScheme() === $this->request->getUri()->getScheme())) { $routes[] = $route; } } return $routes; }
function it_filters_decorated_collection_on_get_goutes(RouteCollection $collection, ServerRequestInterface $request, UriInterface $uri) { $route1 = (new Route(['GET'], '/url1', 'responder1'))->withHost('localhost'); $route2 = (new Route(['GET'], '/url2', 'responder2'))->secure(); $route3 = new Route(['GET'], '/url3', 'responder3'); $collection->getRoutes()->willReturn([$route1, $route2, $route3]); $request->getUri()->willReturn($uri); $uri->getHost()->willReturn('host'); $uri->getScheme()->willReturn('http'); $this->getRoutes()->shouldBe([$route3]); }
function it_generates_url_to_named_route(RouteCollection $routeCollection, Route $route, UriInterface $uri) { $routeCollection->findByName('name')->willReturn($route); $route->getScheme()->willReturn('http'); $route->getHost()->willReturn('example.com'); $route->compilePath(['key' => 'value'])->willReturn('/url'); $uri->withScheme('http')->willReturn($uri); $uri->withHost('example.com')->willReturn($uri); $uri->withPath('/url')->willReturn($uri); $uri->withQuery('param=val')->willReturn($uri); $result = $this->toRoute('name', ['key' => 'value'], ['param' => 'val']); $result->shouldBe($uri); $uri->withScheme('http')->shouldHaveBeenCalled(); $uri->withHost('example.com')->shouldHaveBeenCalled(); $uri->withPath('/url')->shouldHaveBeenCalled(); }
/** * @inheritDoc */ public function match(ServerRequestInterface $request, RouteCollectionContract $routeCollection) : RouteContract { $routes = $routeCollection->getRoutes(); $parsedRouteData = $this->parser->parse($routes); $dispatcher = $this->fastrouteDispatcherFactory->create($parsedRouteData); $match = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath()); switch ($match[0]) { case $dispatcher::FOUND: /** @var RouteContract $route */ list(, $route, $variables) = $match; return $route->withVariables($variables); case $dispatcher::METHOD_NOT_ALLOWED: throw new NotAllowedException($match[1]); default: throw new NotFoundException($request->getMethod(), $request->getUri()->getPath()); } }