XMLWriter.php 2.1 KB

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