/** * Returns a redirect response. * * @param ServerRequestInterface $request * @param UriInterface $uri * @param ResponseInterface $response * * @return ResponseInterface */ private function getRedirectResponse(ServerRequestInterface $request, UriInterface $uri, ResponseInterface $response) { //Fix the basePath $generator = BasePath::getGenerator($request); if ($generator !== null) { $uri = $uri->withPath($generator($uri->getPath())); } return $response->withStatus($this->redirectStatus)->withHeader('Location', (string) $uri); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('This middleware needs FormatNegotiator executed before'); } $renderer = $this->debugBar->getJavascriptRenderer(); //Is an asset? $path = $request->getUri()->getPath(); $renderPath = $renderer->getBaseUrl(); if (strpos($path, $renderPath) === 0) { $file = $renderer->getBasePath() . substr($path, strlen($renderPath)); if (file_exists($file)) { $body = Middleware::createStream(); $body->write(file_get_contents($file)); return $response->withBody($body); } } $response = $next($request, $response); //Fix the render baseUrl $renderPath = Utils\Helpers::joinPath(BasePath::getBasePath($request), $renderer->getBaseUrl()); $renderer->setBaseUrl($renderPath); $ajax = Utils\Helpers::isAjax($request); //Redirection response if (Utils\Helpers::isRedirect($response)) { if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { $this->debugBar->stackData(); } //Html response } elseif (FormatNegotiator::getFormat($request) === 'html') { if (!$ajax) { $response = $this->inject($response, $renderer->renderHead(), 'head'); } $response = $this->inject($response, $renderer->render(!$ajax), 'body'); //Ajax response } elseif ($ajax && $this->captureAjax) { $headers = $this->debugBar->getDataAsHeaders(); foreach ($headers as $name => $value) { $response = $response->withHeader($name, $value); } } return $response; }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $renderer = $this->debugBar->getJavascriptRenderer(); //Is an asset? $path = $request->getUri()->getPath(); $renderPath = $renderer->getBaseUrl(); if (strpos($path, $renderPath) === 0) { $file = $renderer->getBasePath() . substr($path, strlen($renderPath)); if (file_exists($file)) { return $response->withBody(self::createStream($file, 'r')); } } $response = $next($request, $response); //Fix the render baseUrl $generator = BasePath::getGenerator($request); if ($generator) { $renderer->setBaseUrl($generator($renderer->getBaseUrl())); } $ajax = Utils\Helpers::isAjax($request); //Redirection response if (Utils\Helpers::isRedirect($response)) { if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { $this->debugBar->stackData(); } //Html response } elseif (Utils\Helpers::getMimeType($response) === 'text/html') { if (!$ajax) { $response = $this->inject($response, $renderer->renderHead(), 'head'); } $response = $this->inject($response, $renderer->render(!$ajax), 'body'); //Ajax response } elseif ($ajax && $this->captureAjax) { $headers = $this->debugBar->getDataAsHeaders(); foreach ($headers as $name => $value) { $response = $response->withHeader($name, $value); } } return $response; }