本文整理汇总了PHP中React\Promise\Deferred::resolve方法的典型用法代码示例。如果您正苦于以下问题:PHP Deferred::resolve方法的具体用法?PHP Deferred::resolve怎么用?PHP Deferred::resolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类React\Promise\Deferred
的用法示例。
在下文中一共展示了Deferred::resolve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processDeferred
/**
* Make a directory as described in the task description
* @param Deferred $deferred
* @param TaskInterface $task
* @return bool
*/
protected function processDeferred(Deferred $deferred, TaskInterface $task)
{
yield;
/** @var Description $description */
$description = $task->getDescription();
$deferred->notify(new Notification("Creating directory '{$description->getName()}'", Notification::PRIORITY_NORMAL));
if ($this->getFileSystem()->isDirectory($description->getName())) {
$deferred->resolve("Directory '{$description->getName()}' already exists");
return;
}
if ($this->getFileSystem()->makeDirectory($description->getName())) {
$deferred->resolve("Created directory '{$description->getName()}'");
return;
}
$deferred->reject("Directory '{$description->getName()}' could not be created");
}
示例2: doQuery
public function doQuery($nameserver, $transport, $queryData, $name)
{
$parser = $this->parser;
$loop = $this->loop;
$response = new Message();
$deferred = new Deferred();
$retryWithTcp = function () use($nameserver, $queryData, $name) {
return $this->doQuery($nameserver, 'tcp', $queryData, $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->createConnection($nameserver, $transport);
$conn->on('data', function ($data) use($retryWithTcp, $conn, $parser, $response, $transport, $deferred, $timer) {
$responseReady = $parser->parseChunk($data, $response);
if (!$responseReady) {
return;
}
$timer->cancel();
if ($response->header->isTruncated()) {
if ('tcp' === $transport) {
$deferred->reject(new BadServerException('The server set the truncated bit although we issued a TCP request'));
} else {
$conn->end();
$deferred->resolve($retryWithTcp());
}
return;
}
$conn->end();
$deferred->resolve($response);
});
$conn->write($queryData);
return $deferred->promise();
}
示例3: tick
/**
* {@inheritdoc}
*/
public function tick()
{
if ($this->firstTick) {
$this->firstTick = false;
$this->deferred->notify(new MessageEvent($this, $this->generator->current()));
} else {
$this->deferred->notify(new MessageEvent($this, $this->generator->send(null)));
}
if (!$this->generator->valid()) {
$this->deferred->resolve(new Event($this));
}
}
示例4: getTile
/**
* Return file name through a promise.
*
* @param integer $xAxis X coordinate.
* @param integer $yAxis Y coordinate.
*
* @return \React\Promise\Promise
*/
public function getTile($xAxis, $yAxis)
{
$fileName = str_replace(['{x}', '{y}'], [$xAxis, $yAxis], $this->location);
$deferred = new Deferred();
$this->loader->imageExists($fileName)->then(function () use($deferred, $fileName) {
$deferred->resolve($fileName);
}, function () use($deferred, $fileName) {
if (empty($this->fallbackImage)) {
return $deferred->resolve($fileName);
}
$deferred->resolve($this->fallbackImage);
});
return $deferred->promise();
}
示例5: tick
/**
* {@inheritdoc}
*/
public function tick()
{
$this->startPendingTasks();
if ($this->runningTasks->isEmpty()) {
$this->deferred->resolve();
return;
}
/** @var TaskInterface $task */
$task = $this->runningTasks->dequeue();
if (false !== ($pos = array_search($task, $this->finishedTasks, true))) {
unset($this->finishedTasks[array_search($task, $this->finishedTasks, true)]);
return $this->tick();
}
$task->tick();
$this->runningTasks->enqueue($task);
}
示例6: 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);
});
});
}
示例7: 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();
}
示例8: wait
public function wait()
{
if ($this->isRealized) {
return false;
}
// Seed the pool with N number of requests.
$this->addNextRequests();
// Stop if the pool was cancelled while transferring requests.
if ($this->isRealized) {
return false;
}
// Wait on any outstanding FutureResponse objects.
while ($response = array_pop($this->waitQueue)) {
try {
$response->wait();
} catch (\Exception $e) {
// Eat exceptions because they should be handled asynchronously
}
$this->addNextRequests();
}
// Clean up no longer needed state.
$this->isRealized = true;
$this->waitQueue = $this->eventListeners = [];
$this->client = $this->iter = null;
$this->deferred->resolve(true);
return true;
}
示例9: call
/**
* @param $channel
* @param string $message
* @return DeferredPromise
*/
public function call($channel, $message)
{
$loop = LoopFactory::create();
$factory = new StompFactory($loop);
$client = $factory->createClient();
$deferred = new Deferred();
$timer = $loop->addTimer(2, function () use($deferred, $client) {
$client->disconnect();
$deferred->reject(new RpcTimeIsOutException());
});
$client->connect()->then(function (StompClient $client) use($message, $channel, $loop, $deferred, $timer) {
$rpcReceiver = function (Frame $frame) use($deferred, $timer, $client) {
$timer->cancel();
$client->disconnect();
try {
$deferred->resolve($frame);
} catch (\Exception $e) {
$deferred->reject($e);
}
};
$client->sendToTemp($channel, $message, [], $rpcReceiver);
}, function () use($deferred, $client) {
$client->disconnect();
$deferred->reject(new \RuntimeException('Error start rpc connection'));
});
$loop->run();
return $deferred->promise();
}
示例10: 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);
}
}
示例11: executeRequest
private function executeRequest($method, $uri, $body = null)
{
$deferred = new Deferred();
$request = $this->client->request($method, $this->buildUrl($uri), array('Content-Length' => strlen($body), 'Authorization' => 'Basic ' . base64_encode($this->options['user'] . ':' . $this->options['password']), 'Content-Type' => 'application/json'));
$request->writeHead();
$request->on('error', function ($error) use($uri, $deferred) {
$deferred->reject(sprintf('Error while doing the request on %s : %s', $uri, $error->getMessage()));
});
$request->on('response', function (Response $response) use($deferred) {
if ($response->getCode() < 200 || $response->getCode() >= 400) {
$deferred->reject(sprintf('The response is not as expected (status code %s, message is %s)', $response->getCode(), $response->getReasonPhrase()));
}
$response->on('error', function ($error) use($deferred) {
$deferred->reject($error->getMessage());
});
$data = (object) array('data' => '');
$response->on('data', function ($chunk) use($data) {
$data->data .= $chunk;
});
$response->on('end', function () use($deferred, $data) {
$deferred->resolve($data->data);
});
});
$request->end($body);
return $deferred->promise();
}
示例12: 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;
});
}
示例13: 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;
});
}
示例14: 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();
});
}
示例15: 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);
}