diff --git a/src/Writer/AbstractWriter.php b/src/Writer/AbstractWriter.php index eef34fb..a22e90f 100644 --- a/src/Writer/AbstractWriter.php +++ b/src/Writer/AbstractWriter.php @@ -13,6 +13,12 @@ abstract class AbstractWriter implements WriterInterface /** @var resource Pointer to the file/stream we will write to */ protected $filePointer; + /** + * @var bool faut-il garder ouvert le flux quand {@link self::close()} est + * appelé? + */ + protected bool $dontCloseFilePointer = false; + /** @var string document creator */ protected string $creator = 'OpenSpout'; @@ -20,7 +26,7 @@ abstract class AbstractWriter implements WriterInterface protected static string $headerContentType; /** @var string Path to the output file */ - private string $outputFilePath; + private ?string $outputFilePath; /** @var bool Indicates whether the writer has been opened or not */ private bool $isWriterOpened = false; @@ -51,6 +57,20 @@ abstract class AbstractWriter implements WriterInterface $this->isWriterOpened = true; } + final public function writeToStream($filePointer): void + { + if (!is_resource($filePointer)) { + throw new IOException("filePointer is not a resource"); + } + $this->outputFilePath = null; + + $this->filePointer = $filePointer; + $this->dontCloseFilePointer = true; + + $this->openWriter(); + $this->isWriterOpened = true; + } + /** * @codeCoverageIgnore * @@ -140,7 +160,9 @@ abstract class AbstractWriter implements WriterInterface $this->closeWriter(); - fclose($this->filePointer); + if (!$this->dontCloseFilePointer) { + fclose($this->filePointer); + } $this->isWriterOpened = false; }