Ejemplo n.º 1
0
 public function postTelegram()
 {
     BufferedSink::createPromise($this->resolver->request)->then(function ($body) {
         $update = new Update(json_decode($body, true));
         $message = $update->getMessage();
         if ($message !== null && $message->has('text')) {
             $this->resolver->telegram->getCommandBus()->handler($message->getText(), $update);
         }
     });
 }
Ejemplo n.º 2
0
 /**
  * @internal
  * @param ResponseInterface $response
  * @return PromiseInterface Promise<ResponseInterface, Exception>
  */
 public function bufferResponse(ResponseInterface $response)
 {
     $stream = $response->getBody();
     // body is not streaming => already buffered
     if (!$stream instanceof ReadableStreamInterface) {
         return Promise\resolve($response);
     }
     // buffer stream and resolve with buffered body
     $messageFactory = $this->messageFactory;
     return BufferedSink::createPromise($stream)->then(function ($body) use($response, $messageFactory) {
         return $response->withBody($messageFactory->body($body));
     });
 }
Ejemplo n.º 3
0
 /** @test */
 public function gettingEncryptedStuffFromGoogleShouldWork()
 {
     $loop = new StreamSelectLoop();
     $factory = new Factory();
     $dns = $factory->create('8.8.8.8', $loop);
     $connected = false;
     $response = null;
     $secureConnector = new SecureConnector(new Connector($loop, $dns), $loop);
     $secureConnector->create('google.com', 443)->then(function ($conn) use(&$connected) {
         $connected = true;
         $conn->write("GET / HTTP/1.0\r\n\r\n");
         return BufferedSink::createPromise($conn);
     })->then(function ($data) use(&$response) {
         $response = $data;
     });
     $loop->run();
     $this->assertTrue($connected);
     $this->assertRegExp('#^HTTP/1\\.0#', $response);
 }
Ejemplo n.º 4
0
 private function onResponse(Chain $chain)
 {
     return function (Response $response) use($chain) {
         BufferedSink::createPromise($response)->then(function ($body) use($chain) {
             $logger = DI::get()->getLogger();
             $logger->info('Got http response: ' . $body);
             if (!($json = json_decode($body, 1))) {
                 return;
             }
             $logger->info('JSON decoded');
             $channelId = $chain->getUser()->getChannelId();
             /** @var UserCollection $users */
             $users = DI::get()->getUsers();
             $response = (new MusicResponse())->setInfo($json)->setChannelId($channelId);
             $loop = DI::get()->container()->get('eventloop');
             $loop->addTimer(1, function () use($users, $response, $logger) {
                 $users->setResponse($response)->notify(false);
                 $logger->info('Sent MusicResponse!');
             });
             $channel = ChannelsCollection::get()->getChannelById($channelId);
             $history = $channel->getHistory(0);
             foreach ($history as $k => $part) {
                 /** @var Msg $msgObj */
                 $msgObj = $part['msg'];
                 $string = $msgObj->getMsg($chain->getUser()->getLang());
                 if (!preg_match('~id="music-(' . $json['track_id'] . ')"~u', $string)) {
                     continue;
                 }
                 $logger->info('Replacing corresponding history row');
                 $string = str_replace('id="music-' . $json['track_id'] . '" data-src=""><span class="glyphicon glyphicon-play-circle">' . '</span> <span class="audio-title">...</span>', 'id="music-' . $json['track_id'] . '" data-src="' . $json['url'] . '" bind-play-click="1">' . '<span class="glyphicon glyphicon-play-circle"></span> ' . '<span class="audio-title">' . $json['artist'] . ' - ' . $json['track'] . '</span>', $string);
                 $part['msg'] = MsgRaw::create($string);
                 $channel->setRow($k, $part);
             }
         });
     };
 }
Ejemplo n.º 5
0
use Clue\React\Tar\Decoder;
use React\Stream\BufferedSink;
use Clue\Hexdump\Hexdump;
use React\EventLoop\StreamSelectLoop;
require __DIR__ . '/../vendor/autoload.php';
$in = isset($argv[1]) ? $argv[1] : __DIR__ . '/../tests/fixtures/alice-bob.tar';
echo 'Reading file "' . $in . '" (pass as argument to example)' . PHP_EOL;
// using the default loop does *not* work for file I/O
//$loop = Factory::create();
$loop = new StreamSelectLoop();
$stream = new Stream(fopen($in, 'r'), $loop);
$decoder = new Decoder();
$decoder->on('entry', function ($header, $file) {
    static $i = 0;
    echo 'FILE #' . ++$i . PHP_EOL;
    echo 'Received entry headers:' . PHP_EOL;
    var_dump($header);
    BufferedSink::createPromise($file)->then(function ($contents) {
        echo 'Received entry contents (' . strlen($contents) . ' bytes)' . PHP_EOL;
        $d = new Hexdump();
        echo $d->dump($contents) . PHP_EOL . PHP_EOL;
    });
});
$decoder->on('error', function ($error) {
    echo 'ERROR: ' . $error . PHP_EOL;
});
$decoder->on('close', function () {
    echo 'CLOSED' . PHP_EOL;
});
$stream->pipe($decoder);
$loop->run();
 protected function openWrite($path)
 {
     $stream = new ThroughStream();
     BufferedSink::createPromise($stream)->then(function ($contents) use($path) {
         $this->invoker->invokeCall('write', ['flysystem' => serialize($this->flysystem), 'path' => $path, 'contents' => base64_encode($contents)]);
     });
     return \React\Promise\resolve($stream);
 }
Ejemplo n.º 7
0
 /** @test */
 public function factoryMethodShouldImplicitlyPipeAndPromise()
 {
     $callback = $this->expectCallableOnceWith('foo');
     $readable = new ReadableStream();
     BufferedSink::createPromise($readable)->then($callback);
     $readable->emit(Event::DATA, array('foo'));
     $readable->close();
 }
Ejemplo n.º 8
0
 public function onResponse(Response $response)
 {
     $this->response = $response;
     BufferedSink::createPromise($response)->then(array($this, 'onLoad'));
 }
Ejemplo n.º 9
0
 public function queryWhoisServer($domain, $ip)
 {
     $conn = call_user_func($this->connFactory, $ip);
     $conn->write("{$domain}\r\n");
     return BufferedSink::createPromise($conn)->then(array($this, 'normalizeLinefeeds'));
 }
Ejemplo n.º 10
0
 /**
  * Returns a promise for the request body of the given request
  *
  * @param Request $request
  * @return \React\Promise\Promise
  */
 public function getRequestBodyPromiseForRequest($request)
 {
     return BufferedSink::createPromise($request);
 }
Ejemplo n.º 11
0
 /**
  * {@inheritDoc}
  */
 public function getContents()
 {
     return $this->open('r')->then(function ($stream) {
         return BufferedSink::createPromise($stream)->always(function () {
             $this->close();
         });
     });
 }
Ejemplo n.º 12
0
 /** @test */
 public function pipeShouldSucceedAndResolve()
 {
     $callback = $this->expectCallableOnceWith('foobar');
     $sink = new BufferedSink();
     $sink->promise()->then($callback);
     $readable = new ReadableStream();
     $readable->pipe($sink);
     $readable->emit('data', array('foo'));
     $readable->emit('data', array('bar'));
     $readable->close();
 }