From e5c88e899ce7e6cb786c72c2aa0ade55f9862433 Mon Sep 17 00:00:00 2001 From: colorful-bubbles <36796574+colorful-bubbles@users.noreply.github.com> Date: Sun, 11 Nov 2018 21:28:56 +0100 Subject: [PATCH] Added posibility to create "in memory" documents Open writer with openToMemory(), then close it with closeAndGetStream() which will return the stream pointer. After that you have to do fclose() on that pointer. --- src/Spout/Writer/WriterAbstract.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Spout/Writer/WriterAbstract.php b/src/Spout/Writer/WriterAbstract.php index 4bcc407..0474202 100644 --- a/src/Spout/Writer/WriterAbstract.php +++ b/src/Spout/Writer/WriterAbstract.php @@ -142,6 +142,33 @@ abstract class WriterAbstract implements WriterInterface return $this; } + + public function openToMemory() + { + $this->filePointer = $this->globalFunctionsHelper->fopen('php://temp', 'r+'); + $this->throwIfFilePointerIsNotAvailable(); + + $this->openWriter(); + $this->isWriterOpened = true; + + return $this; + } + + public function closeAndGetStream() + { + if (!$this->isWriterOpened) { + return; + } + + $this->closeWriter(); + + $this->isWriterOpened = false; + + rewind($this->filePointer); + fpassthru($this->filePointer); + + return $this->filePointer; + } /** * Checks if the pointer to the file/stream to write to is available.