Skip to content

Commit

Permalink
MDL-75951 core: Update box/spout to address PHP 8.1 deprecation
Browse files Browse the repository at this point in the history
This change is a direct pull from an upstream fix:
openspout/openspout@64a09a7

This addresses the deprecation of auto_detect_line_endings in PHP 8.1.
  • Loading branch information
andrewnicols committed Dec 2, 2022
1 parent 1d863c3 commit fad9666
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/spout/readme_moodle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Description of Spout library import
* Only include the src/Spout directory.
* Update lib/thirdpartylibs.xml with the latest version.

2022/11/25
----------
Imported PHP 8.1 patch from OpenSpout/OpenSpout 4.8.1
https://github.com/openspout/openspout/commit/64a09a748d04992d63b38712599a9d8742bd77f7

2022/10/27
----------
Changes:
Expand Down Expand Up @@ -36,4 +41,4 @@ by Ankit Agarwal <[email protected]>
2016/09/20
----------
Updated to v2.6.0 (MDL-56012)
by Adrian Greeve <[email protected]>
by Adrian Greeve <[email protected]>
32 changes: 29 additions & 3 deletions lib/spout/src/Spout/Reader/CSV/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Box\Spout\Reader\CSV;

use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Reader\Common\Creator\InternalEntityFactoryInterface;
use Box\Spout\Reader\Common\Entity\Options;
use Box\Spout\Reader\CSV\Creator\InternalEntityFactory;
use Box\Spout\Reader\ReaderAbstract;
Expand All @@ -22,6 +25,23 @@ class Reader extends ReaderAbstract
/** @var string Original value for the "auto_detect_line_endings" INI value */
protected $originalAutoDetectLineEndings;

/** @var bool Whether the code is running with PHP >= 8.1 */
private $isRunningAtLeastPhp81;

/**
* @param OptionsManagerInterface $optionsManager
* @param GlobalFunctionsHelper $globalFunctionsHelper
* @param InternalEntityFactoryInterface $entityFactory
*/
public function __construct(
OptionsManagerInterface $optionsManager,
GlobalFunctionsHelper $globalFunctionsHelper,
InternalEntityFactoryInterface $entityFactory
) {
parent::__construct($optionsManager, $globalFunctionsHelper, $entityFactory);
$this->isRunningAtLeastPhp81 = \version_compare(PHP_VERSION, '8.1.0') >= 0;
}

/**
* Sets the field delimiter for the CSV.
* Needs to be called before opening the reader.
Expand Down Expand Up @@ -84,8 +104,11 @@ protected function doesSupportStreamWrapper()
*/
protected function openReader($filePath)
{
$this->originalAutoDetectLineEndings = \ini_get('auto_detect_line_endings');
\ini_set('auto_detect_line_endings', '1');
// "auto_detect_line_endings" is deprecated in PHP 8.1
if (!$this->isRunningAtLeastPhp81) {
$this->originalAutoDetectLineEndings = \ini_get('auto_detect_line_endings');
\ini_set('auto_detect_line_endings', '1');
}

$this->filePointer = $this->globalFunctionsHelper->fopen($filePath, 'r');
if (!$this->filePointer) {
Expand Down Expand Up @@ -123,6 +146,9 @@ protected function closeReader()
$this->globalFunctionsHelper->fclose($this->filePointer);
}

\ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings);
// "auto_detect_line_endings" is deprecated in PHP 8.1
if (!$this->isRunningAtLeastPhp81) {
\ini_set('auto_detect_line_endings', $this->originalAutoDetectLineEndings);
}
}
}

0 comments on commit fad9666

Please sign in to comment.