Skip to content

Only add path prefix if the path does not contain it already #103

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

Closed
wants to merge 4 commits into from
Closed
Changes from 3 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
18 changes: 15 additions & 3 deletions src/Plugin/AddPathPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -39,9 +46,14 @@ 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())
);
$identifier = spl_object_hash((object) $first);

if (!in_array($identifier, $this->alteredRequests)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make alteredRequests a hashmap of $identifier => true instead? then we could check with array_has_key instead of in_array. for long running processes that send lots of requests, the in_array will become expensive once the list of altered requests grows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That came in my mind but IMO the chance of this is really low. Anyway, I updated to code to store identifier => identifier in the array therefore we do not need to check what is the value of an identifier. Storing boolean values in the array would be also misleading because someone may think that $identifier => false means the request has not been altered yet.

$request = $request->withUri($request->getUri()
->withPath($this->uri->getPath().$request->getUri()->getPath())
);
$this->alteredRequests[] = $identifier;
}

return $next($request);
}
Expand Down