1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared; |
4
|
|
|
|
5
|
57 |
|
if (!defined('DATE_W3C')) { |
6
|
|
|
define('DATE_W3C', 'Y-m-d\TH:i:sP'); |
7
|
|
|
} |
8
|
|
|
|
9
|
57 |
|
if (!defined('DEBUGMODE_ENABLED')) { |
10
|
57 |
|
define('DEBUGMODE_ENABLED', false); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet. |
15
|
|
|
* |
16
|
|
|
* This library is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU Lesser General Public |
18
|
|
|
* License as published by the Free Software Foundation; either |
19
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This library is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24
|
|
|
* Lesser General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Lesser General Public |
27
|
|
|
* License along with this library; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
29
|
|
|
* |
30
|
|
|
* @category PhpSpreadsheet |
31
|
|
|
* |
32
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
33
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
34
|
|
|
*/ |
35
|
|
|
class XMLWriter extends \XMLWriter |
36
|
|
|
{ |
37
|
|
|
/** Temporary storage method */ |
38
|
|
|
const STORAGE_MEMORY = 1; |
39
|
|
|
const STORAGE_DISK = 2; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Temporary filename. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $tempFileName = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new XMLWriter instance. |
50
|
|
|
* |
51
|
|
|
* @param int $pTemporaryStorage Temporary storage location |
52
|
|
|
* @param string $pTemporaryStorageFolder Temporary storage folder |
53
|
|
|
*/ |
54
|
56 |
|
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = null) |
55
|
|
|
{ |
56
|
|
|
// Open temporary storage |
57
|
56 |
|
if ($pTemporaryStorage == self::STORAGE_MEMORY) { |
58
|
56 |
|
$this->openMemory(); |
59
|
|
|
} else { |
60
|
|
|
// Create temporary filename |
61
|
|
|
if ($pTemporaryStorageFolder === null) { |
62
|
|
|
$pTemporaryStorageFolder = File::sysGetTempDir(); |
63
|
|
|
} |
64
|
|
|
$this->tempFileName = @tempnam($pTemporaryStorageFolder, 'xml'); |
65
|
|
|
|
66
|
|
|
// Open storage |
67
|
|
|
if ($this->openUri($this->tempFileName) === false) { |
68
|
|
|
// Fallback to memory... |
69
|
|
|
$this->openMemory(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Set default values |
74
|
56 |
|
if (DEBUGMODE_ENABLED) { |
75
|
|
|
$this->setIndent(true); |
76
|
|
|
} |
77
|
56 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Destructor. |
81
|
|
|
*/ |
82
|
56 |
|
public function __destruct() |
83
|
|
|
{ |
84
|
|
|
// Unlink temporary files |
85
|
56 |
|
if ($this->tempFileName != '') { |
86
|
|
|
@unlink($this->tempFileName); |
|
|
|
|
87
|
|
|
} |
88
|
56 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get written data. |
92
|
|
|
* |
93
|
|
|
* @return $data |
|
|
|
|
94
|
|
|
*/ |
95
|
56 |
|
public function getData() |
96
|
|
|
{ |
97
|
56 |
|
if ($this->tempFileName == '') { |
98
|
56 |
|
return $this->outputMemory(true); |
99
|
|
|
} |
100
|
|
|
$this->flush(); |
101
|
|
|
|
102
|
|
|
return file_get_contents($this->tempFileName); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Wrapper method for writeRaw. |
107
|
|
|
* |
108
|
|
|
* @param string|string[] $text |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
56 |
|
public function writeRawData($text) |
113
|
|
|
{ |
114
|
56 |
|
if (is_array($text)) { |
115
|
|
|
$text = implode("\n", $text); |
116
|
|
|
} |
117
|
|
|
|
118
|
56 |
|
return $this->writeRaw(htmlspecialchars($text)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: