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.
   1  <?php
   2  
   3  namespace Box\Spout\Reader\XLSX;
   4  
   5  use Box\Spout\Common\Exception\IOException;
   6  use Box\Spout\Common\Helper\GlobalFunctionsHelper;
   7  use Box\Spout\Common\Manager\OptionsManagerInterface;
   8  use Box\Spout\Reader\Common\Creator\InternalEntityFactoryInterface;
   9  use Box\Spout\Reader\Common\Entity\Options;
  10  use Box\Spout\Reader\ReaderAbstract;
  11  use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory;
  12  use Box\Spout\Reader\XLSX\Creator\ManagerFactory;
  13  
  14  /**
  15   * Class Reader
  16   * This class provides support to read data from a XLSX file
  17   */
  18  class Reader extends ReaderAbstract
  19  {
  20      /** @var ManagerFactory */
  21      protected $managerFactory;
  22  
  23      /** @var \ZipArchive */
  24      protected $zip;
  25  
  26      /** @var \Box\Spout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings */
  27      protected $sharedStringsManager;
  28  
  29      /** @var SheetIterator To iterator over the XLSX sheets */
  30      protected $sheetIterator;
  31  
  32      /**
  33       * @param OptionsManagerInterface $optionsManager
  34       * @param GlobalFunctionsHelper $globalFunctionsHelper
  35       * @param InternalEntityFactoryInterface $entityFactory
  36       * @param ManagerFactory $managerFactory
  37       */
  38      public function __construct(
  39          OptionsManagerInterface $optionsManager,
  40          GlobalFunctionsHelper $globalFunctionsHelper,
  41          InternalEntityFactoryInterface $entityFactory,
  42          ManagerFactory $managerFactory
  43      ) {
  44          parent::__construct($optionsManager, $globalFunctionsHelper, $entityFactory);
  45          $this->managerFactory = $managerFactory;
  46      }
  47  
  48      /**
  49       * @param string $tempFolder Temporary folder where the temporary files will be created
  50       * @return Reader
  51       */
  52      public function setTempFolder($tempFolder)
  53      {
  54          $this->optionsManager->setOption(Options::TEMP_FOLDER, $tempFolder);
  55  
  56          return $this;
  57      }
  58  
  59      /**
  60       * Returns whether stream wrappers are supported
  61       *
  62       * @return bool
  63       */
  64      protected function doesSupportStreamWrapper()
  65      {
  66          return false;
  67      }
  68  
  69      /**
  70       * Opens the file at the given file path to make it ready to be read.
  71       * It also parses the sharedStrings.xml file to get all the shared strings available in memory
  72       * and fetches all the available sheets.
  73       *
  74       * @param  string $filePath Path of the file to be read
  75       * @throws \Box\Spout\Common\Exception\IOException If the file at the given path or its content cannot be read
  76       * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
  77       * @return void
  78       */
  79      protected function openReader($filePath)
  80      {
  81          /** @var InternalEntityFactory $entityFactory */
  82          $entityFactory = $this->entityFactory;
  83  
  84          $this->zip = $entityFactory->createZipArchive();
  85  
  86          if ($this->zip->open($filePath) === true) {
  87              $tempFolder = $this->optionsManager->getOption(Options::TEMP_FOLDER);
  88              $this->sharedStringsManager = $this->managerFactory->createSharedStringsManager($filePath, $tempFolder, $entityFactory);
  89  
  90              if ($this->sharedStringsManager->hasSharedStrings()) {
  91                  // Extracts all the strings from the sheets for easy access in the future
  92                  $this->sharedStringsManager->extractSharedStrings();
  93              }
  94  
  95              $this->sheetIterator = $entityFactory->createSheetIterator(
  96                  $filePath,
  97                  $this->optionsManager,
  98                  $this->sharedStringsManager
  99              );
 100          } else {
 101              throw new IOException("Could not open $filePath for reading.");
 102          }
 103      }
 104  
 105      /**
 106       * Returns an iterator to iterate over sheets.
 107       *
 108       * @return SheetIterator To iterate over sheets
 109       */
 110      protected function getConcreteSheetIterator()
 111      {
 112          return $this->sheetIterator;
 113      }
 114  
 115      /**
 116       * Closes the reader. To be used after reading the file.
 117       *
 118       * @return void
 119       */
 120      protected function closeReader()
 121      {
 122          if ($this->zip) {
 123              $this->zip->close();
 124          }
 125  
 126          if ($this->sharedStringsManager) {
 127              $this->sharedStringsManager->cleanup();
 128          }
 129      }
 130  }