コード例 #1
0
 /**
  * @param  array $criteria              Criterias for querying geocoding
  * @return GuzzlePromise                The promise for guzzle request
  *
  * @throws InvalidArgumentException
  */
 public function lookup($criteria)
 {
     // TODO: Discuss criteria validation
     if (!is_array($criteria)) {
         throw new InvalidArgumentException('Criteria must be an array with keys and values');
     }
     $lookup_promise = null;
     $lookup_promise = new GuzzlePromise(function () use($criteria, &$lookup_promise) {
         $this->client_interface->sendAsync(new Request("GET", sprintf(self::GEOCODEURL, $this->response_type, $this->arrayToQueryParams($criteria))))->then(function ($response) use($lookup_promise) {
             if ($response->getStatusCode() === 200) {
                 $geo_location_response = json_decode($response->getBody());
                 if ($geo_location_response->status !== "OK") {
                     $lookup_promise->reject(sprintf("Server responded with status %s", $geo_location_response->status));
                 } else {
                     $lookup_promise->resolve(Factory::create('GoogleGeoCoder\\GeoCollection', $geo_location_response->results));
                 }
             } else {
                 $lookup_promise->reject(sprintf("Server responded with code %s", $response->getStatusCode()));
             }
         }, function ($response) use($lookup_promise) {
             $lookup_promise->reject(sprintf("Server responded with failure"));
         })->wait();
     });
     return $lookup_promise;
 }
コード例 #2
0
ファイル: Coroutine.php プロジェクト: nsandlin/linepig
 /**
  * @internal
  */
 public function _handleFailure($reason)
 {
     unset($this->currentPromise);
     try {
         $nextYield = $this->generator->throw(exception_for($reason));
         // The throw was caught, so keep iterating on the coroutine
         $this->nextCoroutine($nextYield);
     } catch (Exception $exception) {
         $this->result->reject($exception);
     } catch (Throwable $throwable) {
         $this->result->reject($throwable);
     }
 }
コード例 #3
0
 /**
  * @param \SplFileInfo|string $cwd
  * @return PromiseInterface
  */
 public function enqueue($cwd = null)
 {
     $process = $this->factory->make($cwd);
     /** @var Promise $promise */
     $promise = new Promise(function () use($process, &$promise) {
         if ($process->isStarted()) {
             $process->wait();
         }
         $process->isSuccessful() ? $promise->resolve($process) : $promise->reject($process);
     });
     $this->queue->add($process);
     $process->setOptions([ProcessQueue::PROMISE_KEY => $promise]);
     return $promise;
 }
コード例 #4
0
 public function run(callable $callback, $time)
 {
     $promise = new Promise();
     $loop = Runner::getLoop();
     $loop->addTimer($time, function () use($promise, $callback) {
         if ($promise->getState() === PromiseInterface::PENDING) {
             try {
                 $promise->resolve($callback());
             } catch (\Exception $e) {
                 $promise->reject($e);
             }
         }
     });
     return $promise;
 }
コード例 #5
0
 /**
  * @test
  */
 public function it_rejects_deferred_with_json_response_asynchronous()
 {
     $producerInAsyncMode = new HttpMessageProducer($this->guzzleClient, new NoOpMessageConverter(), null, true);
     $fetchDataQuery = new FetchSomething(['filter' => 'foo']);
     $ex = new \Exception('ka boom');
     $queryDeferred = new Deferred();
     $producerInAsyncMode($fetchDataQuery, $queryDeferred);
     $this->responsePromise->reject($ex);
     //Perform next tick, required to resolve the promise when we are in async mode
     $queue = \GuzzleHttp\Promise\queue();
     $queue->run();
     $rejectionReason = null;
     $queryDeferred->promise()->otherwise(function ($reason) use(&$rejectionReason) {
         $rejectionReason = $reason;
     });
     $this->assertSame($ex, $rejectionReason);
 }
コード例 #6
0
 public function __invoke(RequestInterface $request, array $options)
 {
     $artaxRequest = $this->convertRequest($request, $options);
     $artaxResponsePromise = $this->artaxClient->request($artaxRequest);
     $guzzleResponsePromise = new Promise(function () use($artaxResponsePromise) {
         \Amp\wait($artaxResponsePromise);
     });
     $artaxResponsePromise->when(function ($error = null, Response $artaxResponse = null) use($request, $options, $guzzleResponsePromise) {
         if ($error) {
             $guzzleResponsePromise->reject($error);
         } else {
             $response = $this->convertResponse($artaxResponse, $request, $options);
             $guzzleResponsePromise->resolve($response);
         }
     });
     return $guzzleResponsePromise;
 }
