| 1 |
<?php |
| 2 |
|
| 3 |
namespace GuzzleHttp; |
| 4 |
|
| 5 |
use GuzzleHttp\Promise as P; |
| 6 |
use GuzzleHttp\Promise\EachPromise; |
| 7 |
use GuzzleHttp\Promise\PromiseInterface; |
| 8 |
use GuzzleHttp\Promise\PromisorInterface; |
| 9 |
use Psr\Http\Message\RequestInterface; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class Pool implements PromisorInterface |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private $each; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function __construct(ClientInterface $client, $requests, array $config = []) |
| 42 |
{ |
| 43 |
if (!isset($config['concurrency'])) { |
| 44 |
$config['concurrency'] = 25; |
| 45 |
} |
| 46 |
|
| 47 |
if (isset($config['options'])) { |
| 48 |
$opts = $config['options']; |
| 49 |
unset($config['options']); |
| 50 |
} else { |
| 51 |
$opts = []; |
| 52 |
} |
| 53 |
|
| 54 |
if (!\is_iterable($requests)) { |
| 55 |
\trigger_deprecation( |
| 56 |
'guzzlehttp/guzzle', |
| 57 |
'7.11', |
| 58 |
'Passing a non-iterable request collection to %s::__construct() or %s::batch() is deprecated; guzzlehttp/guzzle 8.0 will require an iterable.', |
| 59 |
__CLASS__, |
| 60 |
__CLASS__ |
| 61 |
); |
| 62 |
|
| 63 |
$requests = [$requests]; |
| 64 |
} |
| 65 |
|
| 66 |
$iterable = P\Create::iterFor($requests); |
| 67 |
$requests = static function () use ($iterable, $client, $opts) { |
| 68 |
foreach ($iterable as $key => $rfn) { |
| 69 |
if ($rfn instanceof RequestInterface) { |
| 70 |
yield $key => $client->sendAsync($rfn, $opts); |
| 71 |
} elseif (\is_callable($rfn)) { |
| 72 |
yield $key => $rfn($opts); |
| 73 |
} else { |
| 74 |
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.'); |
| 75 |
} |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
$this->each = new EachPromise($requests(), $config); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function promise(): PromiseInterface |
| 86 |
{ |
| 87 |
return $this->each->promise(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public static function batch(ClientInterface $client, $requests, array $options = []): array |
| 109 |
{ |
| 110 |
$res = []; |
| 111 |
self::cmpCallback($options, 'fulfilled', $res); |
| 112 |
self::cmpCallback($options, 'rejected', $res); |
| 113 |
$pool = new static($client, $requests, $options); |
| 114 |
$pool->promise()->wait(); |
| 115 |
\ksort($res); |
| 116 |
|
| 117 |
return $res; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private static function cmpCallback(array &$options, string $name, array &$results): void |
| 124 |
{ |
| 125 |
if (!isset($options[$name])) { |
| 126 |
$options[$name] = static function ($v, $k) use (&$results) { |
| 127 |
$results[$k] = $v; |
| 128 |
}; |
| 129 |
} else { |
| 130 |
$currentFn = $options[$name]; |
| 131 |
$options[$name] = static function ($v, $k) use (&$results, $currentFn) { |
| 132 |
$currentFn($v, $k); |
| 133 |
$results[$k] = $v; |
| 134 |
}; |
| 135 |
} |
| 136 |
} |
| 137 |
} |