Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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          if (((bool) ($flags & self::DISABLE_PRECALCULATE_FORMULAE)) === true) {
 103              $this->setPreCalculateFormulas(false);
 104          }
 105      }
 106  
 107      /**
 108       * Open file handle.
 109       *
 110       * @param resource|string $filename
 111       */
 112      public function openFileHandle($filename): void
 113      {
 114          if (is_resource($filename)) {
 115              $this->fileHandle = $filename;
 116              $this->shouldCloseFile = false;
 117  
 118              return;
 119          }
 120  
 121          $mode = 'wb';
 122          $scheme = parse_url($filename, PHP_URL_SCHEME);
 123          if ($scheme === 's3') {
 124              // @codeCoverageIgnoreStart
 125              $mode = 'w';
 126              // @codeCoverageIgnoreEnd
 127          }
 128          $fileHandle = $filename ? fopen($filename, $mode) : false;
 129          if ($fileHandle === false) {
 130              throw new Exception('Could not open file "' . $filename . '" for writing.');
 131          }
 132  
 133          $this->fileHandle = $fileHandle;
 134          $this->shouldCloseFile = true;
 135      }
 136  
 137      /**
 138       * Close file handle only if we opened it ourselves.
 139       */
 140      protected function maybeCloseFileHandle(): void
 141      {
 142          if ($this->shouldCloseFile) {
 143              if (!fclose($this->fileHandle)) {
 144                  throw new Exception('Could not close file after writing.');
 145              }
 146          }
 147      }
 148  }