Exemple #1
0
 /**
  * Sends multiple requests in parallel and returns an array of responses
  * and exceptions that uses the same ordering as the provided requests.
  *
  * IMPORTANT: This method keeps every request and response in memory, and
  * as such, is NOT recommended when sending a large number or an
  * indeterminate number of requests concurrently.
  *
  * @param ClientInterface $client   Client used to send the requests
  * @param array|\Iterator $requests Requests to send in parallel
  * @param array           $options  Passes through the options available in
  *                                  {@see GuzzleHttp5Legacy\Pool::__construct}
  *
  * @return BatchResults Returns a container for the results.
  * @throws \InvalidArgumentException if the event format is incorrect.
  */
 public static function batch(ClientInterface $client, $requests, array $options = [])
 {
     $hash = new \SplObjectStorage();
     foreach ($requests as $request) {
         $hash->attach($request);
     }
     // In addition to the normally run events when requests complete, add
     // and event to continuously track the results of transfers in the hash.
     (new self($client, $requests, RequestEvents::convertEventArray($options, ['end'], ['priority' => RequestEvents::LATE, 'fn' => function (EndEvent $e) use($hash) {
         $hash[$e->getRequest()] = $e->getException() ? $e->getException() : $e->getResponse();
     }])))->wait();
     return new BatchResults($hash);
 }
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testValidatesEventFormat()
 {
     RequestEvents::convertEventArray(['foo' => false], ['foo'], []);
 }