/** * {@inheritdoc} */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $this->request = new ActionRequest($request); $this->request = $this->request->withAttribute('rewrite', $this->rewrite); $path = $this->request->path; if (!$this->rewrite) { if (!isset($path[0]) or $path[0] != $this->request->scriptName) { return $this->redirectPath($path, $this->request->query, '', true); } array_shift($path); $this->request = $this->request->withAttribute('path', $path); } if (count($path) > 0 and $path[count($path) - 1] === '') { return $this->redirectPath($path, $this->request->query, '', true); } $this->route = $this->findMatch($path, $this->request->getMethod()); if (!isset($this->route)) { throw new Route\RouteException('No route found for path "' . implode('/', $path) . '"'); } $middleware = $this->middleware; $this->triggerEvent('dispatch', new RouterEvent($this, $this->route, $this->request, $response)); $first = $this->getNext($middleware, [$this->route, 'dispatch']); $response = $first($this->request, $response); $this->triggerEvent('dispatched', new RouterEvent($this, $this->route, $this->request, $response)); return $response; }