Description
Describe the Bug
Hi,
thanks for your work on this library!
A recent change introduced a bug that I can workaround, but I am not sure if this is intended behavior, as it comes pretty unexpected. It was introduced in #2475, which added TemplateProcessor::__destruct()
in which the temporary file is deleted.
PHPWord/src/PhpWord/TemplateProcessor.php
Lines 141 to 153 in 32a451d
This change basically disallows to use the temporary file while the process is still running, unless you keep the TemplateProcessor instance alive. Temporary files are, to my knowledge, always deleted by the engine after the scripts end, so this code seems to be superfluous.
Steps to Reproduce
This is my code:
$template = new TemplateProcessor('template.docx');
$tplFile = $template->save();
return $this->getFileResponse(new Stream($tplFile), 'export.docx');
This worked fine until I upgraded to 1.2
You save a Template and return the temporary filename, all from the context of a method that does uses a local instance of TemplateProcessor. As soon as your TemplateProcessor instance gets destructed (here when returning the response from the method) the temporary file now gets deleted.
I can work around the problem with this code:
$tplFile = @tempnam(sys_get_temp_dir(), 'foo-bar-docx');
$template->saveAs($tplFile);
return $this->getFileResponse(new Stream($tplFile), 'export.docx');
So basically using my own temporary file. I feel that this change makes the save() method kind of useless.
Expected Behavior
Do not delete the temporary file while I am still using it.
Trust the user of your library: I called save()
, I can/will handle the file myself.
So my proposal is: remove the destruct method again.
Current Behavior
The temporary file is deleted upon destruct of the TemplateProcessor instance, which is likely before the file is used (here sent to the user).
Context
- PHP Version: 8.2
- PHPWord Version: 1.2