Skip to content

Commit abe4268

Browse files
committed
Php8.5 __wakeup Deprecation Implemented
No deprecation message if there is __unserialize in same class, so no need to duplicate XMLWriter.
1 parent 3b67bdd commit abe4268

File tree

10 files changed

+205
-466
lines changed

10 files changed

+205
-466
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,6 @@ parameters:
400400
count: 1
401401
path: src/PhpWord/Shared/Microsoft/PasswordEncoder.php
402402

403-
-
404-
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLWriter\\:\\:getData\\(\\) should return string but returns string\\|false\\.$#"
405-
count: 1
406-
path: src/PhpWord/Shared/XMLWriter.php
407-
408403
-
409404
message: "#^Call to method add\\(\\) on an unknown class PclZip\\.$#"
410405
count: 2

phpstan-baseline.php7.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,6 @@ parameters:
385385
count: 1
386386
path: src/PhpWord/Shared/Microsoft/PasswordEncoder.php
387387

388-
-
389-
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLWriter\\:\\:getData\\(\\) should return string but returns string\\|false\\.$#"
390-
count: 1
391-
path: src/PhpWord/Shared/XMLWriter.php
392-
393388
-
394389
message: "#^Call to method add\\(\\) on an unknown class PclZip\\.$#"
395390
count: 2

phpstan-baseline.php73.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,6 @@ parameters:
385385
count: 1
386386
path: src/PhpWord/Shared/Microsoft/PasswordEncoder.php
387387

388-
-
389-
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLWriter\\:\\:getData\\(\\) should return string but returns string\\|false\\.$#"
390-
count: 1
391-
path: src/PhpWord/Shared/XMLWriter.php
392-
393388
-
394389
message: "#^Call to method add\\(\\) on an unknown class PclZip\\.$#"
395390
count: 2

phpstan.neon.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ parameters:
1111
- */pclzip.lib.php
1212
- src/PhpWord/Reader/MsDoc.php
1313
- src/PhpWord/Shared/OLERead.php
14-
- src/PhpWord/Shared/XMLWriter.php
15-
- src/PhpWord/Shared/XMLWriter.withwakeup.php
16-
- src/PhpWord/Writer/PDF/MPDF.php
1714
bootstrapFiles:
1815
- tests/bootstrap.php
1916
treatPhpDocTypesAsCertain: false

phpstan.neon.php7.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ parameters:
1111
- */pclzip.lib.php
1212
- src/PhpWord/Reader/MsDoc.php
1313
- src/PhpWord/Shared/OLERead.php
14-
- src/PhpWord/Shared/XMLWriter.php
15-
- src/PhpWord/Shared/XMLWriter.withoutwakeup.php
16-
- src/PhpWord/Writer/PDF/MPDF.php
1714
bootstrapFiles:
1815
- tests/bootstrap.php
1916
treatPhpDocTypesAsCertain: false

phpstan.neon.php73.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ parameters:
1111
- */pclzip.lib.php
1212
- src/PhpWord/Reader/MsDoc.php
1313
- src/PhpWord/Shared/OLERead.php
14-
- src/PhpWord/Shared/XMLWriter.php
15-
- src/PhpWord/Shared/XMLWriter.withoutwakeup.php
16-
- src/PhpWord/Writer/PDF/MPDF.php
1714
bootstrapFiles:
1815
- tests/bootstrap.php
1916
reportUnmatchedIgnoredErrors: true

src/PhpWord/Shared/XMLWriter.php

Lines changed: 204 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,207 @@
11
<?php
22

