/**
  * @param Request  $request
  * @param callable $next
  *
  * @return mixed
  * @throws \Exception
  */
 public function handle(Request $request, callable $next)
 {
     $method = $request->param('__ACTION__', $request->getMethod());
     $server = $request->server();
     $server['REQUEST_METHOD'] = $method;
     $pathInfo = $request->getPathInfo();
     $this->router->match($pathInfo, $server);
     if (!($route = $this->router->getMatchedRoute())) {
         throw new \Exception(sprintf('Could not find a route for the path info "%s"', $pathInfo), Response::HTTP_NOT_FOUND);
     }
     $routeParams = $this->router->getMatchedRoute()->getRouteParams();
     $request->set('__ACTION__', strtolower($method));
     foreach ($routeParams as $paramName => $paramValue) {
         if ($paramName == 'namespace' || $paramName == 'controller' || $paramName == 'action') {
             $paramName = '__' . strtoupper($paramName) . '__';
         }
         $request->set($paramName, $paramValue);
     }
     $middlewares = $this->router->getMiddlewares($route->getName());
     $pipe = $this->middlewarePipe->pipe($middlewares);
     if ($response = $pipe($request)) {
         return $response;
     }
     return $next($request);
 }