Skip to content

Commit f8059d8

Browse files
committed
Less Complicated Approach
1 parent ed1d5b8 commit f8059d8

File tree

2 files changed

+46
-79
lines changed

2 files changed

+46
-79
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,13 @@ class TemplateProcessor
9999

100100
protected static $macroClosingChars = '}';
101101

102-
/**
103-
* Delete temp file at destruct?
104-
*
105-
* @var bool
106-
*/
107-
protected $deleteAtDestruct = false;
108-
109102
/**
110103
* @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception
111104
*
112105
* @param string $documentTemplate The fully qualified template filename
113-
* @param bool $deleteAtDestruct Delete temp file at destruct
114106
*/
115-
public function __construct($documentTemplate, $deleteAtDestruct = false)
107+
public function __construct($documentTemplate)
116108
{
117-
$this->deleteAtDestruct = $deleteAtDestruct;
118109
// Temporary document filename initialization
119110
$this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord');
120111
if (false === $this->tempDocumentFilename) {
@@ -155,18 +146,6 @@ public function __destruct()
155146
// Nothing to do here.
156147
}
157148
}
158-
// Temporary file
159-
if ($this->deleteAtDestruct && $this->tempDocumentFilename && file_exists($this->tempDocumentFilename)) {
160-
unlink($this->tempDocumentFilename);
161-
}
162-
}
163-
164-
public function __wakeup(): void
165-
{
166-
$this->tempDocumentFilename = '';
167-
$this->zipClass = null;
168-
169-
throw new Exception('unserialize not permitted for this class');
170149
}
171150

