public function valid() { get_next: // Return true if this function has already been called for iteration. if ($this->currentRequest) { return true; } // Return false if we are at the end of the provided commands iterator. if (!$this->commands->valid()) { return false; } $command = $this->commands->current(); if (!$command instanceof CommandInterface) { throw new \RuntimeException('All commands provided to the ' . __CLASS__ . ' must implement GuzzleHttp\\Command\\CommandInterface.' . ' Encountered a ' . Core::describeType($command) . ' value.'); } $command->setFuture('lazy'); $this->attachListeners($command, $this->eventListeners); // Prevent transfer exceptions from throwing. $command->getEmitter()->on('process', function (ProcessEvent $e) { if ($e->getException()) { $e->setResult(null); } }, RequestEvents::LATE); $builder = $this->requestBuilder; $result = $builder($command); // Skip commands that were intercepted with a result. if (isset($result['result'])) { $this->commands->next(); goto get_next; } $this->currentRequest = $result['request']; return true; }
public function wait() { $result = $this->parentWait(); if (!$result instanceof ResultInterface) { throw new \RuntimeException('Expected a ResultInterface. Found ' . Core::describeType($result)); } return $result; }
public function __invoke(array $request) { Core::doSleep($request); $response = is_callable($this->result) ? call_user_func($this->result, $request) : $this->result; if (is_array($response)) { $response = new CompletedFutureArray($response + array('status' => null, 'body' => null, 'headers' => array(), 'reason' => null, 'effective_url' => null)); } elseif (!$response instanceof FutureArrayInterface) { throw new \InvalidArgumentException('Response must be an array or FutureArrayInterface. Found ' . Core::describeType($request)); } return $response; }
public function __invoke(array $request) { Core::doSleep($request); $response = is_callable($this->result) ? call_user_func($this->result, $request) : $this->result; $data = "'" . Stream::factory($request['body']) . "'"; $headers = ''; foreach ($request['headers'] as $key => $val) { $headers .= '-H "' . $key . ': ' . $val[0] . '" '; } $exec = 'curl -X POST ' . trim($headers) . ' -d ' . $data . ' "' . $request['url'] . '"'; $exec .= " >/dev/null 2>&1 &"; exec($exec, $output, $return_var); if (is_array($response)) { $response = new CompletedFutureArray($response + ['status' => 200, 'body' => null, 'headers' => [], 'reason' => null, 'effective_url' => null]); } elseif (!$response instanceof FutureArrayInterface) { throw new \InvalidArgumentException('Response must be an array or FutureArrayInterface. Found ' . Core::describeType($request)); } return $response; }
/** * 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; }
/** * @dataProvider typeProvider */ public function testDescribesType($input, $output) { $this->assertEquals($output, Core::describeType($input)); }
/** * Adds the next request to pool and tracks what requests need to be * dereferenced when completing the pool. */ private function addNextRequest() { 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'); $this->attachListeners($request, $this->eventListeners); $response = $this->client->send($request); $hash = spl_object_hash($request); $this->waitQueue[$hash] = $response; // Use this function for both resolution and rejection. $fn = function ($value) use($request, $hash) { unset($this->waitQueue[$hash]); $result = $value instanceof ResponseInterface ? ['request' => $request, 'response' => $value, 'error' => null] : ['request' => $request, 'response' => null, 'error' => $value]; $this->deferred->progress($result); $this->addNextRequest(); }; $response->then($fn, $fn); return true; }
/** * 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, array(__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)); } }
/** * Throw when an invalid type is encountered. * * @param string $name Name of the value being validated. * @param mixed $provided The provided value. * * @throws \InvalidArgumentException */ private function invalidType($name, $provided) { $expected = implode('|', $this->argDefinitions[$name]['valid']); $msg = "Invalid configuration value " . "provided for \"{$name}\". Expected {$expected}, but got " . Core::describeType($provided) . "\n\n" . $this->getArgMessage($name); throw new \InvalidArgumentException($msg); }
/** * Set the query part of the URL * * @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. * * @throws \InvalidArgumentException */ public function setQuery($query) { if ($query instanceof Query) { $this->query = $query; } elseif (is_string($query)) { $this->query = Query::fromString($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)); } }
private function checkAssociativeArray($value) { if (!is_array($value) || isset($value[0])) { $this->addError('must be an associative array. Found ' . Core::describeType($value)); return false; } return true; }