Skip to content

Commit

Permalink
Merge pull request #4 from enflow/deleteTemporaryFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
mbardelmeijer authored Mar 31, 2022
2 parents 1692607 + 4b061fd commit e61ed01
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/DocumentReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e61ed01

Please sign in to comment.