Check whether a response is a redirection.
public static isRedirect ( Psr\Http\Message\ResponseInterface $response ) : boolean | ||
$response | Psr\Http\Message\ResponseInterface | |
Résultat | boolean |
/** * 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'); } $ajax = Utils\Helpers::isAjax($request); $debugBar = $this->debugBar ?: new StandardDebugBar(); //Redirection response if (Utils\Helpers::isRedirect($response)) { if ($debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { $debugBar->stackData(); } //Html response } elseif (FormatNegotiator::getFormat($request) === 'html') { $renderer = $debugBar->getJavascriptRenderer(); ob_start(); echo '<style>'; $renderer->dumpCssAssets(); echo '</style>'; echo '<script>'; $renderer->dumpJsAssets(); echo '</script>'; echo $renderer->render(!$ajax); $response = $this->inject($response, ob_get_clean()); //Ajax response } elseif ($ajax && $this->captureAjax) { $headers = $debugBar->getDataAsHeaders(); foreach ($headers as $name => $value) { $response = $response->withHeader($name, $value); } } return $next($request, $response); }
/** * 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; }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $uri = $request->getUri(); if (strtolower($uri->getScheme()) !== 'https') { $uri = $uri->withScheme('https')->withPort(443); if ($this->redirectStatus !== false && (!$this->checkHttpsForward || $request->getHeaderLine('X-Forwarded-Proto') !== 'https' && $request->getHeaderLine('X-Forwarded-Port') !== '443')) { return $this->getRedirectResponse($request, $uri, $response); } $request = $request->withUri($uri); } if (!empty($this->maxAge)) { $response = $response->withHeader(self::HEADER, sprintf('max-age=%d%s', $this->maxAge, $this->includeSubdomains ? ';includeSubDomains' : '')); } $response = $next($request, $response); if (Utils\Helpers::isRedirect($response)) { return $response->withHeader('Location', str_replace('http://', 'https://', $response->getHeaderLine('Location'))); } return $response; }