Exemplo n.º 1
0
 public function isEnabled(Context $context)
 {
     $all = all([$this->first->isEnabled($context), $this->second->isEnabled($context)]);
     return pipe($all, function ($res) {
         return (bool) ($res[0] ^ $res[1]);
     });
 }
Exemplo n.º 2
0
 private function send(string $message, callable $transform = null)
 {
     $promisor = new Deferred();
     $this->connection->send($message);
     $this->promisors[] = $promisor;
     return $transform ? pipe($promisor->promise(), $transform) : $promisor->promise();
 }
Exemplo n.º 3
0
 public function send(string $payload)
 {
     return pipe($this->connect(), function () use($payload) {
         $this->outputBuffer .= $payload;
         $this->outputBufferLength += strlen($payload);
         if ($this->writeWatcher !== null) {
             enable($this->writeWatcher);
         }
     });
 }
Exemplo n.º 4
0
 private function getResourceUri(string $resource) : Promise
 {
     if (substr($resource, 0, 8) === "https://") {
         return new Success($resource);
     }
     if (!$this->dictionary) {
         return pipe(resolve($this->fetchDictionary()), function () use($resource) {
             return $this->getResourceUri($resource);
         });
     }
     if (isset($this->dictionary[$resource])) {
         return new Success($this->dictionary[$resource]);
     }
     return new Failure(new AcmeException("Unknown resource: " . $resource));
 }
Exemplo n.º 5
0
 public function update(string $key, int $user, int $change) : Promise
 {
     $promisor = new Deferred();
     $this->redis->hIncrBy($key, $user, $change)->when(function ($error, $result) use($key, $user, $promisor) {
         if ($error) {
             $promisor->fail($error);
         } else {
             if ($result) {
                 $promisor->succeed($result);
             } else {
                 $promisor->succeed(pipe($this->redis->hDel($key, $user), function () use($result) {
                     return $result;
                 }));
             }
         }
     });
     return $promisor->promise();
 }
Exemplo n.º 6
0
 public function get(int $id)
 {
     return pipe($this->mysql->prepare("SELECT `token` FROM `auth_token` WHERE `user_id` = ?", [$id]), function (ResultSet $stmt) {
         return $stmt->fetchObject();
     });
 }
Exemplo n.º 7
0
 /**
  * Tries to acquire a lock.
  *
  * If acquiring a lock fails, it uses a blocking connection waiting for the current
  * client holding the lock to free it. If the other client crashes or doesn't free the lock, the returned
  * promise will fail, because once having entered the blocking mode, it doesn't try to aquire a lock until
  * another client frees the current lock. It can't react to key expires. You can call this method once again
  * if you absolutely need it, but usually, it should only be required if another client misbehaves or crashes,
  * which is clearly a bug then.
  *
  * @param string $id specific lock ID (every lock has its own ID).
  * @param string $token unique token (only has to be unique within other locking attempts with the same lock ID).
  * @return Promise promise fails if lock couldn't be acquired, otherwise resolves to true.
  */
 public function lock($id, $token) : Promise
 {
     return pipe($this->std->eval(self::LOCK, ["lock:{$id}", "queue:{$id}"], [$token, $this->ttl]), function ($result) use($id, $token) {
         if ($result) {
             return true;
         } else {
             return pipe($this->std->expire("queue:{$id}", $this->timeout * 2, true), function () use($id, $token) {
                 $connection = $this->getReadyConnection();
                 $promise = $connection->brPoplPush("queue:{$id}", "lock:{$id}", $this->timeout);
                 $promise->when(function () use($connection) {
                     $hash = spl_object_hash($connection);
                     $key = $this->busyConnectionMap[$hash] ?? null;
                     unset($this->busyConnections[$key]);
                     unset($this->busyConnectionMap[$hash]);
                     $this->readyConnections[] = [time(), $connection];
                 });
                 return pipe($promise, function ($result) use($id, $token) {
                     if ($result === null) {
                         return new Failure(new LockException());
                     }
                     return $this->std->eval(self::TOKEN, ["lock:{$id}"], [$token, $this->ttl]);
                 });
             });
         }
     });
 }
Exemplo n.º 8
0
 public function get(int $id) : Promise
 {
     return pipe($this->mysql->prepare("SELECT room_id, token FROM hook WHERE hook_id = ?", [$id]), function (ResultSet $stmt) {
         return $stmt->fetchObject();
     });
 }
Exemplo n.º 9
0
 /**
  * Destroys the session
  * @return \Amp\Promise resolving after success
  */
 public function destroy() : Promise
 {
     if ($this->state !== self::LOCKED) {
         if ($this->state === self::PENDING) {
             throw new LockException("Session is not yet locked, wait until the Promise returned by Session::open() is resolved");
         } else {
             throw new LockException("Session is not locked, can't write");
         }
     }
     $this->data = [];
     $this->state = self::UNLOCKED;
     if ($this->id) {
         $promise = $this->driver->save($this->id, [], $this->ttl == -1 ? $this->maxlife : $this->ttl + 1);
         $this->setId(false);
         return pipe($promise, function () {
             return $this;
         });
     } else {
         return new Success($this);
     }
 }
Exemplo n.º 10
0
 /**
  * @param string[] $args
  * @param callable $transform
  * @return Promise
  */
 protected function send(array $args, callable $transform = null)
 {
     $promisor = new Deferred();
     $this->connection->send($args);
     $this->promisors[] = $promisor;
     return $transform ? pipe($promisor->promise(), $transform) : $promisor->promise();
 }
Exemplo n.º 11
0
 /**
  * @param array $strings
  * @return Promise
  */
 public function send(array $strings)
 {
     foreach ($strings as $string) {
         if (!is_scalar($string)) {
             throw new \InvalidArgumentException("All elements must be of type string or scalar and convertible to a string.");
         }
     }
     return pipe($this->connect(), function () use($strings) {
         $payload = "";
         foreach ($strings as $string) {
             $payload .= "\$" . strlen($string) . "\r\n{$string}\r\n";
         }
         $payload = "*" . count($strings) . "\r\n{$payload}";
         $this->outputBuffer .= $payload;
         $this->outputBufferLength += strlen($payload);
         if ($this->writeWatcher !== null) {
             enable($this->writeWatcher);
         }
     });
 }