Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
Remove stdout/stderr/stdin, add {request,server}{Output,Input,Error} (#…
Browse files Browse the repository at this point in the history
…27)

Summary:
fixes #26

STDIN, STDOUT, and STDERR are undefined except in CLI mode. The php://std* handles are always *server* file handles. Expose php://input and php://output as well

Pull Request resolved: #27

Reviewed By: alexeyt

Differential Revision: D13304206

Pulled By: alexeyt

fbshipit-source-id: 94b8cfb303e8a70c72dbb015a938b6c2f4dfdd3f
  • Loading branch information
fredemmott committed Dec 5, 2018
1 parent 336dacb commit 022d36c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 12 deletions.
20 changes: 20 additions & 0 deletions src/io/InvalidHandleException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?hh // strict
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/


namespace HH\Lib\Experimental\IO;

/** Indicates that an invalid handle was used or requested.
*
* For example, calling `IO\requestError()` throws this exception in an
* HTTP request, as the model only defines input and output streams, not an
* error stream.
*/
final class InvalidHandleException extends \Exception {}
36 changes: 30 additions & 6 deletions src/io/_Private/StdioHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,41 @@

final class StdioHandle extends NativeHandle {
<<__Memoize>>
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();
}
}
58 changes: 52 additions & 6 deletions src/io/stdio.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 022d36c

Please sign in to comment.