-
-
Notifications
You must be signed in to change notification settings - Fork 79
feat(php_write): A binary-safe way to write to PHP's stdout/stderr #508 #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kakserpom
wants to merge
1
commit into
extphprs:master
Choose a base branch
from
kakserpom:508_php_write
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # Output | ||
|
|
||
| `ext-php-rs` provides several macros and functions for writing output to PHP's | ||
| stdout and stderr streams. These are essential when your extension needs to | ||
| produce output that integrates with PHP's output buffering system. | ||
|
|
||
| ## Text Output | ||
|
|
||
| For regular text output (strings without NUL bytes), use the `php_print!` and | ||
| `php_println!` macros. These work similarly to Rust's `print!` and `println!` | ||
| macros. | ||
|
|
||
| ### `php_print!` | ||
|
|
||
| Prints to PHP's standard output without a trailing newline. | ||
|
|
||
| ```rust,ignore | ||
| use ext_php_rs::prelude::*; | ||
|
|
||
| #[php_function] | ||
| pub fn greet(name: &str) { | ||
| php_print!("Hello, {}!", name); | ||
| } | ||
| ``` | ||
|
|
||
| ### `php_println!` | ||
|
|
||
| Prints to PHP's standard output with a trailing newline. | ||
|
|
||
| ```rust,ignore | ||
| use ext_php_rs::prelude::*; | ||
|
|
||
| #[php_function] | ||
| pub fn greet(name: &str) { | ||
| php_println!("Hello, {}!", name); | ||
| } | ||
| ``` | ||
|
|
||
| > **Note:** `php_print!` and `php_println!` will panic if the string contains | ||
| > NUL bytes (`\0`). For binary-safe output, use `php_output!` or `php_write!`. | ||
|
|
||
| ## Binary-Safe Output | ||
|
|
||
| When working with binary data that may contain NUL bytes, use the binary-safe | ||
| output functions. These are essential for outputting raw bytes, binary file | ||
| contents, or any data that might contain `\0` characters. | ||
|
|
||
| ### `php_output!` | ||
|
|
||
| Writes binary data to PHP's output stream. This macro is **both binary-safe AND | ||
| respects PHP's output buffering** (`ob_start()`). This is usually what you want | ||
| for binary output. | ||
|
|
||
| ```rust,ignore | ||
| use ext_php_rs::prelude::*; | ||
|
|
||
| #[php_function] | ||
| pub fn output_binary() -> i64 { | ||
| // Write binary data with NUL bytes - will be captured by ob_start() | ||
| let bytes_written = php_output!(b"Hello\x00World"); | ||
| bytes_written as i64 | ||
| } | ||
| ``` | ||
|
|
||
| ### `php_write!` | ||
|
|
||
| Writes binary data directly to the SAPI output, **bypassing PHP's output | ||
| buffering**. This macro is binary-safe but output will NOT be captured by | ||
| `ob_start()`. The "ub" in `ub_write` stands for "unbuffered". | ||
|
|
||
| ```rust,ignore | ||
| use ext_php_rs::prelude::*; | ||
|
|
||
| #[php_function] | ||
| pub fn output_binary() -> i64 { | ||
| // Write a byte literal | ||
| php_write!(b"Hello World").expect("write failed"); | ||
|
|
||
| // Write binary data with NUL bytes (would panic with php_print!) | ||
| let bytes_written = php_write!(b"Hello\x00World").expect("write failed"); | ||
|
|
||
| // Write a byte slice | ||
| let data: &[u8] = &[0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello" | ||
| php_write!(data).expect("write failed"); | ||
|
|
||
| bytes_written as i64 | ||
| } | ||
| ``` | ||
|
|
||
| The macro returns a `Result<usize>` with the number of bytes written, which can | ||
| be useful for verifying that all data was output successfully. The error case | ||
| occurs when the SAPI's `ub_write` function is not available. | ||
|
|
||
| ## Function API | ||
|
|
||
| In addition to macros, you can use the underlying functions directly: | ||
|
|
||
| | Function | Binary-Safe | Output Buffering | Description | | ||
| |----------|-------------|------------------|-------------| | ||
| | `zend::printf()` | No | Yes | Printf-style output (used by `php_print!`) | | ||
| | `zend::output_write()` | Yes | Yes | Binary-safe buffered output | | ||
| | `zend::write()` | Yes | No | Binary-safe unbuffered output | | ||
|
|
||
| ### Example using functions directly | ||
|
|
||
| ```rust,ignore | ||
| use ext_php_rs::zend::output_write; | ||
|
|
||
| fn output_data(data: &[u8]) { | ||
| let bytes_written = output_write(data); | ||
| if bytes_written != data.len() { | ||
| eprintln!("Warning: incomplete write"); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Comparison | ||
|
|
||
| | Macro | Binary-Safe | Output Buffering | Supports Formatting | | ||
| |-------|-------------|------------------|---------------------| | ||
| | `php_print!` | No | Yes | Yes | | ||
| | `php_println!` | No | Yes | Yes | | ||
| | `php_output!` | Yes | Yes | No | | ||
| | `php_write!` | Yes | No | No | | ||
|
|
||
| ## When to Use Each | ||
|
|
||
| - **`php_print!` / `php_println!`**: Use for text output with format strings, | ||
| similar to Rust's `print!` and `println!`. Best for human-readable messages. | ||
|
|
||
| - **`php_output!`**: Use for binary data that needs to work with PHP's output | ||
| buffering. This is the recommended choice for most binary output needs. | ||
|
|
||
| - **`php_write!`**: Use when you need direct, unbuffered output that bypasses | ||
| PHP's output layer. Useful for low-level SAPI interaction or when output | ||
| buffering must be avoided. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.