ZipStream-PHP icon indicating copy to clipboard operation
ZipStream-PHP copied to clipboard

Support callback for output

Open hschletz opened this issue 2 years ago • 1 comments

Description of the problem

Output is done to a stream, STDOUT by default, but can be set to an arbitrary stream.

Sometimes this is not flexible enough. For example, I want to write to 2 streams simultaneously. There may also be situations where a stream is not supported by a particular output method.

This could be solved by a custom stream wrapper or filter, but a callback for output would be much simpler. Would it be possible to support that?

Example code

// Example for new callback option. Overrides default stream output.
$options = new ZipStream\Option\Archive();
$options->setOutputCallback(function ($data) {
    write_output($data);
});

hschletz avatar Nov 05 '21 12:11 hschletz

I would welcome an addition, that would support this the following way:

  • Implement a Stream Wrapper that takes the callback as an argument. (Basically the opposite of https://github.com/phly/psr7examples/blob/master/src/CallbackStream.php)
  • Implement a method as you described, that will wrap that callback function into a stream and set that as the output stream

PRs are welcome for this.

maennchen avatar Feb 24 '22 11:02 maennchen