| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use GuzzleHttp\Promise\PromiseInterface; |
| 5 |
use GuzzleHttp\Promise\PromisorInterface; |
| 6 |
use GuzzleHttp\Promise\EachPromise; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class CommandPool implements PromisorInterface |
| 15 |
{ |
| 16 |
|
| 17 |
private $each; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function __construct( |
| 46 |
AwsClientInterface $client, |
| 47 |
$commands, |
| 48 |
array $config = [] |
| 49 |
) { |
| 50 |
if (!isset($config['concurrency'])) { |
| 51 |
$config['concurrency'] = 25; |
| 52 |
} |
| 53 |
|
| 54 |
$before = $this->getBefore($config); |
| 55 |
$mapFn = function ($commands) use ($client, $before, $config) { |
| 56 |
foreach ($commands as $key => $command) { |
| 57 |
if (!($command instanceof CommandInterface)) { |
| 58 |
throw new \InvalidArgumentException('Each value yielded by ' |
| 59 |
. 'the iterator must be an Aws\CommandInterface.'); |
| 60 |
} |
| 61 |
if ($before) { |
| 62 |
$before($command, $key); |
| 63 |
} |
| 64 |
if (!empty($config['preserve_iterator_keys'])) { |
| 65 |
yield $key => $client->executeAsync($command); |
| 66 |
} else { |
| 67 |
yield $client->executeAsync($command); |
| 68 |
} |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
$this->each = new EachPromise($mapFn($commands), $config); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function promise(): PromiseInterface |
| 79 |
{ |
| 80 |
return $this->each->promise(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static function batch( |
| 95 |
AwsClientInterface $client, |
| 96 |
$commands, |
| 97 |
array $config = [] |
| 98 |
) { |
| 99 |
$results = []; |
| 100 |
self::cmpCallback($config, 'fulfilled', $results); |
| 101 |
self::cmpCallback($config, 'rejected', $results); |
| 102 |
|
| 103 |
return (new self($client, $commands, $config)) |
| 104 |
->promise() |
| 105 |
->then(static function () use (&$results) { |
| 106 |
ksort($results); |
| 107 |
return $results; |
| 108 |
}) |
| 109 |
->wait(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private function getBefore(array $config) |
| 116 |
{ |
| 117 |
if (!isset($config['before'])) { |
| 118 |
return null; |
| 119 |
} |
| 120 |
|
| 121 |
if (is_callable($config['before'])) { |
| 122 |
return $config['before']; |
| 123 |
} |
| 124 |
|
| 125 |
throw new \InvalidArgumentException('before must be callable'); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
private static function cmpCallback(array &$config, $name, array &$results) |
| 138 |
{ |
| 139 |
if (!isset($config[$name])) { |
| 140 |
$config[$name] = function ($v, $k) use (&$results) { |
| 141 |
$results[$k] = $v; |
| 142 |
}; |
| 143 |
} else { |
| 144 |
$currentFn = $config[$name]; |
| 145 |
$config[$name] = function ($v, $k) use (&$results, $currentFn) { |
| 146 |
$currentFn($v, $k); |
| 147 |
$results[$k] = $v; |
| 148 |
}; |
| 149 |
} |
| 150 |
} |
| 151 |
} |