/** * @param array $args * @param callable $transform * @return \Amp\Promise */ public function send(array $args, callable $transform = null) { $promisor = new \Amp\Deferred(); $this->promisors[] = $promisor; $this->getConnection()->send($args); return \Amp\pipe($promisor->promise(), function ($response) { return $this->parser->parse($response); }); }
public function __construct(Promise $promise) { $promise->watch(function ($data) { foreach ($this->watchers as list($func, $cbData)) { $func($data, $cbData); } }); parent::__construct($promise); // DO NOT MOVE - preserve order in which things happen $when = function ($e, $bool) use(&$continue) { $continue = $bool; }; $promise->when(function () use(&$continue, $when) { $this->valid()->when($when); while ($continue) { $string[] = $this->consume(); $this->valid()->when($when); } $this->valid()->when(function ($ex) use(&$e) { $e = $ex; }); if (isset($string)) { if (isset($string[1])) { $string = implode($string); } else { $string = $string[0]; } // way to restart, so that even after the success, the valid() / consume() API will still work if (!$e) { $result = $this->consume(); // consume the final result } $deferred = new \Amp\Deferred(); parent::__construct($deferred->promise()); $deferred->update($string); if ($e) { $deferred->fail($e); } else { $deferred->succeed($result); } } else { $string = ""; } $this->string = $string; $this->error = $e; foreach ($this->whens as list($when, $data)) { $when($e, $string, $data); } $this->whens = $this->watchers = []; }); }
function __watchCrypto($method, $socket) { $result = \stream_socket_enable_crypto($socket, $enable = true, $method); if ($result === true) { return new Success($socket); } elseif ($result === false) { return new Failure(new CryptoException("Crypto negotiation failed: " . \error_get_last()["message"])); } else { $promisor = new \Amp\Deferred(); $cbData = [$promisor, $method]; \Amp\onReadable($socket, '\\Amp\\Socket\\__onCryptoWatchReadability', $options = ["cb_data" => $cbData]); return $promisor->promise(); } }
/** * Execute a previously prepared statement asynchronously * * @param string $statementName * @param array $params * @return \Amp\Promise */ public function execute(string $statementName, array $params) { if ($this->queryCacheSize > $this->maxOutstandingQueries) { return new \Amp\Failure(new \RuntimeException("Too busy")); } $deferred = new \Amp\Deferred(); $this->queryCache[] = [self::$OP_QUERY_PARAMS, [$statementName, $params], $deferred]; if (!$this->queryCacheSize++) { $sendResult = \pg_send_execute($this->db, $statementName, $params); $this->processSendResult($sendResult); } return $deferred->promise(); }