Skip to content

Commit 9bb8b15

Browse files
committed
Dont rewrite path if already rewritten
1 parent 720657f commit 9bb8b15

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/Plugin/AddPathPlugin.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,14 @@ public function __construct(UriInterface $uri)
4646
*/
4747
public function handleRequest(RequestInterface $request, callable $next, callable $first)
4848
{
49-
// Use the request URI as an identifier
50-
$identifier = sha1((string) $request->getUri());
51-
52-
// If the current URL is a result of a previous rewrite, then do nothing.
53-
if (!array_key_exists($identifier, $this->alteredRequests)) {
54-
$prefixedUri = $request->getUri()->withPath($this->uri->getPath().$request->getUri()->getPath());
55-
$request = $request->withUri($prefixedUri);
56-
57-
// Store for future checks
58-
$this->alteredRequests[sha1((string) $prefixedUri)] = true;
59-
}
49+
$prepend = $this->uri->getPath();
50+
$path = $request->getUri()->getPath();
51+
52+
if (substr($path, 0, strlen($prepend)) !== $prepend) {
53+
$request = $request->withUri($request->getUri()
54+
->withPath($prepend.$path)
55+
);
56+
}
6057

6158
return $next($request);
6259
}

tests/Plugin/AddPathPluginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testRewriteWithDifferentUrlWhenSecondUrlIncludesAddedPath()
8989
public function testRewriteWhenPathIsIncluded()
9090
{
9191
$verify = function (RequestInterface $request) {
92-
$this->assertEquals('https://example.com/api/api/foo', $request->getUri()->__toString());
92+
$this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString());
9393
};
9494

9595
$request = new Request('GET', 'https://example.com/api/foo');

0 commit comments

Comments
 (0)