PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Handler / MockHandler.php

MockHandler.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at vendor_prefixed/guzzlehttp/guzzle/src/Handler/MockHandler.php

174 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Handler;
4
5 use YoastSEO_Vendor\GuzzleHttp\Exception\RequestException;
6 use YoastSEO_Vendor\GuzzleHttp\HandlerStack;
7 use YoastSEO_Vendor\GuzzleHttp\Promise as P;
8 use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface;
9 use YoastSEO_Vendor\GuzzleHttp\TransferStats;
10 use YoastSEO_Vendor\Psr\Http\Message\RequestInterface;
11 use YoastSEO_Vendor\Psr\Http\Message\ResponseInterface;
12 use YoastSEO_Vendor\Psr\Http\Message\StreamInterface;
13 /**
14 * Handler that returns responses or throw exceptions from a queue.
15 *
16 * @final
17 */
18 class MockHandler implements \Countable
19 {
20 /**
21 * @var array
22 */
23 private $queue = [];
24 /**
25 * @var RequestInterface|null
26 */
27 private $lastRequest;
28 /**
29 * @var array
30 */
31 private $lastOptions = [];
32 /**
33 * @var callable|null
34 */
35 private $onFulfilled;
36 /**
37 * @var callable|null
38 */
39 private $onRejected;
40 /**
41 * Creates a new MockHandler that uses the default handler stack list of
42 * middlewares.
43 *
44 * @param array|null $queue Array of responses, callables, or exceptions.
45 * @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled.
46 * @param callable|null $onRejected Callback to invoke when the return value is rejected.
47 */
48 public static function createWithMiddleware(?array $queue = null, ?callable $onFulfilled = null, ?callable $onRejected = null) : \YoastSEO_Vendor\GuzzleHttp\HandlerStack
49 {
50 return \YoastSEO_Vendor\GuzzleHttp\HandlerStack::create(new self($queue, $onFulfilled, $onRejected));
51 }
52 /**
53 * The passed in value must be an array of
54 * {@see ResponseInterface} objects, Exceptions,
55 * callables, or Promises.
56 *
57 * @param array<int, mixed>|null $queue The parameters to be passed to the append function, as an indexed array.
58 * @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled.
59 * @param callable|null $onRejected Callback to invoke when the return value is rejected.
60 */
61 public function __construct(?array $queue = null, ?callable $onFulfilled = null, ?callable $onRejected = null)
62 {
63 $this->onFulfilled = $onFulfilled;
64 $this->onRejected = $onRejected;
65 if ($queue) {
66 // array_values included for BC
67 $this->append(...\array_values($queue));
68 }
69 }
70 public function __invoke(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
71 {
72 if (!$this->queue) {
73 throw new \OutOfBoundsException('Mock queue is empty');
74 }
75 if (isset($options['delay']) && \is_numeric($options['delay'])) {
76 \usleep((int) $options['delay'] * 1000);
77 }
78 $this->lastRequest = $request;
79 $this->lastOptions = $options;
80 $response = \array_shift($this->queue);
81 if (isset($options['on_headers'])) {
82 if (!\is_callable($options['on_headers'])) {
83 throw new \InvalidArgumentException('on_headers must be callable');
84 }
85 try {
86 $options['on_headers']($response);
87 } catch (\Exception $e) {
88 $msg = 'An error was encountered during the on_headers event';
89 $response = new \YoastSEO_Vendor\GuzzleHttp\Exception\RequestException($msg, $request, $response, $e);
90 }
91 }
92 if (\is_callable($response)) {
93 $response = $response($request, $options);
94 }
95 $response = $response instanceof \Throwable ? \YoastSEO_Vendor\GuzzleHttp\Promise\Create::rejectionFor($response) : \YoastSEO_Vendor\GuzzleHttp\Promise\Create::promiseFor($response);
96 return $response->then(function (?\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $value) use($request, $options) {
97 $this->invokeStats($request, $options, $value);
98 if ($this->onFulfilled) {
99 ($this->onFulfilled)($value);
100 }
101 if ($value !== null && isset($options['sink'])) {
102 $contents = (string) $value->getBody();
103 $sink = $options['sink'];
104 if (\is_resource($sink)) {
105 \fwrite($sink, $contents);
106 } elseif (\is_string($sink)) {
107 \file_put_contents($sink, $contents);
108 } elseif ($sink instanceof \YoastSEO_Vendor\Psr\Http\Message\StreamInterface) {
109 $sink->write($contents);
110 }
111 }
112 return $value;
113 }, function ($reason) use($request, $options) {
114 $this->invokeStats($request, $options, null, $reason);
115 if ($this->onRejected) {
116 ($this->onRejected)($reason);
117 }
118 return \YoastSEO_Vendor\GuzzleHttp\Promise\Create::rejectionFor($reason);
119 });
120 }
121 /**
122 * Adds one or more variadic requests, exceptions, callables, or promises
123 * to the queue.
124 *
125 * @param mixed ...$values
126 */
127 public function append(...$values) : void
128 {
129 foreach ($values as $value) {
130 if ($value instanceof \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface || $value instanceof \Throwable || $value instanceof \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface || \is_callable($value)) {
131 $this->queue[] = $value;
132 } else {
133 throw new \TypeError('Expected a Response, Promise, Throwable or callable. Found ' . \get_debug_type($value));
134 }
135 }
136 }
137 /**
138 * Get the last received request.
139 */
140 public function getLastRequest() : ?\YoastSEO_Vendor\Psr\Http\Message\RequestInterface
141 {
142 return $this->lastRequest;
143 }
144 /**
145 * Get the last received request options.
146 */
147 public function getLastOptions() : array
148 {
149 return $this->lastOptions;
150 }
151 /**
152 * Returns the number of remaining items in the queue.
153 */
154 public function count() : int
155 {
156 return \count($this->queue);
157 }
158 public function reset() : void
159 {
160 $this->queue = [];
161 }
162 /**
163 * @param mixed $reason Promise or reason.
164 */
165 private function invokeStats(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options, ?\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response = null, $reason = null) : void
166 {
167 if (isset($options['on_stats'])) {
168 $transferTime = $options['transfer_time'] ?? 0;
169 $stats = new \YoastSEO_Vendor\GuzzleHttp\TransferStats($request, $response, $transferTime, $reason);
170 $options['on_stats']($stats);
171 }
172 }
173 }
174