Skip to content

Instantly share code, notes, and snippets.

@WyriHaximus
Created July 10, 2017 19:06
Show Gist options
  • Save WyriHaximus/b9c079d588d54d04288b6a22130b5999 to your computer and use it in GitHub Desktop.
Save WyriHaximus/b9c079d588d54d04288b6a22130b5999 to your computer and use it in GitHub Desktop.
<?php
use function Clue\React\Block\await;
use React\EventLoop\Factory;
use React\Promise\Deferred;
use function React\Promise\Stream\buffer;
use React\Stream\ThroughStream;
use WyriHaximus\React\Stream\Base64\WritableStreamBase64Encode;
use WyriHaximus\React\Stream\Hash\WritableStreamHash;
require 'vendor/autoload.php';
$loop = Factory::create();
$destination = new ThroughStream();
$fileStream = new ThroughStream();
$hashStream = $fileStream;
$loop->futureTick(function () use ($destination, $fileStream) {
$deferred = new Deferred();
$decoratedFileStream = new WritableStreamHash(new WritableStreamBase64Encode($fileStream), 'sha1');
$destination->write('{');
$destination->write('"message":"filename",');
$destination->write('"comitter": {');
$destination->write('"name":"Cees-Jan Kiewiet",');
$destination->write('"email":"hi@ceesjankiewiet.nl');
$destination->write('},');
$destination->write('"content": "');
$fileStream->on('data', function ($data) use ($destination) {
$destination->write($data);
});
$fileStream->on('close', function () use ($destination, $deferred) {
$deferred->promise()->then(function ($hash) use ($destination) {
$destination->write('",');
$destination->write('"sha": "' . $hash . '"');
$destination->end('}');
});
});
$decoratedFileStream->on('hash', function ($hash) use ($deferred) {
$deferred->resolve($hash);
});
$decoratedFileStream->end('foo.bar');
});
var_export(await(buffer($destination), $loop));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment