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 402 and 403]

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Reader\XLSX;
   6  
   7  use OpenSpout\Common\Exception\IOException;
   8  use OpenSpout\Common\Helper\Escaper\XLSX;
   9  use OpenSpout\Reader\AbstractReader;
  10  use OpenSpout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
  11  use OpenSpout\Reader\XLSX\Manager\SharedStringsCaching\MemoryLimit;
  12  use OpenSpout\Reader\XLSX\Manager\SharedStringsManager;
  13  use OpenSpout\Reader\XLSX\Manager\SheetManager;
  14  use OpenSpout\Reader\XLSX\Manager\WorkbookRelationshipsManager;
  15  use ZipArchive;
  16  
  17  /**
  18   * @extends AbstractReader<SheetIterator>
  19   */
  20  final class Reader extends AbstractReader
  21  {
  22      private ZipArchive $zip;
  23  
  24      /** @var SharedStringsManager Manages shared strings */
  25      private SharedStringsManager $sharedStringsManager;
  26  
  27      /** @var SheetIterator To iterator over the XLSX sheets */
  28      private SheetIterator $sheetIterator;
  29  
  30      private Options $options;
  31      private CachingStrategyFactory $cachingStrategyFactory;
  32  
  33      public function __construct(
  34          ?Options $options = null,
  35          ?CachingStrategyFactory $cachingStrategyFactory = null
  36      ) {
  37          $this->options = $options ?? new Options();
  38  
  39          if (null === $cachingStrategyFactory) {
  40              $memoryLimit = \ini_get('memory_limit');
  41              \assert(false !== $memoryLimit);
  42  
  43              $cachingStrategyFactory = new CachingStrategyFactory(new MemoryLimit($memoryLimit));
  44          }
  45          $this->cachingStrategyFactory = $cachingStrategyFactory;
  46      }
  47  
  48      public function getSheetIterator(): SheetIterator
  49      {
  50          $this->ensureStreamOpened();
  51  
  52          return $this->sheetIterator;
  53      }
  54  
  55      /**
  56       * Returns whether stream wrappers are supported.
  57       */
  58      protected function doesSupportStreamWrapper(): bool
  59      {
  60          return false;
  61      }
  62  
  63      /**
  64       * Opens the file at the given file path to make it ready to be read.
  65       * It also parses the sharedStrings.xml file to get all the shared strings available in memory
  66       * and fetches all the available sheets.
  67       *
  68       * @param string $filePath Path of the file to be read
  69       *
  70       * @throws \OpenSpout\Common\Exception\IOException            If the file at the given path or its content cannot be read
  71       * @throws \OpenSpout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
  72       */
  73      protected function openReader(string $filePath): void
  74      {
  75          $this->zip = new ZipArchive();
  76  
  77          if (true !== $this->zip->open($filePath)) {
  78              throw new IOException("Could not open {$filePath} for reading.");
  79          }
  80  
  81          $this->sharedStringsManager = new SharedStringsManager(
  82              $filePath,
  83              $this->options,
  84              new WorkbookRelationshipsManager($filePath),
  85              $this->cachingStrategyFactory
  86          );
  87  
  88          if ($this->sharedStringsManager->hasSharedStrings()) {
  89              // Extracts all the strings from the sheets for easy access in the future
  90              $this->sharedStringsManager->extractSharedStrings();
  91          }
  92  
  93          $this->sheetIterator = new SheetIterator(
  94              new SheetManager(
  95                  $filePath,
  96                  $this->options,
  97                  $this->sharedStringsManager,
  98                  new XLSX()
  99              )
 100          );
 101      }
 102  
 103      /**
 104       * Closes the reader. To be used after reading the file.
 105       */
 106      protected function closeReader(): void
 107      {
 108          $this->zip->close();
 109          $this->sharedStringsManager->cleanup();
 110      }
 111  }