Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,9 @@ public function __construct($documentTemplate)
// Temporary document content extraction
$this->zipClass = new ZipArchive();
$this->zipClass->open($this->tempDocumentFilename);
$index = 1;
while (false !== $this->zipClass->locateName($this->getHeaderName($index))) {
$this->tempDocumentHeaders[$index] = $this->readPartWithRels($this->getHeaderName($index));
++$index;
}
$index = 1;
while (false !== $this->zipClass->locateName($this->getFooterName($index))) {
$this->tempDocumentFooters[$index] = $this->readPartWithRels($this->getFooterName($index));
++$index;
}

$this->tempDocumentHeaders = $this->loadDocumentParts('word/header');
$this->tempDocumentFooters = $this->loadDocumentParts('word/footer');

$this->tempDocumentMainPart = $this->readPartWithRels($this->getMainPartName());
$this->tempDocumentSettingsPart = $this->readPartWithRels($this->getSettingsPartName());
Expand Down Expand Up @@ -1504,4 +1497,26 @@ public function getTempDocumentFilename(): string
{
return $this->tempDocumentFilename;
}

/**
* Check existing files inside zip from prefix.
*/
private function loadDocumentParts(string $prefix): array
{
$parts = [];
$total = $this->zipClass->numFiles;

for ($i = 0; $i < $total; ++$i) {
$name = $this->zipClass->getNameIndex($i);

if (preg_match('#^' . preg_quote($prefix, '#') . '(\d+)\.xml$#', $name, $m)) {
$idx = (int) $m[1];
$parts[$idx] = $this->readPartWithRels($name);
}
}

ksort($parts);

return $parts;
}
}