172151
/**
@@ -1003,6 +982,12 @@ public function save()
1003982
return $this->tempDocumentFilename;
1004983
}
1005984

985+
/** @return string */
986+
public function getTempDocumentFilename()
987+
{
988+
return $this->tempDocumentFilename;
989+
}
990+
1006991
/**
1007992
* @param string $fileName
1008993
* @param string $xml

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Exception;
2222
use PhpOffice\PhpWord\Element\Text;
2323
use PhpOffice\PhpWord\Element\TextRun;
24-
use PhpOffice\PhpWord\Exception\Exception as WordException;
2524
use PhpOffice\PhpWord\IOFactory;
2625
use PhpOffice\PhpWord\PhpWord;
2726
use PhpOffice\PhpWord\Settings;
@@ -38,45 +37,40 @@
3837
*/
3938
final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
4039
{
41-
private const DELETE_AT_DESTRUCT = true;
40+
/** @var ?TemplateProcessor */
41+
private $templateProcessor;
4242

43-
/**
44-
* Construct test.
45-
*
46-
* @covers ::__construct
47-
* @covers ::__destruct
48-
*/
49-
public function testTheConstruct(): void
43+
private function getTemplateProcessor(string $filename): TemplateProcessor
5044
{
51-
$object = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx', self::DELETE_AT_DESTRUCT);
52-
self::assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessor', $object);
53-
self::assertEquals([], $object->getVariables());
54-
$filename1 = $object->save();
55-
self::assertFileExists($filename1);
56-
unset($object);
57-
gc_collect_cycles();
58-
if (method_exists(self::class, 'assertFileDoesNotExist')) {
59-
self::assertFileDoesNotExist($filename1);
60-
} else {
61-
self::assertFileNotExists($filename1);
45+
$this->templateProcessor = new TemplateProcessor($filename);
46+
47+
return $this->templateProcessor;
48+
}
49+
50+
protected function tearDown(): void
51+
{
52+
if ($this->templateProcessor !== null) {
53+
$filename = $this->templateProcessor->getTempDocumentFilename();
54+
$this->templateProcessor = null;
55+
if (file_exists($filename)) {
56+
@unlink($filename);
57+
}
6258
}
6359
}
6460

6561
/**
66-
* Do not delete tempfile test.
62+
* Construct test.
6763
*
6864
* @covers ::__construct
6965
* @covers ::__destruct
7066
*/
71-
public function testDoNotDeleteTempfile(): void
67+
public function testTheConstruct(): void
7268
{
73-
$object = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
69+
$object = $this->getTemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
70+
self::assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessor', $object);
71+
self::assertEquals([], $object->getVariables());
7472
$filename1 = $object->save();
7573
self::assertFileExists($filename1);
76-
unset($object);
77-
gc_collect_cycles();
78-
self::assertFileExists($filename1);
79-
@unlink($filename1);
8074
}
8175

8276
/**
@@ -135,7 +129,7 @@ public function xtestTemplateCanBeSavedInTemporaryLocation(string $templateFqfn,
135129
public function testXslStyleSheetCanBeApplied(): void
136130
{
137131
$templateFqfn = __DIR__ . '/_files/templates/with_table_macros.docx';
138-
$templateProcessor = new TemplateProcessor($templateFqfn, self::DELETE_AT_DESTRUCT);
132+
$templateProcessor = $this->getTemplateProcessor($templateFqfn);
139133

140134
$actualDocumentFqfn = $this->xtestTemplateCanBeSavedInTemporaryLocation($templateFqfn, $templateProcessor);
141135
$expectedDocumentFqfn = __DIR__ . '/_files/documents/without_table_macros.docx';
@@ -179,7 +173,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue
179173
$this->expectExceptionMessage('Could not set values for the given XSL style sheet parameters.');
180174
}
181175

182-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx', self::DELETE_AT_DESTRUCT);
176+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
183177

184178
$xslDomDocument = new DOMDocument();
185179
$xslDomDocument->load(__DIR__ . '/_files/xsl/passthrough.xsl');
@@ -200,7 +194,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
200194
{
201195
$this->expectException(\PhpOffice\PhpWord\Exception\Exception::class);
202196
$this->expectExceptionMessage('Could not load the given XML document.');
203-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/corrupted_main_document_part.docx', self::DELETE_AT_DESTRUCT);
197+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/corrupted_main_document_part.docx');
204198

205199
$xslDomDocument = new DOMDocument();
206200
$xslDomDocument->load(__DIR__ . '/_files/xsl/passthrough.xsl');
@@ -219,7 +213,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
219213
*/
220214
public function testDeleteRow(): void
221215
{
222-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/delete-row.docx', self::DELETE_AT_DESTRUCT);
216+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/delete-row.docx');
223217

224218
self::assertEquals(
225219
['deleteMe', 'deleteMeToo'],
@@ -245,7 +239,7 @@ public function testDeleteRow(): void
245239
*/
246240
public function testCloneRow(): void
247241
{
248-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx', self::DELETE_AT_DESTRUCT);
242+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');
249243

250244
self::assertEquals(
251245
['tableHeader', 'userId', 'userName', 'userLocation'],
@@ -269,7 +263,7 @@ public function testCloneRow(): void
269263
*/
270264
public function testCloneRowWithCustomMacro(): void
271265
{
272-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx', self::DELETE_AT_DESTRUCT);
266+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx');
273267

274268
$templateProcessor->setMacroOpeningChars('{#');
275269
$templateProcessor->setMacroClosingChars('#}');
@@ -426,7 +420,7 @@ public function testCloneRowAndSetValuesWithCustomMacro(): void
426420
*/
427421
public function testMacrosCanBeReplacedInHeaderAndFooter(): void
428422
{
429-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx', self::DELETE_AT_DESTRUCT);
423+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');
430424

431425
self::assertEquals(['documentContent', 'headerValue:100:100', 'footerValue'], $templateProcessor->getVariables());
432426

@@ -447,7 +441,7 @@ public function testMacrosCanBeReplacedInHeaderAndFooter(): void
447441
*/
448442
public function testCustomMacrosCanBeReplacedInHeaderAndFooter(): void
449443
{
450-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx', self::DELETE_AT_DESTRUCT);
444+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx');
451445
$templateProcessor->setMacroOpeningChars('{{');
452446
$templateProcessor->setMacroClosingChars('}}');
453447

@@ -469,7 +463,7 @@ public function testCustomMacrosCanBeReplacedInHeaderAndFooter(): void
469463
*/
470464
public function testSetValue(): void
471465
{
472-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx', self::DELETE_AT_DESTRUCT);
466+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');
473467
Settings::setOutputEscapingEnabled(true);
474468
$helloworld = "hello\nworld";
475469
$templateProcessor->setValue('userName', $helloworld);
@@ -484,7 +478,7 @@ public function testSetValue(): void
484478
*/
485479
public function testSetValueWithCustomMacro(): void
486480
{
487-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx', self::DELETE_AT_DESTRUCT);
481+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx');
488482
$templateProcessor->setMacroChars('{#', '#}');
489483
Settings::setOutputEscapingEnabled(true);
490484
$helloworld = "hello\nworld";
@@ -644,7 +638,7 @@ public function testSetValuesWithCustomMacro(): void
644638
*/
645639
public function testSetImageValue(): void
646640
{
647-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx', self::DELETE_AT_DESTRUCT);
641+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');
648642
$imagePath = __DIR__ . '/_files/images/earth.jpg';
649643

650644
$variablesReplace = [
@@ -724,7 +718,7 @@ public function testSetImageValue(): void
724718
*/
725719
public function testCloneDeleteBlock(): void
726720
{
727-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-delete-block.docx', self::DELETE_AT_DESTRUCT);
721+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-delete-block.docx');
728722

729723
self::assertEquals(
730724
['DELETEME', '/DELETEME', 'CLONEME', 'blockVariable', '/CLONEME'],
@@ -764,7 +758,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresent()
764758
$templatePath = 'test.docx';
765759
$objWriter->save($templatePath);
766760

767-
$templateProcessor = new TemplateProcessor($templatePath, self::DELETE_AT_DESTRUCT);
761+
$templateProcessor = $this->getTemplateProcessor($templatePath);
768762
$variableCount = $templateProcessor->getVariableCount();
769763
unlink($templatePath);
770764

@@ -801,7 +795,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresentWi
801795
$templatePath = 'test.docx';
802796
$objWriter->save($templatePath);
803797

804-
$templateProcessor = new TemplateProcessor($templatePath, self::DELETE_AT_DESTRUCT);
798+
$templateProcessor = $this->getTemplateProcessor($templatePath);
805799
$templateProcessor->setMacroChars('{{', '}}');
806800
$variableCount = $templateProcessor->getVariableCount();
807801
unlink($templatePath);
@@ -839,7 +833,7 @@ public function testCloneBlockCanCloneABlockTwice(): void
839833
$objWriter->save($templatePath);
840834

841835
// replace placeholders and save the file
842-
$templateProcessor = new TemplateProcessor($templatePath, self::DELETE_AT_DESTRUCT);
836+
$templateProcessor = $this->getTemplateProcessor($templatePath);
843837
$templateProcessor->setValue('title', 'Some title');
844838
$templateProcessor->cloneBlock('subreport', 2);
845839
$templateProcessor->setValue('subreport.id', '123', 1);
@@ -892,7 +886,7 @@ public function testCloneBlockCanCloneABlockTwiceWithCustomMacro(): void
892886
$objWriter->save($templatePath);
893887

894888
// replace placeholders and save the file
895-
$templateProcessor = new TemplateProcessor($templatePath, self::DELETE_AT_DESTRUCT);
889+
$templateProcessor = $this->getTemplateProcessor($templatePath);
896890
$templateProcessor->setMacroChars('{{', '}}');
897891
$templateProcessor->setValue('title', 'Some title');
898892
$templateProcessor->cloneBlock('subreport', 2);
@@ -1181,7 +1175,7 @@ public function testFixBrokenMacrosWithCustomMacro(): void
11811175
*/
11821176
public function testMainPartNameDetection(): void
11831177
{
1184-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx', self::DELETE_AT_DESTRUCT);
1178+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx');
11851179

11861180
$variables = ['test'];
11871181

@@ -1193,7 +1187,7 @@ public function testMainPartNameDetection(): void
11931187
*/
11941188
public function testMainPartNameDetectionWithCustomMacro(): void
11951189
{
1196-
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx', self::DELETE_AT_DESTRUCT);
1190+
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx');
11971191
$templateProcessor->setMacroOpeningChars('{#');
11981192
$templateProcessor->setMacroClosingChars('#}');
11991193
$variables = ['test'];
@@ -1453,18 +1447,6 @@ public function testShouldMakeFieldsUpdateOnOpen(): void
14531447
self::assertStringContainsString('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
14541448
}
14551449

1456-
/**
1457-
* Should not allow unserialize to avoid malware.
1458-
*/
1459-
public function testUnserialize(): void
1460-
{
1461-
$this->expectException(WordException::class);
1462-
$this->expectExceptionMessage('unserialize not permitted');
1463-
$object = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx', self::DELETE_AT_DESTRUCT);
1464-
$serialized = serialize($object);
1465-
$object2 = unserialize($serialized);
1466-
}
1467-
14681450
public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void
14691451
{
14701452
$settingsPart = '<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">

0 commit comments

Comments
 (0)