mirror of
https://github.com/danog/byte-stream.git
synced 2024-12-02 09:17:50 +01:00
1b0b8daed4
STDERR is used by amphp/parallel for error forwarding from children. STDERR gets written to the error log in case of Apache and similar locations for other SAPIs. This also ensures STDOUT / STDERR can always be used and only a single stream ID exists for STDOUT / STDERR, which might otherwise error for libuv if people open 'php://stderr' multiple times. Fixes amphp/parallel-functions#6. Fixes amphp/parallel#34.
43 lines
1.0 KiB
PHP
43 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace Amp\ByteStream;
|
|
|
|
use Amp\Promise;
|
|
use function Amp\call;
|
|
|
|
// @codeCoverageIgnoreStart
|
|
if (\strlen('…') !== 3) {
|
|
throw new \Error(
|
|
'The mbstring.func_overload ini setting is enabled. It must be disabled to use the stream package.'
|
|
);
|
|
} // @codeCoverageIgnoreEnd
|
|
|
|
if (!\defined('STDOUT')) {
|
|
\define('STDOUT', \fopen('php://stdout', 'w'));
|
|
}
|
|
|
|
if (!\defined('STDERR')) {
|
|
\define('STDERR', \fopen('php://stderr', 'w'));
|
|
}
|
|
|
|
/**
|
|
* @param \Amp\ByteStream\InputStream $source
|
|
* @param \Amp\ByteStream\OutputStream $destination
|
|
*
|
|
* @return \Amp\Promise
|
|
*/
|
|
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);
|
|
$writePromise = $destination->write($chunk);
|
|
$chunk = null; // free memory
|
|
yield $writePromise;
|
|
}
|
|
|
|
return $written;
|
|
});
|
|
}
|