getBefore($config); $mapFn = function ($commands) use ($client, $before, $config) { foreach ($commands as $key => $command) { if (!($command instanceof CommandInterface)) { throw new \InvalidArgumentException('Each value yielded by ' . 'the iterator must be an Aws\CommandInterface.'); } if ($before) { $before($command, $key); } if (!empty($config['preserve_iterator_keys'])) { yield $key => $client->executeAsync($command); } else { yield $client->executeAsync($command); } } }; $this->each = new EachPromise($mapFn($commands), $config); } /** * @return \GuzzleHttp\Promise\PromiseInterface */ public function promise() { return $this->each->promise(); } /** * Executes a pool synchronously and aggregates the results of the pool * into an indexed array in the same order as the passed in array. * * @param AwsClientInterface $client Client used to execute commands. * @param mixed $commands Iterable that yields commands. * @param array $config Configuration options. * * @return array * @see \Aws\CommandPool::__construct for available configuration options. */ public static function batch( AwsClientInterface $client, $commands, array $config = [] ) { $results = []; self::cmpCallback($config, 'fulfilled', $results); self::cmpCallback($config, 'rejected', $results); return (new self($client, $commands, $config)) ->promise() ->then(static function () use (&$results) { ksort($results); return $results; }) ->wait(); } /** * @return callable */ private function getBefore(array $config) { if (!isset($config['before'])) { return null; } if (is_callable($config['before'])) { return $config['before']; } throw new \InvalidArgumentException('before must be callable'); } /** * Adds an onFulfilled or onRejected callback that aggregates results into * an array. If a callback is already present, it is replaced with the * composed function. * * @param array $config * @param $name * @param array $results */ private static function cmpCallback(array &$config, $name, array &$results) { if (!isset($config[$name])) { $config[$name] = function ($v, $k) use (&$results) { $results[$k] = $v; }; } else { $currentFn = $config[$name]; $config[$name] = function ($v, $k) use (&$results, $currentFn) { $currentFn($v, $k); $results[$k] = $v; }; } } }