From 9afddf2d1ba8e239792cb665e96507d9e0b3f195 Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Thu, 31 Mar 2022 09:39:46 +0200 Subject: [PATCH 1/2] Delete temporary files after conversion/output --- src/DocumentReplacer.php | 42 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/DocumentReplacer.php b/src/DocumentReplacer.php index fc2b1dc..428bba4 100644 --- a/src/DocumentReplacer.php +++ b/src/DocumentReplacer.php @@ -55,31 +55,45 @@ public function save(string $outputPath): string { $temporaryFile = $this->templateProcessor->save(); - if (! file_exists($temporaryFile) || ! filesize($temporaryFile)) { - throw new Exception("Template processor failed to output valid file to {$temporaryFile}"); - } + try { + if (! file_exists($temporaryFile) || ! filesize($temporaryFile)) { + throw new Exception("Template processor failed to output valid file to {$temporaryFile}"); + } + + if ($this->converter) { + /** @var AbstractConverter $class */ + $class = $this->converter; - if ($this->converter) { - /** @var AbstractConverter $class */ - $class = $this->converter; + $class::make($this, $this->converterOptions)->convert($temporaryFile, $outputPath); - $class::make($this, $this->converterOptions)->convert($temporaryFile, $outputPath); + if (! file_exists($outputPath) || ! filesize($outputPath)) { + throw new Exception("Converter failed to output valid file to {$outputPath}"); + } - if (! file_exists($outputPath) || ! filesize($outputPath)) { - throw new Exception("Converter failed to output valid file to {$outputPath}"); + return $outputPath; } + rename($temporaryFile, $outputPath); + return $outputPath; + } finally { + if (file_exists($temporaryFile)) { + unlink($temporaryFile); + } } - - rename($temporaryFile, $outputPath); - - return $outputPath; } public function output(): string { - return file_get_contents($this->save(tempnam(sys_get_temp_dir(), 'document-replacer-output'))); + $temporaryFile = tempnam(sys_get_temp_dir(), 'document-replacer-output'); + + try { + return file_get_contents($this->save($temporaryFile)); + } finally { + if (file_exists($temporaryFile)) { + unlink($temporaryFile); + } + } } public function templateProcessor(): TemplateProcessor From 4b061fdbd6c04ef663310aac11e0d4408f529da5 Mon Sep 17 00:00:00 2001 From: mbardelmeijer Date: Thu, 31 Mar 2022 07:40:12 +0000 Subject: [PATCH 2/2] Fix styling --- src/Converters/AbstractConverter.php | 3 ++- src/ValueTypes/Image.php | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Converters/AbstractConverter.php b/src/Converters/AbstractConverter.php index 619279c..e5bfba2 100644 --- a/src/Converters/AbstractConverter.php +++ b/src/Converters/AbstractConverter.php @@ -6,7 +6,8 @@ abstract class AbstractConverter { - private function __construct(protected DocumentReplacer $documentReplacer, protected array $options) { + private function __construct(protected DocumentReplacer $documentReplacer, protected array $options) + { } public static function make(DocumentReplacer $documentReplacer, array $options = []): self diff --git a/src/ValueTypes/Image.php b/src/ValueTypes/Image.php index c65f6d6..e9e216e 100644 --- a/src/ValueTypes/Image.php +++ b/src/ValueTypes/Image.php @@ -12,7 +12,6 @@ class Image private function __construct(private Closure|string $image) { - } public static function forPath(string $path): self