Skip to content

Remove spl_object_hash to a version compatible PHP 5.X #211

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

Merged
merged 1 commit into from
Jul 11, 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
35 changes: 30 additions & 5 deletions src/Plugin/AddPathPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,42 @@ public function __construct(UriInterface $uri)
}

/**
* Adds a prefix in the beginning of the URL's path.
*
* The prefix is not added if that prefix is already on the URL's path. This will fail on the edge
* case of the prefix being repeated, for example if `https://example.com/api/api/foo` is a valid
* URL on the server and the configured prefix is `/api`.
*
* We looked at other solutions, but they are all much more complicated, while still having edge
* cases:
* - Doing an spl_object_hash on `$first` will lead to collisions over time because over time the
* hash can collide.
* - Have the PluginClient provide a magic header to identify the request chain and only apply
* this plugin once.
*
* There are 2 reasons for the AddPathPlugin to be executed twice on the same request:
* - A plugin can restart the chain by calling `$first`, e.g. redirect
* - A plugin can call `$next` more than once, e.g. retry
*
* Depending on the scenario, the path should or should not be added. E.g. `$first` could
* be called after a redirect response from the server. The server likely already has the
* correct path.
*
* No solution fits all use cases. This implementation will work fine for the common use cases.
* If you have a specific situation where this is not the right thing, you can build a custom plugin
* that does exactly what you need.
*
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$identifier = spl_object_hash((object) $first);
$prepend = $this->uri->getPath();
$path = $request->getUri()->getPath();

if (!array_key_exists($identifier, $this->alteredRequests)) {
if (substr($path, 0, strlen($prepend)) !== $prepend) {
$request = $request->withUri($request->getUri()
->withPath($this->uri->getPath().$request->getUri()->getPath())
);
$this->alteredRequests[$identifier] = $identifier;
->withPath($prepend.$path)
);
}

return $next($request);
Expand Down