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.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Reader\CSV;
   6  
   7  use OpenSpout\Common\Helper\EncodingHelper;
   8  use OpenSpout\Reader\AbstractReader;
   9  
  10  /**
  11   * @extends AbstractReader<SheetIterator>
  12   */
  13  final class Reader extends AbstractReader
  14  {
  15      /** @var resource Pointer to the file to be written */
  16      private $filePointer;
  17  
  18      /** @var SheetIterator To iterator over the CSV unique "sheet" */
  19      private SheetIterator $sheetIterator;
  20  
  21      /** @var string Original value for the "auto_detect_line_endings" INI value */
  22      private string $originalAutoDetectLineEndings;
  23  
  24      /** @var bool Whether the code is running with PHP >= 8.1 */
  25      private bool $isRunningAtLeastPhp81;
  26  
  27      private Options $options;
  28      private EncodingHelper $encodingHelper;
  29  
  30      public function __construct(
  31          ?Options $options = null,
  32          ?EncodingHelper $encodingHelper = null
  33      ) {
  34          $this->options = $options ?? new Options();
  35          $this->encodingHelper = $encodingHelper ?? EncodingHelper::factory();
  36          $this->isRunningAtLeastPhp81 = \PHP_VERSION_ID >= 80100;
  37      }
  38  
  39      public function getSheetIterator(): SheetIterator
  40      {
  41          $this->ensureStreamOpened();
  42  
  43          return $this->sheetIterator;
  44      }
  45  
  46      /**
  47       * Returns whether stream wrappers are supported.
  48       */
  49      protected function doesSupportStreamWrapper(): bool
  50      {
  51          return true;
  52      }
  53  
  54      /**
  55       * Opens the file at the given path to make it ready to be read.
  56       * If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
  57       *
  58       * @param string $filePath Path of the CSV file to be read
  59       *
  60       * @throws \OpenSpout\Common\Exception\IOException
  61       */
  62      protected function openReader(string $filePath): void
  63      {
  64          // "auto_detect_line_endings" is deprecated in PHP 8.1
  65          if (!$this->isRunningAtLeastPhp81) {
  66              // @codeCoverageIgnoreStart
  67              $originalAutoDetectLineEndings = \ini_get('auto_detect_line_endings');
  68              \assert(false !== $originalAutoDetectLineEndings);
  69              $this->originalAutoDetectLineEndings = $originalAutoDetectLineEndings;
  70              ini_set('auto_detect_line_endings', '1');
  71              // @codeCoverageIgnoreEnd
  72          }
  73  
  74          $resource = fopen($filePath, 'r');
  75          \assert(false !== $resource);
  76          $this->filePointer = $resource;
  77  
  78          $this->sheetIterator = new SheetIterator(
  79              new Sheet(
  80                  new RowIterator(
  81                      $this->filePointer,
  82                      $this->options,
  83                      $this->encodingHelper
  84                  )
  85              )
  86          );
  87      }
  88  
  89      /**
  90       * Closes the reader. To be used after reading the file.
  91       */
  92      protected function closeReader(): void
  93      {
  94          fclose($this->filePointer);
  95  
  96          // "auto_detect_line_endings" is deprecated in PHP 8.1
  97          if (!$this->isRunningAtLeastPhp81) {
  98              // @codeCoverageIgnoreStart
  99              ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings);
 100              // @codeCoverageIgnoreEnd
 101          }
 102      }
 103  }