From 1f1e3457059d118d04a67287f2b2b5d6614de566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20BICZ=C3=93?= Date: Thu, 3 May 2018 08:54:49 +0200 Subject: [PATCH 1/4] Only add path prefix if the path does not contain it already --- src/Plugin/AddPathPlugin.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Plugin/AddPathPlugin.php b/src/Plugin/AddPathPlugin.php index e24d61a..12f5246 100644 --- a/src/Plugin/AddPathPlugin.php +++ b/src/Plugin/AddPathPlugin.php @@ -39,9 +39,11 @@ public function __construct(UriInterface $uri) */ public function handleRequest(RequestInterface $request, callable $next, callable $first) { - $request = $request->withUri($request->getUri() - ->withPath($this->uri->getPath().$request->getUri()->getPath()) - ); + if (strpos($request->getUri()->getPath(), $this->uri->getPath()) !== 0) { + $request = $request->withUri($request->getUri() + ->withPath($this->uri->getPath().$request->getUri()->getPath()) + ); + } return $next($request); } From 7a0034ca46442744eb15ba423367712bc4960a9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20BICZ=C3=93?= Date: Thu, 3 May 2018 08:59:11 +0200 Subject: [PATCH 2/4] CS fix. --- src/Plugin/AddPathPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/AddPathPlugin.php b/src/Plugin/AddPathPlugin.php index 12f5246..41d65bb 100644 --- a/src/Plugin/AddPathPlugin.php +++ b/src/Plugin/AddPathPlugin.php @@ -39,7 +39,7 @@ public function __construct(UriInterface $uri) */ public function handleRequest(RequestInterface $request, callable $next, callable $first) { - if (strpos($request->getUri()->getPath(), $this->uri->getPath()) !== 0) { + if (0 !== strpos($request->getUri()->getPath(), $this->uri->getPath())) { $request = $request->withUri($request->getUri() ->withPath($this->uri->getPath().$request->getUri()->getPath()) ); From c34a2c9278b7430161ebee3a75a8bf12bd8140aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20BICZ=C3=93?= Date: Thu, 3 May 2018 09:48:09 +0200 Subject: [PATCH 3/4] Hash requests to prevent multiple alteration of the same request. --- src/Plugin/AddPathPlugin.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Plugin/AddPathPlugin.php b/src/Plugin/AddPathPlugin.php index 41d65bb..4dfa68e 100644 --- a/src/Plugin/AddPathPlugin.php +++ b/src/Plugin/AddPathPlugin.php @@ -18,6 +18,13 @@ final class AddPathPlugin implements Plugin */ private $uri; + /** + * Stores identifiers of the already altered requests. + * + * @var array + */ + private $alteredRequests = []; + /** * @param UriInterface $uri */ @@ -39,10 +46,13 @@ public function __construct(UriInterface $uri) */ public function handleRequest(RequestInterface $request, callable $next, callable $first) { - if (0 !== strpos($request->getUri()->getPath(), $this->uri->getPath())) { + $identifier = spl_object_hash((object) $first); + + if (!in_array($identifier, $this->alteredRequests)) { $request = $request->withUri($request->getUri() ->withPath($this->uri->getPath().$request->getUri()->getPath()) ); + $this->alteredRequests[] = $identifier; } return $next($request); From 6e68b3eb1f543ed47ed4f4e372a5e0a1a4d8063d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20BICZ=C3=93?= Date: Thu, 3 May 2018 15:21:47 +0200 Subject: [PATCH 4/4] Small performance improvement --- src/Plugin/AddPathPlugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugin/AddPathPlugin.php b/src/Plugin/AddPathPlugin.php index 4dfa68e..93ded1c 100644 --- a/src/Plugin/AddPathPlugin.php +++ b/src/Plugin/AddPathPlugin.php @@ -48,11 +48,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl { $identifier = spl_object_hash((object) $first); - if (!in_array($identifier, $this->alteredRequests)) { + if (!array_key_exists($identifier, $this->alteredRequests)) { $request = $request->withUri($request->getUri() ->withPath($this->uri->getPath().$request->getUri()->getPath()) ); - $this->alteredRequests[] = $identifier; + $this->alteredRequests[$identifier] = $identifier; } return $next($request);