Skip to content

Commit

Permalink
Add Router::finalizePath() to finalize path
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Dec 3, 2024
1 parent 0196349 commit 74eded0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.5.0] - 2024-12-03

### Added

- `Router::finalizePath()` to finalize path

## [2.4.1] - 2024-12-03

### Fixed
Expand Down
22 changes: 18 additions & 4 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,32 @@ public function generate(string|RouteInterface $route, array|RouteAttributes $pa
throw new NotFoundException(sprintf('Route "%s" does not exists', $route));
}

$str = $route->generate($parameters);
return $this->finalizePath($route->generate($parameters));
}

/**
* Finalize path.
*
* @param string $path
*
* @return string
*/
public function finalizePath(string $path): string
{
if (true === str_contains($path, '://')) {
return $path;
}

// X-Forwarded-Prefix
if (false !== $this->options['X-Forwarded-Prefix']) {
$xForwardedPrefix = $this->options['X-Forwarded-Prefix'] === true ? 'X-Forwarded-Prefix' : (string)$this->options['X-Forwarded-Prefix'];
$xForwardedPrefix = 'HTTP_' . strtoupper(str_replace('-', '_', $xForwardedPrefix));
if (!empty($prefix = $_SERVER[$xForwardedPrefix] ?? null)) {
$str = '/' . trim($prefix, '/') . $str;
$path = rtrim('/' . trim($prefix, '/'), '/') . '/' . ltrim($path, '/');
}
}

return $str;
return $path;
}

private function generateParameters(array|RouteAttributes $parameters = []): array
Expand Down Expand Up @@ -199,4 +213,4 @@ public function handle(ServerRequestInterface &$request): ?RouteInterface

return $route;
}
}
}

0 comments on commit 74eded0

Please sign in to comment.