/** * @testdox Ensure the Router parses routes. * * @param string $r The route to parse. * @param boolean $e True if an exception is expected. * @param array $i The expected return data. * @param boolean $m True if routes should be set up. * * @dataProvider seedTestParseRoute */ public function testParseRoute($r, $e, $i, $m) { if ($m) { $this->setRoutes(); } // If we should expect an exception set that up. if ($e) { $this->expectException('InvalidArgumentException'); } // Execute the route parsing. $actual = $this->instance->parseRoute($r); // Test the assertions. $this->assertEquals($i, $actual, 'Incorrect value returned.'); }
/** * Execute the middleware. Don't call this method directly; it is used by the `Application` internally. * * @internal * * @param ServerRequestInterface $request The request object * @param ResponseInterface $response The response object * @param callable $next The next middleware handler * * @return ResponseInterface */ public function handle(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $attributes = $request->getAttributes(); if (!isset($attributes['command'])) { try { /** @var RepositoryInterface $repository */ $repository = $this->container->get('Repository')->forEntity(Page::class); /** @var Page[] $pages */ $pages = $repository->getAll(); $router = new Router(); foreach ($pages as $page) { $router->get($this->expandUrl($page->url, $page), function () use($page) { return $page; }); } $path = preg_replace('~^/.*?index.php/?~', '', $request->getUri()->getPath()); $route = $router->parseRoute($path); $page = $route['controller'](); $vars = $route['vars']; $command = new DisplayPageCommand($page->id, $vars, $request, $response->getBody(), $this->container); $request = $request->withAttribute('command', $command); // @todo Emit afterRouting event } catch (InvalidArgumentException $e) { // Do nothing } } return $next($request, $response); }