Esempio n. 1
0
 public function testPromiseThen__ThenThen_Resolved_onFulfilledException_onRejectedResolved()
 {
     $promise1 = new Client($this->store);
     $promise2 = $promise1->then([get_class($this), 'callException'], null);
     $message = ' was resolved';
     $this->object = $promise2->then(null, function ($reason) use($message) {
         return $reason->getMessage() . $message;
     });
     $promise1->resolve('result');
     $this->assertStringEndsWith(' was resolved', $this->object->wait(false));
 }
Esempio n. 2
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return ResponseInterface
  * @throws \zaboy\rest\RestException
  * @internal param callable|null $next
  */
 public function methodPutWithId(ServerRequestInterface $request, ResponseInterface $response)
 {
     $primaryId = $request->getAttribute('Primary-Key-Value');
     if (!$this->isId($primaryId)) {
         throw new PromiseException('There is no promise. PromiseId: ' . $primaryId);
     }
     $promise = new Client($this->store, $primaryId);
     $promiseData = $request->getParsedBody();
     if (!isset($promiseData[Store::STATE])) {
         throw new PromiseException('There is no key STATE in the Body. PromiseId: ' . $primaryId);
     }
     if (!isset($promiseData[Store::RESULT])) {
         throw new PromiseException('There is no key RESULT in the Body. PromiseId: ' . $primaryId);
     }
     switch ($promiseData[Store::STATE]) {
         case PromiseInterface::FULFILLED:
             $promise->resolve($promiseData[Store::RESULT]);
             break;
         case PromiseInterface::REJECTED:
             $promise->reject($promiseData[Store::RESULT]);
             break;
         default:
             throw new PromiseException('The STATE field must be FULFILLED or REJECTED. PromiseId: ' . $primaryId);
     }
     $responseBody = $promise->toArray();
     $this->request = $request->withAttribute('Response-Body', $responseBody);
     $response = $response->withStatus(200);
     return $response;
 }
Esempio n. 3
0
use zaboy\scheduler\FileSystem\CommandLineWorker;
use zaboy\scheduler\Callback\CallbackException;
use zaboy\async\Promise\Store;
use zaboy\async\Promise\Client as Promise;
$commandLineWorker = new CommandLineWorker();
$options = $commandLineWorker->getCallOptions($_SERVER['argv']);
if (!isset($options['rpc_callback'])) {
    throw new CallbackException("The necessary parameter \"rpc_callback\" does not exist");
}
$callbackServiceName = $options['rpc_callback'];
unset($options['rpc_callback']);
/** @var Zend\ServiceManager\ServiceManager $container */
$container = (include './config/container.php');
/** @var zaboy\scheduler\Callback\CallbackManager $callbackManager */
$callbackManager = $container->get('callback_manager');
/** @var Store $store */
$store = $container->get('Store');
$promise = new Promise($store, $options['promise']);
unset($options['promise']);
try {
    if (is_callable($callbackServiceName)) {
        $result = call_user_func($callbackServiceName, $options);
    } elseif ($callbackManager->has($callbackServiceName)) {
        $result = $callbackManager->{$callbackServiceName}($options);
    } else {
        throw new CallbackException('Specified callback "' . print_r($callbackServiceName) . '" wasn\'t found');
    }
    $promise->resolve($result);
} catch (\Exception $e) {
    $promise->reject($e);
}