本文整理汇总了PHP中React\Promise\Deferred类的典型用法代码示例。如果您正苦于以下问题:PHP Deferred类的具体用法?PHP Deferred怎么用?PHP Deferred使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Deferred类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: queryDnsSeeds
/**
* Connect to $numSeeds DNS seeds
*
* @param int $numSeeds
* @return \React\Promise\Promise|\React\Promise\PromiseInterface
*/
public function queryDnsSeeds($numSeeds = 1)
{
$peerList = new Deferred();
// Take $numSeeds
$seedHosts = self::dnsSeedHosts();
$seeds = array_slice($seedHosts, 0, min($numSeeds, count($seedHosts)));
// Connect to $numSeeds peers
/** @var Peer[] $vNetAddr */
$vNetAddr = [];
foreach ($seeds as $seed) {
echo " [ query DNS seed: " . $seed . " ] \n";
$this->dns->resolve($seed)->then(function ($ipList) use(&$vNetAddr, $peerList, &$numSeeds) {
$vNetAddr[] = $ipList;
if (count($vNetAddr) == $numSeeds) {
$peerList->resolve($vNetAddr);
}
});
}
// Compile the list of lists of peers into $this->knownAddresses
return $peerList->promise()->then(function (array $vPeerVAddrs) {
shuffle($vPeerVAddrs);
/** @var NetworkAddressInterface[] $addresses */
$addresses = [];
array_map(function (array $value) use(&$addresses) {
foreach ($value as $ip) {
$addresses[] = new NetworkAddress(Buffer::hex('01', 8), $ip, 8333);
}
}, $vPeerVAddrs);
$this->knownAddresses = array_merge($this->knownAddresses, $addresses);
return $this;
});
}
示例2: loadEtcResolvConf
public function loadEtcResolvConf($filename)
{
if (!file_exists($filename)) {
return When::reject(new \InvalidArgumentException("The filename for /etc/resolv.conf given does not exist: {$filename}"));
}
try {
$deferred = new Deferred();
$fd = fopen($filename, 'r');
stream_set_blocking($fd, 0);
$contents = '';
$stream = new Stream($fd, $this->loop);
$stream->on('data', function ($data) use(&$contents) {
$contents .= $data;
});
$stream->on('end', function () use(&$contents, $deferred) {
$deferred->resolve($contents);
});
$stream->on('error', function ($error) use($deferred) {
$deferred->reject($error);
});
return $deferred->promise();
} catch (\Exception $e) {
return When::reject($e);
}
}
示例3: createInternalAsync
protected function createInternalAsync($host, $docroot, $router)
{
$deferred = new Deferred();
$process = new BuiltinServer($host, $docroot, $router, $this->php);
$process->start($this->loop);
$process->on('exit', function ($code) use($deferred) {
$this->stderr->write("Process exit with code {$code}\n");
$deferred->reject();
});
$process->stdin->close();
$process->stdout->close();
$process->stderr->on('data', function ($output) use($deferred) {
$this->stderr->write($output);
$deferred->reject();
});
$timer = new Deferred();
$this->loop->addTimer(0.05, function () use($timer, $process) {
if (DIRECTORY_SEPARATOR === '\\') {
// Pipes opened by proc_open() can break stream_select() loop in Windows.
// This fix might do the trick...
$process->stderr->close();
}
$timer->resolve($process);
});
return \React\Promise\race([$deferred->promise(), $timer->promise()])->then(null, function () use($process) {
$process->terminate();
return new RejectedPromise();
});
}
示例4: testThrowsWhenAccessingInvalidProperty
/**
* @expectedException \RuntimeException
*/
public function testThrowsWhenAccessingInvalidProperty()
{
$deferred = new Deferred();
$f = new FutureArray($deferred->promise(), function () {
});
$f->foo;
}
示例5: connect
public function connect($timeout = 5)
{
if ($this->connectDeferred) {
return $this->connectDeferred->promise();
}
$this->connectionStatus = 'connecting';
$promise = new Deferred();
$client = $this;
$loop = $this->loop;
$this->connectDeferred = $promise;
$this->connectDeferred->then(function () use($client) {
$client->setConnectionStatus('connected');
});
$timer = $this->loop->addTimer($timeout, function () use($client, $promise) {
$promise->reject(new ConnectionException('Connection timeout'));
$client->resetConnectDeferred();
$client->setConnectionStatus('not-connected');
});
$this->on('connect', function ($client) use($timer, $promise) {
$timer->cancel();
$promise->resolve($client);
});
$frame = $this->packageCreator->connect($this->options['vhost'], $this->options['login'], $this->options['passcode']);
$this->output->sendFrame($frame);
return $this->connectDeferred->promise();
}
示例6: sendRequest
/**
* @param Request $request
* @return \React\Promise\Promise|\React\Promise\PromiseInterface
*/
public function sendRequest(Request $request)
{
$result = new Deferred();
$this->deferred[$request->getId()] = $result;
$this->sendData($request->write());
return $result->promise();
}
示例7: show
/**
* @returns \React\Promise\Promise
*/
public function show()
{
$deferred = new Deferred();
$this->io->out('hello');
$deferred->reject(false);
return $deferred->promise();
}
示例8: doQuery
public function doQuery($nameserver, $queryData, $name)
{
$that = $this;
$parser = $this->parser;
$loop = $this->loop;
$deferred = new Deferred(function ($_, $reject) use(&$conn, &$timer, $name) {
$conn->close();
$timer->cancel();
$reject(new \RuntimeException(sprintf("DNS query for %s cancelled", $name)));
});
$timer = $this->loop->addTimer($this->timeout, function () use(&$conn, $name, $deferred) {
$conn->close();
$deferred->reject(new TimeoutException(sprintf("DNS query for %s timed out", $name)));
});
$conn = $this->factory->createSender();
$conn->on('message', function ($data) use($conn, $parser, $deferred, $timer) {
$response = new Message();
$responseReady = $parser->parseChunk($data, $response);
$conn->close();
$timer->cancel();
if (!$responseReady) {
$deferred->reject(new BadServerException('Invalid response received'));
return;
}
if ($response->header->isTruncated()) {
$deferred->reject(new BadServerException('The server set the truncated bit although we issued a TCP request'));
return;
}
$deferred->resolve($response);
});
$conn->send($queryData, $nameserver);
return $deferred->promise();
}
示例9: collectEvents
private function collectEvents($command, $expectedEndEvent)
{
$req = $this->client->createAction($command);
$ret = $this->client->request($req);
$id = $req->getActionId();
$deferred = new Deferred();
// collect all intermediary channel events with this action ID
$collected = array();
$collector = function (Event $event) use($id, &$collected, $deferred, $expectedEndEvent) {
if ($event->getActionId() === $id) {
$collected[] = $event;
if ($event->getName() === $expectedEndEvent) {
$deferred->resolve($collected);
}
}
};
$this->client->on('event', $collector);
// unregister collector if client fails
$client = $this->client;
$unregister = function () use($client, $collector) {
$client->removeListener('event', $collector);
};
$ret->then(null, $unregister);
// stop waiting for events
$deferred->promise()->then($unregister);
return $ret->then(function (Response $response) use($deferred) {
// final result has been received => merge all intermediary channel events
return $deferred->promise()->then(function ($collected) use($response) {
$last = array_pop($collected);
return new Collection($response, $collected, $last);
});
});
}
示例10: search
public function search($searchTarget = 'ssdp:all', $mx = 2)
{
$data = "M-SEARCH * HTTP/1.1\r\n";
$data .= "HOST: " . self::ADDRESS . "\r\n";
$data .= "MAN: \"ssdp:discover\"\r\n";
$data .= "MX: {$mx}\r\n";
$data .= "ST: {$searchTarget}\r\n";
$data .= "\r\n";
$socket = $this->multicast->createSender();
// TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
$timer = $this->loop->addTimer($mx, function () use($socket, &$deferred) {
$deferred->resolve();
$socket->close();
});
$deferred = new Deferred(function () use($socket, &$timer) {
// canceling resulting promise cancels timer and closes socket
$timer->cancel();
$socket->close();
throw new RuntimeException('Cancelled');
});
$that = $this;
$socket->on('message', function ($data, $remote) use($deferred, $that) {
$message = $that->parseMessage($data, $remote);
$deferred->progress($message);
});
$socket->send($data, self::ADDRESS);
return $deferred->promise();
}
示例11: sendAsyncRequest
/**
* {@inheritdoc}
*/
public function sendAsyncRequest(RequestInterface $request)
{
$reactRequest = $this->buildReactRequest($request);
$deferred = new Deferred();
$reactRequest->on('error', function (\Exception $error) use($deferred, $request) {
$deferred->reject(new RequestException($error->getMessage(), $request, $error));
});
$reactRequest->on('response', function (ReactResponse $reactResponse = null) use($deferred, $reactRequest, $request) {
$bodyStream = $this->streamFactory->createStream();
$reactResponse->on('data', function ($data) use(&$bodyStream) {
$bodyStream->write((string) $data);
});
$reactResponse->on('end', function (\Exception $error = null) use($deferred, $request, $reactResponse, &$bodyStream) {
$response = $this->buildResponse($reactResponse, $bodyStream);
if (null !== $error) {
$deferred->reject(new HttpException($error->getMessage(), $request, $response, $error));
} else {
$deferred->resolve($response);
}
});
});
$reactRequest->end((string) $request->getBody());
$promise = new Promise($deferred->promise());
$promise->setLoop($this->loop);
return $promise;
}
示例12: sendAsyncRequest
/**
* {@inheritdoc}
*/
public function sendAsyncRequest(RequestInterface $request)
{
$requestStream = $this->buildReactRequest($request);
$deferred = new Deferred();
$requestStream->on('error', function (\Exception $error) use($deferred, $request) {
$deferred->reject(new RequestException($error->getMessage(), $request, $error));
});
$requestStream->on('response', function (ReactResponse $response = null) use($deferred, $requestStream, $request) {
$bodyStream = null;
$response->on('data', function ($data) use(&$bodyStream) {
if ($data instanceof StreamInterface) {
$bodyStream = $data;
} else {
$bodyStream->write($data);
}
});
$response->on('end', function (\Exception $error = null) use($deferred, $request, $response, &$bodyStream) {
$bodyStream->rewind();
$psr7Response = $this->messageFactory->createResponse($response->getCode(), $response->getReasonPhrase(), $response->getHeaders(), $bodyStream, $response->getVersion());
if (null !== $error) {
$deferred->reject(new HttpException($error->getMessage(), $request, $psr7Response, $error));
} else {
$deferred->resolve($psr7Response);
}
});
});
$requestStream->end((string) $request->getBody());
$promise = new ReactPromiseAdapter($deferred->promise());
$this->loop->run();
return $promise;
}
示例13: createFuture
public static function createFuture(callable $wait, callable $cancel = null)
{
$deferred = new Deferred();
return new FutureResponse($deferred->promise(), function () use($deferred, $wait) {
$deferred->resolve($wait());
}, $cancel);
}
示例14: go
/** @internal */
public function go(Process $process)
{
$this->deferred = $deferred = new Deferred();
$this->process = $process;
$buffered = null;
$process->stdout->on('data', function ($data) use(&$buffered) {
if ($data !== '') {
$buffered .= $data;
}
});
$process->on('exit', function ($code) use($deferred) {
if ($code !== 0) {
$deferred->reject($code);
} else {
$deferred->resolve();
}
});
$that = $this;
$this->promise = $deferred->promise()->then(function () use(&$buffered, $that) {
if ($buffered === null) {
$buffered = true;
} else {
$buffered = $that->parseValue(trim($buffered));
}
return $buffered;
});
}
示例15: __invoke
public function __invoke(GetAllUsers $query, Deferred $deferred = null)
{
$user = $this->userFinder->findAll();
if (null === $deferred) {
return $user;
}
$deferred->resolve($user);
}