PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / php-http / client-common / src / Plugin / AddPathPlugin.php
ameliabooking / vendor / php-http / client-common / src / Plugin Last commit date
AddHostPlugin.php 3 years ago AddPathPlugin.php 3 years ago AuthenticationPlugin.php 3 years ago BaseUriPlugin.php 3 years ago ContentLengthPlugin.php 3 years ago ContentTypePlugin.php 3 years ago CookiePlugin.php 3 years ago DecoderPlugin.php 3 years ago ErrorPlugin.php 3 years ago HeaderAppendPlugin.php 3 years ago HeaderDefaultsPlugin.php 3 years ago HeaderRemovePlugin.php 3 years ago HeaderSetPlugin.php 3 years ago HistoryPlugin.php 3 years ago Journal.php 3 years ago QueryDefaultsPlugin.php 3 years ago RedirectPlugin.php 3 years ago RequestMatcherPlugin.php 3 years ago RetryPlugin.php 3 years ago VersionBridgePlugin.php 3 years ago
AddPathPlugin.php
86 lines
1 <?php
2
3 namespace AmeliaHttp\Client\Common\Plugin;
4
5 use AmeliaHttp\Client\Common\Plugin;
6 use AmeliaPsr\Http\Message\RequestInterface;
7 use AmeliaPsr\Http\Message\UriInterface;
8
9 /**
10 * Prepend a base path to the request URI. Useful for base API URLs like http://domain.com/api.
11 *
12 * @author Sullivan Senechal <soullivaneuh@gmail.com>
13 */
14 final class AddPathPlugin implements Plugin
15 {
16 /**
17 * @var UriInterface
18 */
19 private $uri;
20
21 /**
22 * Stores identifiers of the already altered requests.
23 *
24 * @var array
25 */
26 private $alteredRequests = [];
27
28 /**
29 * @param UriInterface $uri
30 */
31 public function __construct(UriInterface $uri)
32 {
33 if ('' === $uri->getPath()) {
34 throw new \LogicException('URI path cannot be empty');
35 }
36
37 if ('/' === substr($uri->getPath(), -1)) {
38 $uri = $uri->withPath(rtrim($uri->getPath(), '/'));
39 }
40
41 $this->uri = $uri;
42 }
43
44 /**
45 * Adds a prefix in the beginning of the URL's path.
46 *
47 * The prefix is not added if that prefix is already on the URL's path. This will fail on the edge
48 * case of the prefix being repeated, for example if `https://example.com/api/api/foo` is a valid
49 * URL on the server and the configured prefix is `/api`.
50 *
51 * We looked at other solutions, but they are all much more complicated, while still having edge
52 * cases:
53 * - Doing an spl_object_hash on `$first` will lead to collisions over time because over time the
54 * hash can collide.
55 * - Have the PluginClient provide a magic header to identify the request chain and only apply
56 * this plugin once.
57 *
58 * There are 2 reasons for the AddPathPlugin to be executed twice on the same request:
59 * - A plugin can restart the chain by calling `$first`, e.g. redirect
60 * - A plugin can call `$next` more than once, e.g. retry
61 *
62 * Depending on the scenario, the path should or should not be added. E.g. `$first` could
63 * be called after a redirect response from the server. The server likely already has the
64 * correct path.
65 *
66 * No solution fits all use cases. This implementation will work fine for the common use cases.
67 * If you have a specific situation where this is not the right thing, you can build a custom plugin
68 * that does exactly what you need.
69 *
70 * {@inheritdoc}
71 */
72 public function handleRequest(RequestInterface $request, callable $next, callable $first)
73 {
74 $prepend = $this->uri->getPath();
75 $path = $request->getUri()->getPath();
76
77 if (substr($path, 0, strlen($prepend)) !== $prepend) {
78 $request = $request->withUri($request->getUri()
79 ->withPath($prepend.$path)
80 );
81 }
82
83 return $next($request);
84 }
85 }
86