Ejemplo n.º 1
0
 /**
  * Adds the next request to pool and tracks what requests need to be
  * dereferenced when completing the pool.
  */
 private function addNextRequest()
 {
     add_next:
     if ($this->isRealized || !$this->iter || !$this->iter->valid()) {
         return false;
     }
     $request = $this->iter->current();
     $this->iter->next();
     if (!$request instanceof RequestInterface) {
         throw new \InvalidArgumentException(sprintf('All requests in the provided iterator must implement ' . 'RequestInterface. Found %s', Core::describeType($request)));
     }
     // Be sure to use "lazy" futures, meaning they do not send right away.
     $request->getConfig()->set('future', 'lazy');
     $hash = spl_object_hash($request);
     $this->attachListeners($request, $this->eventListeners);
     $request->getEmitter()->on('before', [$this, '_trackRetries'], RequestEvents::EARLY);
     $response = $this->client->send($request);
     $this->waitQueue[$hash] = $response;
     $promise = $response->promise();
     // Don't recursively call itself for completed or rejected responses.
     if ($promise instanceof FulfilledPromise || $promise instanceof RejectedPromise) {
         try {
             $this->finishResponse($request, $response->wait(), $hash);
         } catch (\Exception $e) {
             $this->finishResponse($request, $e, $hash);
         }
         goto add_next;
     }
     // Use this function for both resolution and rejection.
     $thenFn = function ($value) use($request, $hash) {
         $this->finishResponse($request, $value, $hash);
         if (!$request->getConfig()->get('_pool_retries')) {
             $this->addNextRequests();
         }
     };
     $promise->then($thenFn, $thenFn);
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Set the query part of the URL.
  *
  * You may provide a query string as a string and pass $rawString as true
  * to provide a query string that is not parsed until a call to getQuery()
  * is made. Setting a raw query string will still encode invalid characters
  * in a query string.
  *
  * @param Query|string|array $query Query string value to set. Can
  *     be a string that will be parsed into a Query object, an array
  *     of key value pairs, or a Query object.
  * @param bool $rawString Set to true when providing a raw query string.
  *
  * @throws \InvalidArgumentException
  */
 public function setQuery($query, $rawString = false)
 {
     if ($query instanceof Query) {
         $this->query = $query;
     } elseif (is_string($query)) {
         if (!$rawString) {
             $this->query = Query::fromString($query);
         } else {
             // Ensure the query does not have illegal characters.
             $this->query = preg_replace_callback(self::$queryPattern, [__CLASS__, 'encodeMatch'], $query);
         }
     } elseif (is_array($query)) {
         $this->query = new Query($query);
     } else {
         throw new \InvalidArgumentException('Query must be a Query, ' . 'array, or string. Got ' . Core::describeType($query));
     }
 }