| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Shared; |
| 4 |
|
| 5 |
class XMLWriter extends \XMLWriter |
| 6 |
{ |
| 7 |
public static $debugEnabled = false; |
| 8 |
|
| 9 |
/** Temporary storage method */ |
| 10 |
const STORAGE_MEMORY = 1; |
| 11 |
const STORAGE_DISK = 2; |
| 12 |
|
| 13 |
/** |
| 14 |
* Temporary filename. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
private $tempFileName = ''; |
| 19 |
|
| 20 |
/** |
| 21 |
* Create a new XMLWriter instance. |
| 22 |
* |
| 23 |
* @param int $pTemporaryStorage Temporary storage location |
| 24 |
* @param string $pTemporaryStorageFolder Temporary storage folder |
| 25 |
*/ |
| 26 |
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = null) |
| 27 |
{ |
| 28 |
// Open temporary storage |
| 29 |
if ($pTemporaryStorage == self::STORAGE_MEMORY) { |
| 30 |
$this->openMemory(); |
| 31 |
} else { |
| 32 |
// Create temporary filename |
| 33 |
if ($pTemporaryStorageFolder === null) { |
| 34 |
$pTemporaryStorageFolder = File::sysGetTempDir(); |
| 35 |
} |
| 36 |
$this->tempFileName = @tempnam($pTemporaryStorageFolder, 'xml'); |
| 37 |
|
| 38 |
// Open storage |
| 39 |
if ($this->openUri($this->tempFileName) === false) { |
| 40 |
// Fallback to memory... |
| 41 |
$this->openMemory(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// Set default values |
| 46 |
if (self::$debugEnabled) { |
| 47 |
$this->setIndent(true); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Destructor. |
| 53 |
*/ |
| 54 |
public function __destruct() |
| 55 |
{ |
| 56 |
// Unlink temporary files |
| 57 |
if ($this->tempFileName != '') { |
| 58 |
@unlink($this->tempFileName); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get written data. |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function getData() |
| 68 |
{ |
| 69 |
if ($this->tempFileName == '') { |
| 70 |
return $this->outputMemory(true); |
| 71 |
} |
| 72 |
$this->flush(); |
| 73 |
|
| 74 |
return file_get_contents($this->tempFileName); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Wrapper method for writeRaw. |
| 79 |
* |
| 80 |
* @param string|string[] $text |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public function writeRawData($text) |
| 85 |
{ |
| 86 |
if (is_array($text)) { |
| 87 |
$text = implode("\n", $text); |
| 88 |
} |
| 89 |
|
| 90 |
return $this->writeRaw(htmlspecialchars($text)); |
| 91 |
} |
| 92 |
} |
| 93 |
|