|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Redbitcz\Utils\IO; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use RuntimeException; |
| 9 | + |
| 10 | +class MemoryWriter implements IBufferWriter |
| 11 | +{ |
| 12 | + /** @var resource|null */ |
| 13 | + private $outputStream; |
| 14 | + |
| 15 | + /** @var resource|null */ |
| 16 | + private $errorStream; |
| 17 | + |
| 18 | + /** @var LazyStreamWriter */ |
| 19 | + private $writer; |
| 20 | + |
| 21 | + public function __construct() |
| 22 | + { |
| 23 | + $this->writer = new LazyStreamWriter( |
| 24 | + Closure::fromCallable([$this, 'createOutputStream']), |
| 25 | + Closure::fromCallable([$this, 'createErrorStream']), |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + public function __destruct() |
| 30 | + { |
| 31 | + if ($this->hasOutputContent()) { |
| 32 | + fclose($this->outputStream); |
| 33 | + } |
| 34 | + |
| 35 | + if ($this->hasErrorContent()) { |
| 36 | + fclose($this->errorStream); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public function write(string $string): void |
| 41 | + { |
| 42 | + $this->writer->write($string); |
| 43 | + } |
| 44 | + |
| 45 | + public function error(string $string): void |
| 46 | + { |
| 47 | + $this->writer->error($string); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return resource |
| 52 | + */ |
| 53 | + private function createOutputStream() |
| 54 | + { |
| 55 | + return $this->outputStream = $this->createMemoryStream(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return resource |
| 60 | + */ |
| 61 | + private function createErrorStream() |
| 62 | + { |
| 63 | + return $this->errorStream = $this->createMemoryStream(); |
| 64 | + } |
| 65 | + |
| 66 | + public function hasOutputContent(): bool |
| 67 | + { |
| 68 | + return isset($this->outputStream); |
| 69 | + } |
| 70 | + |
| 71 | + public function getOutputContent(): string |
| 72 | + { |
| 73 | + return $this->hasOutputContent() ? $this->getStreamContent($this->outputStream) : ''; |
| 74 | + } |
| 75 | + |
| 76 | + public function hasErrorContent(): bool |
| 77 | + { |
| 78 | + return isset($this->errorStream); |
| 79 | + } |
| 80 | + |
| 81 | + public function getErrorContent(): string |
| 82 | + { |
| 83 | + return $this->hasErrorContent() ? $this->getStreamContent($this->errorStream) : ''; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return resource |
| 88 | + */ |
| 89 | + private function createMemoryStream() |
| 90 | + { |
| 91 | + $fileHandler = fopen('php://memory', 'wb+'); |
| 92 | + |
| 93 | + if ($fileHandler === false) { |
| 94 | + throw new RuntimeException("Unable to create Memory stream (php://memory)"); |
| 95 | + } |
| 96 | + |
| 97 | + return $fileHandler; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param resource $fileHandler |
| 102 | + * @return string |
| 103 | + */ |
| 104 | + private function getStreamContent($fileHandler): string |
| 105 | + { |
| 106 | + return stream_get_contents($fileHandler, -1, 0); |
| 107 | + } |
| 108 | +} |
0 commit comments