/** * @param Request $httpRequest * @param RedirectInterface $redirect * @return Response|null */ protected function buildResponse(Request $httpRequest, RedirectInterface $redirect) { if (headers_sent() === true && FLOW_SAPITYPE !== 'CLI') { return null; } $response = new Response(); $statusCode = $redirect->getStatusCode(); $response->setStatus($statusCode); if ($statusCode >= 300 && $statusCode <= 399) { $location = $redirect->getTargetUriPath(); if (parse_url($location, PHP_URL_SCHEME) === null) { $location = $httpRequest->getBaseUri() . $location; } $response->setHeaders(new Headers(['Location' => $location, 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT'])); } elseif ($statusCode >= 400 && $statusCode <= 599) { $exception = new Exception(); $exception->setStatusCode($statusCode); throw $exception; } return $response; }
/** * @param RedirectInterface $redirect * @return RedirectInterface */ public static function create(RedirectInterface $redirect) { return new self($redirect->getSourceUriPath(), $redirect->getTargetUriPath(), $redirect->getStatusCode(), $redirect->getHost()); }
/** * Updates affected redirects in order to avoid redundant or circular redirections. * * @param RedirectInterface $newRedirect * @throws Exception if creating the redirect would cause conflicts * @return void */ protected function updateDependingRedirects(RedirectInterface $newRedirect) { /** @var $existingRedirectForSourceUriPath Redirect */ $existingRedirectForSourceUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getSourceUriPath(), $newRedirect->getHost(), false); if ($existingRedirectForSourceUriPath !== null) { $this->removeAndLog($existingRedirectForSourceUriPath, sprintf('Existing redirect for the source URI path "%s" removed.', $newRedirect->getSourceUriPath())); $this->routerCachingService->flushCachesForUriPath($existingRedirectForSourceUriPath->getSourceUriPath()); } /** @var $existingRedirectForTargetUriPath Redirect */ $existingRedirectForTargetUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getTargetUriPath(), $newRedirect->getHost(), false); if ($existingRedirectForTargetUriPath !== null) { $this->removeAndLog($existingRedirectForTargetUriPath, sprintf('Existing redirect for the target URI path "%s" removed.', $newRedirect->getTargetUriPath())); $this->routerCachingService->flushCachesForUriPath($existingRedirectForTargetUriPath->getSourceUriPath()); } $obsoleteRedirectInstances = $this->redirectRepository->findByTargetUriPathAndHost($newRedirect->getSourceUriPath(), $newRedirect->getHost()); /** @var $obsoleteRedirect Redirect */ foreach ($obsoleteRedirectInstances as $obsoleteRedirect) { if ($obsoleteRedirect->getSourceUriPath() === $newRedirect->getTargetUriPath()) { $this->redirectRepository->remove($obsoleteRedirect); } else { $obsoleteRedirect->setTargetUriPath($newRedirect->getTargetUriPath()); $this->redirectRepository->update($obsoleteRedirect); } } }
/** * @param string $prefix * @param RedirectInterface $redirect * @return void */ protected function outputRedirectLine($prefix, RedirectInterface $redirect) { $this->outputLine(' %s %s <info>=></info> %s <comment>(%d)</comment> - %s', [$prefix, $redirect->getSourceUriPath(), $redirect->getTargetUriPath(), $redirect->getStatusCode(), $redirect->getHost() ?: 'all host']); }