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 / Pool.php

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

117 lines 4.7 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 as P;
6 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\EachPromise;
7 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
8 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromisorInterface;
9 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
10 /**
11 * Sends an iterator of requests concurrently using a capped pool size.
12 *
13 * The pool will read from an iterator until it is cancelled or until the
14 * iterator is consumed. When a request is yielded, the request is sent after
15 * applying the "request_options" request options (if provided in the ctor).
16 *
17 * When a function is yielded by the iterator, the function is provided the
18 * "request_options" array that should be merged on top of any existing
19 * options, and the function MUST then return a wait-able promise.
20 *
21 * @final
22 */
23 class Pool implements PromisorInterface
24 {
25 /**
26 * @var EachPromise
27 */
28 private $each;
29 /**
30 * @param ClientInterface $client Client used to send the requests.
31 * @param array|\Iterator $requests Requests or functions that return
32 * requests to send concurrently.
33 * @param array $config Associative array of options
34 * - concurrency: (int) Maximum number of requests to send concurrently
35 * - options: Array of request options to apply to each request.
36 * - fulfilled: (callable) Function to invoke when a request completes.
37 * - rejected: (callable) Function to invoke when a request is rejected.
38 */
39 public function __construct(ClientInterface $client, $requests, array $config = [])
40 {
41 if (!isset($config['concurrency'])) {
42 $config['concurrency'] = 25;
43 }
44 if (isset($config['options'])) {
45 $opts = $config['options'];
46 unset($config['options']);
47 } else {
48 $opts = [];
49 }
50 $iterable = P\Create::iterFor($requests);
51 $requests = static function () use($iterable, $client, $opts) {
52 foreach ($iterable as $key => $rfn) {
53 if ($rfn instanceof RequestInterface) {
54 (yield $key => $client->sendAsync($rfn, $opts));
55 } elseif (\is_callable($rfn)) {
56 (yield $key => $rfn($opts));
57 } else {
58 throw new \InvalidArgumentException('Each value yielded by the iterator must be a Psr7\\Http\\Message\\RequestInterface or a callable that returns a promise that fulfills with a Psr7\\Message\\Http\\ResponseInterface object.');
59 }
60 }
61 };
62 $this->each = new EachPromise($requests(), $config);
63 }
64 /**
65 * Get promise
66 */
67 public function promise() : PromiseInterface
68 {
69 return $this->each->promise();
70 }
71 /**
72 * Sends multiple requests concurrently and returns an array of responses
73 * and exceptions that uses the same ordering as the provided requests.
74 *
75 * IMPORTANT: This method keeps every request and response in memory, and
76 * as such, is NOT recommended when sending a large number or an
77 * indeterminate number of requests concurrently.
78 *
79 * @param ClientInterface $client Client used to send the requests
80 * @param array|\Iterator $requests Requests to send concurrently.
81 * @param array $options Passes through the options available in
82 * {@see Pool::__construct}
83 *
84 * @return array Returns an array containing the response or an exception
85 * in the same order that the requests were sent.
86 *
87 * @throws \InvalidArgumentException if the event format is incorrect.
88 */
89 public static function batch(ClientInterface $client, $requests, array $options = []) : array
90 {
91 $res = [];
92 self::cmpCallback($options, 'fulfilled', $res);
93 self::cmpCallback($options, 'rejected', $res);
94 $pool = new static($client, $requests, $options);
95 $pool->promise()->wait();
96 \ksort($res);
97 return $res;
98 }
99 /**
100 * Execute callback(s)
101 */
102 private static function cmpCallback(array &$options, string $name, array &$results) : void
103 {
104 if (!isset($options[$name])) {
105 $options[$name] = static function ($v, $k) use(&$results) {
106 $results[$k] = $v;
107 };
108 } else {
109 $currentFn = $options[$name];
110 $options[$name] = static function ($v, $k) use(&$results, $currentFn) {
111 $currentFn($v, $k);
112 $results[$k] = $v;
113 };
114 }
115 }
116 }
117