PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / HandlerStack.php

HandlerStack.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/HandlerStack.php

239 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp;
4
5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
6 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
7 use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface;
8 /**
9 * Creates a composed Guzzle handler function by stacking middlewares on top of
10 * an HTTP handler function.
11 *
12 * @final
13 */
14 class HandlerStack
15 {
16 /**
17 * @var (callable(RequestInterface, array): PromiseInterface)|null
18 */
19 private $handler;
20 /**
21 * @var array{(callable(callable(RequestInterface, array): PromiseInterface): callable), (string|null)}[]
22 */
23 private $stack = [];
24 /**
25 * @var (callable(RequestInterface, array): PromiseInterface)|null
26 */
27 private $cached;
28 /**
29 * Creates a default handler stack that can be used by clients.
30 *
31 * The returned handler will wrap the provided handler or use the most
32 * appropriate default handler for your system. The returned HandlerStack has
33 * support for cookies, redirects, HTTP error exceptions, and preparing a body
34 * before sending.
35 *
36 * The returned handler stack can be passed to a client in the "handler"
37 * option.
38 *
39 * @param (callable(RequestInterface, array): PromiseInterface)|null $handler HTTP handler function to use with the stack. If no
40 * handler is provided, the best handler for your
41 * system will be utilized.
42 */
43 public static function create(?callable $handler = null) : self
44 {
45 $stack = new self($handler ?: Utils::chooseHandler());
46 $stack->push(Middleware::httpErrors(), 'http_errors');
47 $stack->push(Middleware::redirect(), 'allow_redirects');
48 $stack->push(Middleware::cookies(), 'cookies');
49 $stack->push(Middleware::prepareBody(), 'prepare_body');
50 return $stack;
51 }
52 /**
53 * @param (callable(RequestInterface, array): PromiseInterface)|null $handler Underlying HTTP handler.
54 */
55 public function __construct(?callable $handler = null)
56 {
57 $this->handler = $handler;
58 }
59 /**
60 * Invokes the handler stack as a composed handler
61 *
62 * @return ResponseInterface|PromiseInterface
63 */
64 public function __invoke(RequestInterface $request, array $options)
65 {
66 $handler = $this->resolve();
67 return $handler($request, $options);
68 }
69 /**
70 * Dumps a string representation of the stack.
71 *
72 * @return string
73 */
74 public function __toString()
75 {
76 $depth = 0;
77 $stack = [];
78 if ($this->handler !== null) {
79 $stack[] = '0) Handler: ' . $this->debugCallable($this->handler);
80 }
81 $result = '';
82 foreach (\array_reverse($this->stack) as $tuple) {
83 ++$depth;
84 $str = "{$depth}) Name: '{$tuple[1]}', ";
85 $str .= 'Function: ' . $this->debugCallable($tuple[0]);
86 $result = "> {$str}\n{$result}";
87 $stack[] = $str;
88 }
89 foreach (\array_keys($stack) as $k) {
90 $result .= "< {$stack[$k]}\n";
91 }
92 return $result;
93 }
94 /**
95 * Set the HTTP handler that actually returns a promise.
96 *
97 * @param callable(RequestInterface, array): PromiseInterface $handler Accepts a request and array of options and
98 * returns a Promise.
99 */
100 public function setHandler(callable $handler) : void
101 {
102 $this->handler = $handler;
103 $this->cached = null;
104 }
105 /**
106 * Returns true if the builder has a handler.
107 */
108 public function hasHandler() : bool
109 {
110 return $this->handler !== null;
111 }
112 /**
113 * Unshift a middleware to the bottom of the stack.
114 *
115 * @param callable(callable): callable $middleware Middleware function
116 * @param string $name Name to register for this middleware.
117 */
118 public function unshift(callable $middleware, ?string $name = null) : void
119 {
120 \array_unshift($this->stack, [$middleware, $name]);
121 $this->cached = null;
122 }
123 /**
124 * Push a middleware to the top of the stack.
125 *
126 * @param callable(callable): callable $middleware Middleware function
127 * @param string $name Name to register for this middleware.
128 */
129 public function push(callable $middleware, string $name = '') : void
130 {
131 $this->stack[] = [$middleware, $name];
132 $this->cached = null;
133 }
134 /**
135 * Add a middleware before another middleware by name.
136 *
137 * @param string $findName Middleware to find
138 * @param callable(callable): callable $middleware Middleware function
139 * @param string $withName Name to register for this middleware.
140 */
141 public function before(string $findName, callable $middleware, string $withName = '') : void
142 {
143 $this->splice($findName, $withName, $middleware, \true);
144 }
145 /**
146 * Add a middleware after another middleware by name.
147 *
148 * @param string $findName Middleware to find
149 * @param callable(callable): callable $middleware Middleware function
150 * @param string $withName Name to register for this middleware.
151 */
152 public function after(string $findName, callable $middleware, string $withName = '') : void
153 {
154 $this->splice($findName, $withName, $middleware, \false);
155 }
156 /**
157 * Remove a middleware by instance or name from the stack.
158 *
159 * @param callable|string $remove Middleware to remove by instance or name.
160 */
161 public function remove($remove) : void
162 {
163 if (!\is_string($remove) && !\is_callable($remove)) {
164 trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a callable or string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__);
165 }
166 $this->cached = null;
167 $idx = \is_callable($remove) ? 0 : 1;
168 $this->stack = \array_values(\array_filter($this->stack, static function ($tuple) use($idx, $remove) {
169 return $tuple[$idx] !== $remove;
170 }));
171 }
172 /**
173 * Compose the middleware and handler into a single callable function.
174 *
175 * @return callable(RequestInterface, array): PromiseInterface
176 */
177 public function resolve() : callable
178 {
179 if ($this->cached === null) {
180 if (($prev = $this->handler) === null) {
181 throw new \LogicException('No handler has been specified');
182 }
183 foreach (\array_reverse($this->stack) as $fn) {
184 /** @var callable(RequestInterface, array): PromiseInterface $prev */
185 $prev = $fn[0]($prev);
186 }
187 $this->cached = $prev;
188 }
189 return $this->cached;
190 }
191 private function findByName(string $name) : int
192 {
193 foreach ($this->stack as $k => $v) {
194 if ($v[1] === $name) {
195 return $k;
196 }
197 }
198 throw new \InvalidArgumentException("Middleware not found: {$name}");
199 }
200 /**
201 * Splices a function into the middleware list at a specific position.
202 */
203 private function splice(string $findName, string $withName, callable $middleware, bool $before) : void
204 {
205 $this->cached = null;
206 $idx = $this->findByName($findName);
207 $tuple = [$middleware, $withName];
208 if ($before) {
209 if ($idx === 0) {
210 \array_unshift($this->stack, $tuple);
211 } else {
212 $replacement = [$tuple, $this->stack[$idx]];
213 \array_splice($this->stack, $idx, 1, $replacement);
214 }
215 } elseif ($idx === \count($this->stack) - 1) {
216 $this->stack[] = $tuple;
217 } else {
218 $replacement = [$this->stack[$idx], $tuple];
219 \array_splice($this->stack, $idx, 1, $replacement);
220 }
221 }
222 /**
223 * Provides a debug string for a given callable.
224 *
225 * @param callable|string $fn Function to write as a string.
226 */
227 private function debugCallable($fn) : string
228 {
229 if (\is_string($fn)) {
230 return "callable({$fn})";
231 }
232 if (\is_array($fn)) {
233 return \is_string($fn[0]) ? "callable({$fn[0]}::{$fn[1]})" : "callable(['" . \get_class($fn[0]) . "', '{$fn[1]}'])";
234 }
235 /** @var object $fn */
236 return 'callable(' . \spl_object_hash($fn) . ')';
237 }
238 }
239