public function current() { if ($this->isLastChunk) { return $this->applyChunkEncoding(''); } elseif (($current = $this->iterator->current()) === '') { return null; } elseif (is_string($current)) { return $this->applyChunkEncoding($current); } elseif ($current instanceof Promise) { $future = new Future(); $current->when(function ($error, $result) use($future) { if ($error) { $future->fail($error); } elseif (is_string($result)) { $future->succeed($this->applyChunkEncoding($result)); } else { $future->fail(new \DomainException(sprintf('Only string/Promise elements may be chunked; %s provided', gettype($result)))); } }); return $future->promise(); } else { // @TODO How to react to an invalid type returned from an iterator? return null; } }
/** * Return a key-value array of headers to add to the outbound request * * @return \Amp\Promise * @TODO */ public function getHeaders() { // @TODO Implement non-blocking php-uv header retrieval. // For now we'll just use the dumb blocking version. // v1.0.0 cannot be a thing until this is implemented. $promisor = new Future(); $this->getLength()->when(function ($error, $result) use($promisor) { if ($error) { $promisor->fail($error); } else { $promisor->succeed(['Content-Length' => $result]); } }); return $promisor->promise(); }
private function tunnelThroughProxy(Future $future, $socket, $authority) { if (empty(stream_context_get_options($socket)['artax*']['is_tunneled'])) { $futureTunnel = $this->tunneler->tunnel($socket, $authority); $futureTunnel->when(function ($error) use($future, $socket) { if ($error) { $future->fail($error); } else { $future->succeed($socket); } }); } else { $future->succeed($socket); } }
/** * Asynchronously request an HTTP resource * * @param mixed[string|\Amp\Artax\Request] An HTTP URI string or an \Amp\Artax\Request instance * @param array $options An array specifying options applicable only for this request * @return \Amp\Promise A promise to resolve the request at some point in the future */ public function request($uriOrRequest, array $options = []) { $promisor = new Future(); try { $cycle = new RequestCycle(); $cycle->futureResponse = $promisor; list($cycle->request, $cycle->uri) = $this->generateRequestFromUri($uriOrRequest); $cycle->options = $options ? array_merge($this->options, $options) : $this->options; $body = $cycle->request->getBody(); if ($body instanceof AggregateBody) { $this->processAggregateBody($cycle, $body); } else { $this->finalizeRequest($cycle); } } catch (\Exception $e) { $promisor->fail($e); } return $promisor->promise(); }
private function acceptNewTask(\Stackable $task) { $future = new Future($this->reactor); $promiseId = $this->nextId++; $this->queue[$promiseId] = [$future, $task]; $this->outstandingTaskCount++; if ($this->isPeriodWatcherEnabled === false) { $this->now = microtime(true); $this->reactor->enable($this->timeoutWatcher); $this->isPeriodWatcherEnabled = true; } if ($this->taskTimeout > -1) { $timeoutAt = $this->taskTimeout + $this->now; $this->promiseTimeoutMap[$promiseId] = $timeoutAt; } if ($this->availableWorkers) { $this->dequeueNextTask(); } elseif ($this->poolSize + $this->pendingWorkerCount < $this->poolSizeMax) { $this->spawnWorker(); } return $future->promise(); }
private function finalizeNewConnection(Future $future, $uri, $socket, $options) { if (--$this->pendingSockets[$uri] === 0) { unset($this->pendingSockets[$uri]); } $socketId = (int) $socket; $poolStruct = new SocketPoolStruct(); $poolStruct->id = $socketId; $poolStruct->uri = $uri; $poolStruct->resource = $socket; $poolStruct->isAvailable = false; $poolStruct->msIdleTimeout = $options[self::OP_MS_IDLE_TIMEOUT]; $this->sockets[$uri][$socketId] = $poolStruct; $this->socketIdUriMap[$socketId] = $uri; $future->succeed($poolStruct->resource); if (empty($this->queuedSocketRequests[$uri])) { unset($this->queuedSocketRequests[$uri]); } }
private function sumMultipartFieldLengths(array $fields) { $lengths = []; foreach ($fields as $field) { if (is_string($field)) { $lengths[] = strlen($field); } else { $lengths[] = $field->getLength(); } } $promisor = new Future(); \Amp\all($lengths)->when(function ($error, $result) use($promisor) { if ($error) { $promisor->fail($error); } else { $promisor->succeed(array_sum($result)); } }); return $promisor->promise(); }