Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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 $temporaryStorage Temporary storage location
  24       * @param string $temporaryStorageFolder Temporary storage folder
  25       */
  26      public function __construct($temporaryStorage = self::STORAGE_MEMORY, $temporaryStorageFolder = null)
  27      {
  28          // Open temporary storage
  29          if ($temporaryStorage == self::STORAGE_MEMORY) {
  30              $this->openMemory();
  31          } else {
  32              // Create temporary filename
  33              if ($temporaryStorageFolder === null) {
  34                  $temporaryStorageFolder = File::sysGetTempDir();
  35              }
  36              $this->tempFileName = @tempnam($temporaryStorageFolder, '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          // There is nothing reasonable to do if unlink fails.
  58          if ($this->tempFileName != '') {
  59              /** @scrutinizer ignore-unhandled */
  60              @unlink($this->tempFileName);
  61          }
  62      }
  63  
  64      /**
  65       * Get written data.
  66       *
  67       * @return string
  68       */
  69      public function getData()
  70      {
  71          if ($this->tempFileName == '') {
  72              return $this->outputMemory(true);
  73          }
  74          $this->flush();
  75  
  76          return file_get_contents($this->tempFileName);
  77      }
  78  
  79      /**
  80       * Wrapper method for writeRaw.
  81       *
  82       * @param null|string|string[] $rawTextData
  83       *
  84       * @return bool
  85       */
  86      public function writeRawData($rawTextData)
  87      {
  88          if (is_array($rawTextData)) {
  89              $rawTextData = implode("\n", $rawTextData);
  90          }
  91  
  92          return $this->writeRaw(htmlspecialchars($rawTextData ?? ''));
  93      }
  94  }