コード例 #7
0
 public function start()
 {
     $promise = new Promise([$this, 'wait']);
     $this->process->start(function ($type, $buffer) use($promise) {
         if (Process::ERR === $type) {
             $this->logger->error($buffer);
         } else {
             $this->logger->debug($buffer);
         }
         if ($promise->getState() === Promise::PENDING) {
             if (Process::ERR === $type) {
                 $promise->reject(false);
             } else {
                 $promise->resolve(true);
             }
         }
     });
     return $promise;
 }
コード例 #8
0
ファイル: functionsTest.php プロジェクト: alvarobfdev/applog
 public function testCoroutineOtherwiseIntegrationTest()
 {
     $a = new P\Promise();
     $b = new P\Promise();
     $promise = P\coroutine(function () use($a, $b) {
         // Execute the pool of commands concurrently, and process errors.
         (yield $a);
         (yield $b);
     })->otherwise(function (\Exception $e) {
         // Throw errors from the operations as a specific Multipart error.
         throw new \OutOfBoundsException('a', 0, $e);
     });
     $a->resolve('a');
     $b->reject('b');
     $reason = P\inspect($promise)['reason'];
     $this->assertInstanceOf('OutOfBoundsException', $reason);
     $this->assertInstanceOf('GuzzleHttp\\Promise\\RejectionException', $reason->getPrevious());
 }
コード例 #9
0
ファイル: Request.php プロジェクト: ferjul17/Sellsy-api
 /**
  * @param string $method
  * @param mixed  $params
  *
  * @return PromiseInterface
  */
 public function callAsync($method, $params)
 {
     $requestId = uniqid();
     $options = $this->prepareCall($method, $params, $requestId);
     $response = $this->getClient()->postAsync('', $options);
     $promise = new Promise(function ($unwrap) use($response) {
         return $response->wait($unwrap);
     }, function () use($response) {
         return $response->cancel();
     });
     $response->then(function (ResponseInterface $res) use($promise) {
         try {
             $promise->resolve($this->handleResponse($res->getBody()));
             return $res;
         } catch (\Exception $e) {
             $promise->reject($e);
             throw $e;
         }
     }, function (\Exception $reqException) use($promise, $requestId) {
         $this->logResponse($requestId, strval($reqException));
         $promise->reject($reqException);
         return $reqException;
     });
     $promise->then(function ($res) use($requestId) {
         $this->logResponse($requestId, json_encode($res));
         return $res;
     }, function ($res) use($requestId) {
         $this->logResponse($requestId, strval($res));
         return $res;
     });
     return $promise;
 }
コード例 #10
0
ファイル: PromiseTest.php プロジェクト: lewishealey/confetti
 public function testOtherwiseIsSugarForRejections()
 {
     $p = new Promise();
     $p->reject('foo');
     $p->otherwise(function ($v) use(&$c) {
         $c = $v;
     });
     P\queue()->run();
     $this->assertEquals($c, 'foo');
 }
コード例 #11
0
 /**
  * @param RequestInterface $request
  * @param array $options
  * @return Promise
  */
 public function __invoke(RequestInterface $request, array $options)
 {
     $ready = false;
     $promise = new Promise(function () use(&$ready) {
         do {
             $this->loop->tick();
         } while (!$ready);
     });
     $this->requestFactory->create($request, $options, $this->httpClient, $this->loop)->then(function (ResponseInterface $response) use(&$ready, $promise) {
         $ready = true;
         $promise->resolve($response);
         $this->invokeQueue();
     }, function ($error) use(&$ready, $promise) {
         $ready = true;
         $promise->reject($error);
         $this->invokeQueue();
     });
     return $promise;
 }
コード例 #12
0
ファイル: Operator.php プロジェクト: alewitt/openstack
 /**
  * Magic method which intercepts async calls, finds the sequential version, and wraps it in a
  * {@see Promise} object. In order for this to happen, the called methods need to be in the
  * following format: `createAsync`, where `create` is the sequential method being wrapped.
  *
  * @param $methodName The name of the method being invoked.
  * @param $args       The arguments to be passed to the sequential method.
  *
  * @return Promise
  */
 public function __call($methodName, $args)
 {
     if (substr($methodName, -5) === 'Async') {
         $realMethod = substr($methodName, 0, -5);
         if (!method_exists($this, $realMethod)) {
             throw new \InvalidArgumentException(sprintf('%s is not a defined method on %s', $realMethod, get_class($this)));
         }
         $promise = new Promise(function () use(&$promise, $realMethod, $args) {
             $value = call_user_func_array([$this, $realMethod], $args);
             $promise->resolve($value);
         }, function ($e) use(&$promise) {
             $promise->reject($e);
         });
         return $promise;
     }
 }