public function setup() { /** @var ContainerInterface $container */ $container = (require 'config/container.php'); $config = $container->get('config'); /** @var RouterInterface $router */ $this->router = $container->get(RouterInterface::class); foreach ($config['routes'] as $route) { $this->router->addRoute(new Route($route['path'], $route['middleware'], $route['allowed_methods'], $route['name'])); } $this->request = new ServerRequest([], [], 'http://example.com/', 'GET', 'php://memory'); }
private function seedRoutes(RouterInterface $router) : RouterInterface { $middleware = function () { }; foreach ($this->routes as $name => $path) { $router->addRoute(new Route($path, $middleware, ['GET'], $name)); } return $router; }
/** * Add a route for the route middleware to match. * * Accepts either a Router\Route instance, or a combination of a path and * middleware, and optionally the HTTP methods allowed. * * On first invocation, pipes the route middleware to the middleware * pipeline. * * @param string|Router\Route $path * @param callable|string|array $middleware Middleware (or middleware service name) to associate with route. * @param null|array $methods HTTP method to accept; null indicates any. * @param null|string $name the name of the route * @return Router\Route * @throws Exception\InvalidArgumentException if $path is not a Router\Route AND middleware is null. */ public function route($path, $middleware = null, array $methods = null, $name = null) { if (!$path instanceof Router\Route && null === $middleware) { throw new Exception\InvalidArgumentException(sprintf('%s expects either a route argument, or a combination of a path and middleware arguments', __METHOD__)); } if ($path instanceof Router\Route) { $route = $path; $path = $route->getPath(); $methods = $route->getAllowedMethods(); $name = $route->getName(); } $this->checkForDuplicateRoute($path, $methods); if (!isset($route)) { $methods = null === $methods ? Router\Route::HTTP_METHOD_ANY : $methods; $route = new Router\Route($path, $middleware, $methods, $name); } $this->routes[] = $route; $this->router->addRoute($route); return $route; }