Search moodle.org's
Developer Documentation

See Release Notes

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