amphp / byte-stream

A non-blocking stream abstraction for PHP based on Amp.
https://amphp.org/byte-stream
MIT License
363 stars 31 forks source link

clarify `getStderr` behaviour when STDERR is not available #112

Open azjezz opened 3 months ago

azjezz commented 3 months ago

currently, ByteStream\getStderr has a weird behaviour in my opnion when the standard error output is not available. which is that ByteStream\getStderr() returns a stream, but, it is not the expected STDERR stream, instead, its a php://memory stream that has been closed.

I would expect either:

  1. return null indicating that the stream is not available
  2. throw an exception informing me that the stream is not available

It would be good if this behaviour could be clarified maybe via documentation. and maybe introducing a new function that has a ( IMO ) saner behaviour?


For context, i was trying to replicate this:


    /**
     * Returns the default terminal output.
     *
     * @internal
     */
    public static function getOutput(): OutputInterface
    {
        $standardOutputHandle = IO\output_handle();
        $standardErrorOutputHandle = IO\error_handle();
        if (null === $standardErrorOutputHandle) {
            return new HandleOutput($standardOutputHandle);
        }

        return new HandleConsoleOutput($standardOutputHandle, $standardErrorOutputHandle);
    }

which is possible to replicate with the current behaviour:

    /**
     * Returns the default terminal output.
     *
     * @internal
     */
    public static function getOutput(): OutputInterface
    {
        $standardOutputStream = ByteStream\getStdout();
        $standardErrorOutputStream = ByteStream\getStderr();
        if ($standardErrorOutputStream->isClosed()) {
            return new ByteStreamOutput($standardOutputStream);
        }

        return new ByteStreamConsoleOutput($standardOutputStream, $standardErrorOutputStream);
    }

however, this implementation took me a bit of time to figure out as Amp does not mention anything about returning a closed stream from getStderr.