-
-
Notifications
You must be signed in to change notification settings - Fork 110
Add CallbackStreamWrapper for custom ZIP output (#199) #363
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new CallbackStreamWrapper to allow ZIP output to be sent to any custom PHP callback, enabling streaming to multiple destinations and real-time progress tracking.
- Introduces the CallbackStreamWrapper class with the zipcb:// protocol support
- Adds comprehensive tests in CallbackOutputTest.php covering data forwarding, error handling, and stream statistics
- Updates documentation in README.md, Options.rst, and StreamOutput.rst to cover usage details
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
test/CallbackOutputTest.php | New tests validating the callback stream behavior and error cases |
src/Stream/CallbackStreamWrapper.php | Implements the callback stream wrapper class for ZIP streaming |
guides/StreamOutput.rst | Added examples for using CallbackStreamWrapper in documentation |
guides/Options.rst | Documents CallbackStreamWrapper as an option for outputStream |
README.md | Demonstrates CallbackStreamWrapper usage with code examples |
…te file permissions notation
…exception handling
Thanks for the PR, this looks amazing ❤️ I’m off organizing a festival this week and will have a closer look next week (Wednesday or a bit later). |
$zip = new ZipStream( | ||
outputStream: CallbackStreamWrapper::open(function (string $data) use (&$totalBytes) { | ||
$totalBytes += strlen($data); | ||
echo "Progress: {$totalBytes} bytes written\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this example since it outputs both data and status messages to the browser which will result in an invalid zip. Can we replace this with some dummy code like reportProgress($totalBytes);
or something like that?
.. code-block:: php | ||
|
||
// Example 3: Data transformation | ||
$zip = new ZipStream( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I would point people to do that. There's a builtin way in PHP to deal with this kind of thing: Stream Filters
So instead you would:
$fp = fopen('php://output', 'w');
stream_filter_append($fp, "str.base64");
$zip = new ZipStream(
outputStream: $fp,
// ...
);
Here's how to register new filters: https://www.php.net/manual/en/function.stream-filter-register.php
Summary
This PR introduces the
CallbackStreamWrapper
, a new stream wrapper (zipcb://) allowing ZIP output to be sent to any custom PHP callback function.Why
Requested originally in #199.
What changed
ZipStream\Stream\CallbackStreamWrapper
(zipcb://
protocol).CallbackStreamWrapper::open(callable $cb)
returns a writable resource for theoutputStream
option.README.md
,guides/Options.rst
,guides/StreamOutput.rst
.test/CallbackOutputTest.php
(data integrity, multi-stream, error handling, stats, large chunks, progress).Backward compatibility
No breaking changes. Existing code that passes a normal stream resource still works.
Closes #199