diff --git a/src/io/InvalidHandleException.php b/src/io/InvalidHandleException.php new file mode 100644 index 00000000..7ed6b25a --- /dev/null +++ b/src/io/InvalidHandleException.php @@ -0,0 +1,20 @@ +> - public static function stderr(): IO\WriteHandle { - return new self(\STDERR); + public static function serverError(): IO\WriteHandle { + // while the documentation says to use the STDERR constant, that is + // conditionally defined + return new self(\fopen('php://stderr', 'w')); } <<__Memoize>> - public static function stdout(): IO\WriteHandle { - return new self(\STDOUT); + public static function serverOutput(): IO\WriteHandle { + return new self(\fopen('php://stdout', 'w')); } <<__Memoize>> - public static function stdin(): IO\ReadHandle { - return new self(\STDIN); + public static function serverInput(): IO\ReadHandle { + return new self(\fopen('php://stdin', 'r')); + } + + <<__Memoize>> + public static function requestInput(): IO\ReadHandle { + return new self(\fopen('php://input', 'r')); + } + + <<__Memoize>> + public static function requestOutput(): IO\WriteHandle{ + return new self(\fopen('php://output', 'w')); + } + + <<__Memoize>> + public static function requestError(): IO\WriteHandle { + /* HH_FIXME[2049] deregistered PHP stdlib */ + /* HH_FIXME[4107] deregistered PHP stdlib */ + if (\php_sapi_name() !== "cli") { + throw new IO\InvalidHandleException( + "requestError is not supported in the current execution mode" + ); + } + return self::serverError(); } } diff --git a/src/io/stdio.php b/src/io/stdio.php index f31a1384..45faeee8 100644 --- a/src/io/stdio.php +++ b/src/io/stdio.php @@ -12,14 +12,60 @@ use namespace HH\Lib\_Private; -function stdout(): WriteHandle { - return _Private\StdioHandle::stdout(); +/** Return STDOUT for the server process. + * + * This is usually not the same thing as request output. + * + * @see request_output + */ +function server_output(): WriteHandle { + return _Private\StdioHandle::serverOutput(); } -function stderr(): WriteHandle { - return _Private\StdioHandle::stderr(); +/** Return STDERR for the server process. + * + * @see request_error + */ +function server_error(): WriteHandle { + return _Private\StdioHandle::serverError(); } -function stdin(): ReadHandle { - return _Private\StdioHandle::stdin(); +/** Return STDIN for the server process. + * + * @see request_input + */ +function server_input(): ReadHandle { + return _Private\StdioHandle::serverInput(); +} + +/** Return the output handle for the current request. + * + * This should generally be used for sending data to clients. In CLI mode, this + * is usually the process STDOUT. + * + * @see requestOutput + */ +function request_output(): WriteHandle { + return _Private\StdioHandle::requestOutput(); +} + +/** Return the error output handle for the current request. + * + * This is usually only available for CLI scripts; it will throw an + * `UnsupportedHandleException` in most other contexts, including HTTP + * requests. + * + * In CLI mode, this is usually the process STDERR. + */ +function request_error(): WriteHandle { + return _Private\StdioHandle::requestError(); +} + +/** Return the input handle for the current request. + * + * In CLI mode, this is likely STDIN; for HTTP requests, it may contain the + * POST data, if any. + */ +function request_input(): ReadHandle { + return _Private\StdioHandle::requestInput(); }