Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class TransformingStream {
- protected $input;
- protected $output;
- // Chunk size...
- protected $chunkSize = 4096;
- // Find/replace operations
- protected $search = [];
- protected $replace = [];
- /** @var callable|null */
- protected $progress;
- /**
- * TransformingStream constructor.
- *
- * @param resource $input
- * @param resource $output
- */
- public function __construct($input, $output)
- {
- $this->input = $input;
- $this->output = $output;
- }
- /**
- * @return int
- */
- public function getChunkSize(): int
- {
- return $this->chunkSize;
- }
- /**
- * @param int $chunkSize
- * @return \TransformingStream
- */
- public function setChunkSize(int $chunkSize): self
- {
- $this->chunkSize = $chunkSize;
- return $this;
- }
- /**
- * @param callable $progress The onProgress callback
- */
- public function setProgress(callable $progress)
- {
- $this->progress = $progress;
- }
- public function transform(string $pattern, string $replace): self
- {
- $this->search[] = $pattern;
- $this->replace[] = $replace;
- return $this;
- }
- public function process()
- {
- // Early read to fill to twice the buffer size, if possible
- $buffer = fread($this->input, $this->chunkSize);
- $chunk = 1;
- $cb = $this->progress;
- do {
- $buffer .= fread($this->input, $this->chunkSize);
- // Apply transforms to the whole buffer to fix any operations skipped by
- // boundaries of the buffer chunks.
- $buffer = preg_replace($this->search, $this->replace, $buffer);
- // pull off the output chunk...
- fwrite($this->output, substr($buffer, 0, $this->chunkSize));
- $buffer = substr($buffer, $this->chunkSize);
- if ($cb) {
- $cb($chunk);
- }
- $chunk++;
- } while (!feof($this->input));
- if ($buffer) {
- fwrite($this->output, $buffer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement