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\Writer;
   4  
   5  abstract class BaseWriter implements IWriter
   6  {
   7      /**
   8       * Write charts that are defined in the workbook?
   9       * Identifies whether the Writer should write definitions for any charts that exist in the PhpSpreadsheet object.
  10       *
  11       * @var bool
  12       */
  13      protected $includeCharts = false;
  14  
  15      /**
  16       * Pre-calculate formulas
  17       * Forces PhpSpreadsheet to recalculate all formulae in a workbook when saving, so that the pre-calculated values are
  18       * immediately available to MS Excel or other office spreadsheet viewer when opening the file.
  19       *
  20       * @var bool
  21       */
  22      protected $preCalculateFormulas = true;
  23  
  24      /**
  25       * Use disk caching where possible?
  26       *
  27       * @var bool
  28       */
  29      private $useDiskCaching = false;
  30  
  31      /**
  32       * Disk caching directory.
  33       *
  34       * @var string
  35       */
  36      private $diskCachingDirectory = './';
  37  
  38      /**
  39       * @var resource
  40       */
  41      protected $fileHandle;
  42  
  43      /**
  44       * @var bool
  45       */
  46      private $shouldCloseFile;
  47  
  48      public function getIncludeCharts()
  49      {
  50          return $this->includeCharts;
  51      }
  52  
  53      public function setIncludeCharts($includeCharts)
  54      {
  55          $this->includeCharts = (bool) $includeCharts;
  56  
  57          return $this;
  58      }
  59  
  60      public function getPreCalculateFormulas()
  61      {
  62          return $this->preCalculateFormulas;
  63      }
  64  
  65      public function setPreCalculateFormulas($precalculateFormulas)
  66      {
  67          $this->preCalculateFormulas = (bool) $precalculateFormulas;
  68  
  69          return $this;
  70      }
  71  
  72      public function getUseDiskCaching()
  73      {
  74          return $this->useDiskCaching;
  75      }
  76  
  77      public function setUseDiskCaching($useDiskCache, $cacheDirectory = null)
  78      {
  79          $this->useDiskCaching = $useDiskCache;
  80  
  81          if ($cacheDirectory !== null) {
  82              if (is_dir($cacheDirectory)) {
  83                  $this->diskCachingDirectory = $cacheDirectory;
  84              } else {
  85                  throw new Exception("Directory does not exist: $cacheDirectory");
  86              }
  87          }
  88  
  89          return $this;
  90      }
  91  
  92      public function getDiskCachingDirectory()
  93      {
  94          return $this->diskCachingDirectory;
  95      }
  96  
  97      protected function processFlags(int $flags): void
  98      {
  99          if (((bool) ($flags & self::SAVE_WITH_CHARTS)) === true) {
 100              $this->setIncludeCharts(true);
 101          }
 102      }
 103  
 104      /**
 105       * Open file handle.
 106       *
 107       * @param resource|string $filename
 108       */
 109      public function openFileHandle($filename): void
 110      {
 111          if (is_resource($filename)) {
 112              $this->fileHandle = $filename;
 113              $this->shouldCloseFile = false;
 114  
 115              return;
 116          }
 117  
 118          $mode = 'wb';
 119          $scheme = parse_url($filename, PHP_URL_SCHEME);
 120          if ($scheme === 's3') {
 121              // @codeCoverageIgnoreStart
 122              $mode = 'w';
 123              // @codeCoverageIgnoreEnd
 124          }
 125          $fileHandle = $filename ? fopen($filename, $mode) : false;
 126          if ($fileHandle === false) {
 127              throw new Exception('Could not open file "' . $filename . '" for writing.');
 128          }
 129  
 130          $this->fileHandle = $fileHandle;
 131          $this->shouldCloseFile = true;
 132      }
 133  
 134      /**
 135       * Close file handle only if we opened it ourselves.
 136       */
 137      protected function maybeCloseFileHandle(): void
 138      {
 139          if ($this->shouldCloseFile) {
 140              if (!fclose($this->fileHandle)) {
 141                  throw new Exception('Could not close file after writing.');
 142              }
 143          }
 144      }
 145  }