Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] ignore NullSite scenario #9

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions src/Middleware/LowerCaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,52 @@ class LowerCaseUri implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$previousResult = $request->getAttribute('routing', null);
/** @var Site $site */
$site = $request->getAttribute('site', null);
$site->getRouter()->matchRequest($request, $previousResult);
} catch (RouteNotFoundException $e) {
$originalUri = $request->getUri();
$path = $originalUri->getPath();
$lowerCasePath = mb_strtolower($path);
/** @var Site $site */
$site = $request->getAttribute('site');

// nothing has changed, nothing to do
if ($lowerCasePath === $path) {
if ($site instanceof Site) {
try {
$previousResult = $request->getAttribute('routing');

$site->getRouter()->matchRequest($request, $previousResult);
return $handler->handle($request);
} catch (RouteNotFoundException $e) {
}
}

$updatedUri = $request->getUri()->withPath($lowerCasePath);
$originalUri = $request->getUri();
$path = $originalUri->getPath();
$lowerCasePath = mb_strtolower($path);

$doRedirect = false;
$redirectStatusCode = 307;
$site = $request->getAttribute('site');
if ($site instanceof Site) {
$doRedirect = (bool)$site->getConfiguration()['settings']['redirectOnUpperCase'];
$redirectStatusCode = (int)($site->getConfiguration()['settings']['redirectStatusCode'] ?? 307);
}
// nothing has changed, nothing to do
if ($lowerCasePath === $path) {
return $handler->handle($request);
}

// Redirects only work on GET and HEAD requests
if ($doRedirect && in_array($request->getMethod(), ['GET', 'HEAD'])) {
return new RedirectResponse($updatedUri, $redirectStatusCode);
}
$updatedUri = $request->getUri()->withPath($lowerCasePath);

// Update the path to just work as before, and continue with the process
$request = $request->withUri($updatedUri);
$routeResult = $request->getAttribute('routing');
if ($routeResult instanceof SiteRouteResult) {
$routeResult = new SiteRouteResult(
$updatedUri, $routeResult->getSite(), $routeResult->getLanguage(),
mb_strtolower($routeResult->getTail())
);
$request = $request->withAttribute('routing', $routeResult);
}
$doRedirect = false;
$redirectStatusCode = 307;
if ($site instanceof Site) {
$doRedirect = (bool)$site->getConfiguration()['settings']['redirectOnUpperCase'];
$redirectStatusCode = (int)($site->getConfiguration()['settings']['redirectStatusCode'] ?? 307);
}

// Redirects only work on GET and HEAD requests
if ($doRedirect && in_array($request->getMethod(), ['GET', 'HEAD'])) {
return new RedirectResponse($updatedUri, $redirectStatusCode);
}

// Update the path to just work as before, and continue with the process
$request = $request->withUri($updatedUri);
$routeResult = $request->getAttribute('routing');

if ($routeResult instanceof SiteRouteResult) {
$routeResult = new SiteRouteResult(
$updatedUri, $routeResult->getSite(), $routeResult->getLanguage(),
mb_strtolower($routeResult->getTail())
);
$request = $request->withAttribute('routing', $routeResult);
}

return $handler->handle($request);
Expand Down