2016-12-30 06:09:06 +01:00
|
|
|
<?php
|
2016-08-16 23:23:46 +02:00
|
|
|
|
2017-03-16 16:22:32 +01:00
|
|
|
namespace Amp\ByteStream;
|
2016-08-10 23:48:42 +02:00
|
|
|
|
2017-04-26 08:27:52 +02:00
|
|
|
use Amp\Promise;
|
2017-04-30 08:31:53 +02:00
|
|
|
use function Amp\call;
|
2016-08-10 23:48:42 +02:00
|
|
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
if (\strlen('…') !== 3) {
|
2016-08-16 00:19:32 +02:00
|
|
|
throw new \Error(
|
2017-04-26 08:17:19 +02:00
|
|
|
'The mbstring.func_overload ini setting is enabled. It must be disabled to use the stream package.'
|
2016-08-10 23:48:42 +02:00
|
|
|
);
|
|
|
|
} // @codeCoverageIgnoreEnd
|
|
|
|
|
|
|
|
/**
|
2017-04-30 08:31:53 +02:00
|
|
|
* @param \Amp\ByteStream\InputStream $source
|
|
|
|
* @param \Amp\ByteStream\OutputStream $destination
|
2016-08-10 23:48:42 +02:00
|
|
|
*
|
2017-03-16 16:22:32 +01:00
|
|
|
* @return \Amp\Promise
|
2016-08-10 23:48:42 +02:00
|
|
|
*/
|
2017-04-30 08:31:53 +02:00
|
|
|
function pipe(InputStream $source, OutputStream $destination): Promise {
|
|
|
|
return call(function () use ($source, $destination): \Generator {
|
|
|
|
$written = 0;
|
|
|
|
|
|
|
|
while (($chunk = yield $source->read()) !== null) {
|
|
|
|
$written += \strlen($chunk);
|
|
|
|
yield $destination->write($chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $written;
|
|
|
|
});
|
2017-05-07 22:14:45 +02:00
|
|
|
}
|