Пример #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;
 }
Пример #2
0
 /**
  * Creates a Guzzle request object using a ring request array.
  *
  * @param array $request Ring request
  *
  * @return Request
  * @throws \InvalidArgumentException for incomplete requests.
  */
 public static function fromRingRequest(array $request)
 {
     $options = [];
     if (isset($request['version'])) {
         $options['protocol_version'] = $request['version'];
     }
     if (!isset($request['http_method'])) {
         throw new \InvalidArgumentException('No http_method');
     }
     return new Request($request['http_method'], Core::url($request), isset($request['headers']) ? $request['headers'] : [], isset($request['body']) ? Stream::factory($request['body']) : null, $options);
 }
Пример #3
0
 private function configureDefaults($config)
 {
     if (!isset($config['defaults'])) {
         $this->defaults = $this->getDefaultOptions();
     } else {
         $this->defaults = array_replace($this->getDefaultOptions(), $config['defaults']);
     }
     // Add the default user-agent header
     if (!isset($this->defaults['headers'])) {
         $this->defaults['headers'] = ['User-Agent' => Utils::getDefaultUserAgent()];
     } elseif (!Core::hasHeader($this->defaults, 'User-Agent')) {
         // Add the User-Agent header if one was not already set
         $this->defaults['headers']['User-Agent'] = Utils::getDefaultUserAgent();
     }
 }
Пример #4
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));
     }
 }