Skip to content

Commit f33abcd

Browse files
author
Roman Syroeshko
committed
Performance improvement for #513.
1 parent 75022c5 commit f33abcd

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,20 @@ public function __construct($documentTemplate)
8282
$this->zipClass = new ZipArchive();
8383
$this->zipClass->open($this->temporaryDocumentFilename);
8484
$index = 1;
85-
while ($this->zipClass->locateName($this->getHeaderName($index)) !== false) {
86-
$this->temporaryDocumentHeaders[$index] = $this->zipClass->getFromName($this->getHeaderName($index));
85+
while (false !== $this->zipClass->locateName($this->getHeaderName($index))) {
86+
$this->temporaryDocumentHeaders[$index] = $this->fixBrokenMacros(
87+
$this->zipClass->getFromName($this->getHeaderName($index))
88+
);
8789
$index++;
8890
}
8991
$index = 1;
90-
while ($this->zipClass->locateName($this->getFooterName($index)) !== false) {
91-
$this->temporaryDocumentFooters[$index] = $this->zipClass->getFromName($this->getFooterName($index));
92+
while (false !== $this->zipClass->locateName($this->getFooterName($index))) {
93+
$this->temporaryDocumentFooters[$index] = $this->fixBrokenMacros(
94+
$this->zipClass->getFromName($this->getFooterName($index))
95+
);
9296
$index++;
9397
}
94-
$this->temporaryDocumentMainPart = $this->zipClass->getFromName('word/document.xml');
98+
$this->temporaryDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName('word/document.xml'));
9599
}
96100

97101
/**
@@ -175,7 +179,7 @@ public function getVariables()
175179
*/
176180
public function cloneRow($search, $numberOfClones)
177181
{
178-
if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
182+
if ('${' !== substr($search, 0, 2) && '}' !== substr($search, -1)) {
179183
$search = '${' . $search . '}';
180184
}
181185

@@ -338,6 +342,30 @@ public function saveAs($fileName)
338342
rename($tempFileName, $fileName);
339343
}
340344

345+
/**
346+
* Finds parts of broken macros and sticks them together.
347+
* Macros, while being edited, could be implicitly broken by some of the word processors.
348+
*
349+
* @since 0.13.0
350+
*
351+
* @param string $documentPart The document part in XML representation.
352+
*
353+
* @return string
354+
*/
355+
protected function fixBrokenMacros($documentPart) {
356+
$fixedDocumentPart = $documentPart;
357+
358+
$pattern = '|\$\{([^\}]+)\}|U';
359+
preg_match_all($pattern, $fixedDocumentPart, $matches);
360+
foreach ($matches[0] as $value) {
361+
$valueCleaned = preg_replace('/<[^>]+>/', '', $value);
362+
$valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned);
363+
$fixedDocumentPart = str_replace($value, $valueCleaned, $fixedDocumentPart);
364+
}
365+
366+
return $fixedDocumentPart;
367+
}
368+
341369
/**
342370
* Find and replace placeholders in the given XML section.
343371
*
@@ -349,14 +377,6 @@ public function saveAs($fileName)
349377
*/
350378
protected function setValueForPart($documentPartXML, $search, $replace, $limit)
351379
{
352-
$pattern = '|\$\{([^\}]+)\}|U';
353-
preg_match_all($pattern, $documentPartXML, $matches);
354-
foreach ($matches[0] as $value) {
355-
$valueCleaned = preg_replace('/<[^>]+>/', '', $value);
356-
$valueCleaned = preg_replace('/<\/[^>]+>/', '', $valueCleaned);
357-
$documentPartXML = str_replace($value, $valueCleaned, $documentPartXML);
358-
}
359-
360380
if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
361381
$search = '${' . $search . '}';
362382
}

0 commit comments

Comments
 (0)