3-
if (\PHP_VERSION_ID < 80300) {
4-
require_once __DIR__ . '/XMLWriter.withwakeup.php'; // @codeCoverageIgnore
5-
} else {
6-
require_once __DIR__ . '/XMLWriter.withoutwakeup.php';
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWord\Shared;
20+
21+
use PhpOffice\PhpWord\Exception\Exception as WordException;
22+
23+
/**
24+
* XMLWriter.
25+
*
26+
* @method bool endElement()
27+
* @method mixed flush(bool $empty = null)
28+
* @method bool openMemory()
29+
* @method string outputMemory(bool $flush = null)
30+
* @method bool setIndent(bool $indent)
31+
* @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
32+
* @method bool startElement(string $name)
33+
* @method bool text(string $content)
34+
* @method bool writeCData(string $content)
35+
* @method bool writeComment(string $content)
36+
* @method bool writeElement(string $name, string $content = null)
37+
* @method bool writeRaw(string $content)
38+
*
39+
* @codeCoverageIgnore
40+
*/
41+
class XMLWriter extends \XMLWriter
42+
{
43+
/** Temporary storage method */
44+
const STORAGE_MEMORY = 1;
45+
const STORAGE_DISK = 2;
46+
47+
/**
48+
* Temporary filename.
49+
*
50+
* @var string
51+
*/
52+
private $tempFileName = '';
53+
54+
/** @var int */
55+
private $hash;
56+
57+
/**
58+
* Create a new \PhpOffice\PhpWord\Shared\XMLWriter instance.
59+
*
60+
* @param int $pTemporaryStorage Temporary storage location
61+
* @param string $pTemporaryStorageDir Temporary storage folder
62+
* @param bool $compatibility
63+
*/
64+
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false)
65+
{
66+
$this->hash = spl_object_id($this);
67+
// Open temporary storage
68+
if ($pTemporaryStorage == self::STORAGE_MEMORY) {
69+
$this->openMemory();
70+
} else {
71+
if (!$pTemporaryStorageDir || !is_dir($pTemporaryStorageDir)) {
72+
$pTemporaryStorageDir = sys_get_temp_dir();
73+
}
74+
// Create temporary filename
75+
$this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
76+
77+
// Open storage
78+
$this->openUri($this->tempFileName);
79+
}
80+
81+
if ($compatibility) {
82+
$this->setIndent(false);
83+
$this->setIndentString('');
84+
} else {
85+
$this->setIndent(true);
86+
$this->setIndentString(' ');
87+
}
88+
}
89+
90+
/**
91+
* Destructor.
92+
*/
93+
public function __destruct()
94+
{
95+
if ($this->hash !== spl_object_id($this)) {
96+
throw new WordException('Unserialize not permitted1');
97+
}
98+
// Unlink temporary files
99+
if (empty($this->tempFileName)) {
100+
return;
101+
}
102+
@unlink($this->tempFileName);
103+
}
104+
105+
/** @codeCoverageIgnore */
106+
public function __wakeup(): void
107+
{
108+
$this->tempFileName = '';
109+
110+
throw new WordException('Unserialize not permitted2');
111+
}
112+
113+
/** @param mixed[] $data */
114+
public function __unserialize(array $data): void
115+
{
116+
$this->tempFileName = '';
117+
118+
throw new WordException('Unserialize not permitted3');
119+
}
120+
121+
/**
122+
* Get written data.
123+
*
124+
* @return string
125+
*/
126+
public function getData()
127+
{
128+
if ($this->tempFileName == '') {
129+
return $this->outputMemory(true);
130+
}
131+
132+
$this->flush();
133+
134+
return (string) file_get_contents($this->tempFileName);
135+
}
136+
137+
/**
138+
* Write simple element and attribute(s) block.
139+
*
140+
* There are two options:
141+
* 1. If the `$attributes` is an array, then it's an associative array of attributes
142+
* 2. If not, then it's a simple attribute-value pair
143+
*
144+
* @param string $element
145+
* @param array|string $attributes
146+
* @param string $value
147+
*/
148+
public function writeElementBlock($element, $attributes, $value = null): void
149+
{
150+
$this->startElement($element);
151+
if (!is_array($attributes)) {
152+
$attributes = [$attributes => $value];
153+
}
154+
foreach ($attributes as $attribute => $value) {
155+
$this->writeAttribute($attribute, $value);
156+
}
157+
$this->endElement();
158+
}
159+
160+
/**
161+
* Write element if ...
162+
*
163+
* @param bool $condition
164+
* @param string $element
165+
* @param string $attribute
166+
* @param mixed $value
167+
*/
168+
public function writeElementIf($condition, $element, $attribute = null, $value = null): void
169+
{
170+
if ($condition == true) {
171+
if (null === $attribute) {
172+
$this->writeElement($element, $value);
173+
} else {
174+
$this->startElement($element);
175+
$this->writeAttribute($attribute, $value);
176+
$this->endElement();
177+
}
178+
}
179+
}
180+
181+
/**
182+
* Write attribute if ...
183+
*
184+
* @param bool $condition
185+
* @param string $attribute
186+
* @param mixed $value
187+
*/
188+
public function writeAttributeIf($condition, $attribute, $value): void
189+
{
190+
if ($condition == true) {
191+
$this->writeAttribute($attribute, $value);
192+
}
193+
}
194+
195+
/**
196+
* @param string $name
197+
* @param mixed $value
198+
*/
199+
public function writeAttribute($name, $value): bool
200+
{
201+
if (is_float($value)) {
202+
$value = (string) $value;
203+
}
204+
205+
return parent::writeAttribute($name, $value ?? '');
206+
}
7207
}

0 commit comments

Comments
 (0)