Skip to content

Commit

Permalink
change IReader functions to allow passing of resources
Browse files Browse the repository at this point in the history
  • Loading branch information
apreiml committed Oct 5, 2023
1 parent db35a41 commit 19ec1b5
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 112 deletions.
34 changes: 24 additions & 10 deletions src/PhpSpreadsheet/Reader/BaseReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,40 +160,50 @@ protected function processFlags(int $flags): void
}
}

protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
/**
* @param string|resource $file
*/
protected function loadSpreadsheetFromFile($file): Spreadsheet
{
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method');
}

/**
* Loads Spreadsheet from file.
*
* @param string|resource $file
* @param int $flags the optional second parameter flags may be used to identify specific elements
* that should be loaded, but which won't be loaded by default, using these values:
* IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file
*/
public function load(string $filename, int $flags = 0): Spreadsheet
public function load($file, int $flags = 0): Spreadsheet
{
$this->processFlags($flags);

try {
return $this->loadSpreadsheetFromFile($filename);
return $this->loadSpreadsheetFromFile($file);
} catch (ReaderException $e) {
throw $e;
}
}

/**
* Open file for reading.
*
* @param string|resource $file
*/
protected function openFile(string $filename): void
protected function openFile($file): void
{
$fileHandle = false;
if ($filename) {
File::assertFile($filename);
if (is_string($file)) {
$filename = $file;
File::assertFile($file);

// Open file
$fileHandle = fopen($filename, 'rb');
$fileHandle = fopen($file, 'rb');
} elseif (is_resource($file)) {
$filename = 'stream';
$fileHandle = $file;
}
if ($fileHandle === false) {
throw new ReaderException('Could not open file ' . $filename . ' for reading.');
Expand All @@ -204,8 +214,10 @@ protected function openFile(string $filename): void

/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
*
* @param string|resource $file
*/
public function listWorksheetInfo(string $filename): array
public function listWorksheetInfo($file): array
{
throw new PhpSpreadsheetException('Reader classes must implement their own listWorksheetInfo() method');
}
Expand All @@ -215,11 +227,13 @@ public function listWorksheetInfo(string $filename): array
* possibly without parsing the whole file to a Spreadsheet object.
* Readers will often have a more efficient method with which
* they can override this method.
*
* @param string|resource $file
*/
public function listWorksheetNames(string $filename): array
public function listWorksheetNames($file): array
{
$returnArray = [];
$info = $this->listWorksheetInfo($filename);
$info = $this->listWorksheetInfo($file);
foreach ($info as $infoArray) {
if (isset($infoArray['worksheetName'])) {
$returnArray[] = $infoArray['worksheetName'];
Expand Down
29 changes: 20 additions & 9 deletions src/PhpSpreadsheet/Reader/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,13 @@ protected function inferSeparator(): void

/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
*
* @param string|resource $file
*/
public function listWorksheetInfo(string $filename): array
public function listWorksheetInfo($file): array
{
// Open file
$this->openFileOrMemory($filename);
$this->openFileOrMemory($file);
$fileHandle = $this->fileHandle;

// Skip BOM, if any
Expand Down Expand Up @@ -254,14 +256,16 @@ public function listWorksheetInfo(string $filename): array

/**
* Loads Spreadsheet from file.
*
* @param string|resource $file
*/
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
protected function loadSpreadsheetFromFile($file): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();

// Load into this instance
return $this->loadIntoExisting($filename, $spreadsheet);
return $this->loadIntoExisting($file, $spreadsheet);
}

/**
Expand All @@ -276,7 +280,10 @@ public function loadSpreadsheetFromString(string $contents): Spreadsheet
return $this->loadStringOrFile('data://text/plain,' . urlencode($contents), $spreadsheet, true);
}

private function openFileOrMemory(string $filename): void
/**
* @param string|resource $filename
*/
private function openFileOrMemory($filename): void
{
// Open file
$fhandle = $this->canRead($filename);
Expand Down Expand Up @@ -345,6 +352,8 @@ private function openDataUri(string $filename): void

/**
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
*
* @param string|resource $file
*/
public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet
{
Expand All @@ -353,6 +362,8 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp

/**
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
*
* @param string|resource $file
*/
private function loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bool $dataUri): Spreadsheet
{
Expand Down Expand Up @@ -539,25 +550,25 @@ public function getEscapeCharacter(): string
/**
* Can the current IReader read the file?
*/
public function canRead(string $filename): bool
public function canRead($file): bool
{
// Check if file exists
try {
$this->openFile($filename);
$this->openFile($file);
} catch (ReaderException) {
return false;
}

fclose($this->fileHandle);

// Trust file extension if any
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($extension, ['csv', 'tsv'])) {
return true;
}

// Attempt to guess mimetype
$type = mime_content_type($filename);
$type = mime_content_type($file);
$supportedTypes = [
'application/csv',
'text/csv',
Expand Down
54 changes: 39 additions & 15 deletions src/PhpSpreadsheet/Reader/Gnumeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ public function __construct()

/**
* Can the current IReader read the file?
*
* @param string|resource $file
*/
public function canRead(string $filename): bool
public function canRead($file): bool
{
if (is_resource($file)) {
return false; // TODO
}

$data = null;
if (File::testFileNoThrow($filename)) {
$data = $this->gzfileGetContents($filename);
if (File::testFileNoThrow($file)) {
$data = $this->gzfileGetContents($file);
if (!str_contains($data, self::NAMESPACE_GNM)) {
$data = '';
}
Expand All @@ -99,16 +105,22 @@ private static function matchXml(XMLReader $xml, string $expectedLocalName): boo

/**
* Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
*
* @param string|resource $file
*/
public function listWorksheetNames(string $filename): array
public function listWorksheetNames($file): array
{
File::assertFile($filename);
if (!$this->canRead($filename)) {
throw new Exception($filename . ' is an invalid Gnumeric file.');
if (is_resource($file)) {
throw new Exception('file as stream not supported');
}

File::assertFile($file);
if (!$this->canRead($file)) {
throw new Exception($file . ' is an invalid Gnumeric file.');
}

$xml = new XMLReader();
$contents = $this->gzfileGetContents($filename);
$contents = $this->gzfileGetContents($file);
$xml->xml($contents, null, Settings::getLibXmlLoaderOptions());
$xml->setParserProperty(2, true);

Expand All @@ -128,16 +140,22 @@ public function listWorksheetNames(string $filename): array

/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
*
* @param string|resource $file
*/
public function listWorksheetInfo(string $filename): array
public function listWorksheetInfo($file): array
{
File::assertFile($filename);
if (!$this->canRead($filename)) {
throw new Exception($filename . ' is an invalid Gnumeric file.');
if (is_resource($file)) {
throw new Exception('file as stream not supported');
}

File::assertFile($file);
if (!$this->canRead($file)) {
throw new Exception($file . ' is an invalid Gnumeric file.');
}

$xml = new XMLReader();
$contents = $this->gzfileGetContents($filename);
$contents = $this->gzfileGetContents($file);
$xml->xml($contents, null, Settings::getLibXmlLoaderOptions());
$xml->setParserProperty(2, true);

Expand Down Expand Up @@ -229,15 +247,21 @@ private static function testSimpleXml(mixed $value): SimpleXMLElement

/**
* Loads Spreadsheet from file.
*
* @param string|resource $file
*/
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
protected function loadSpreadsheetFromFile($file): Spreadsheet
{
if (is_resource($file)) {
throw new Exception('file as stream not supported');
}

// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
$spreadsheet->removeSheetByIndex(0);

// Load into this instance
return $this->loadIntoExisting($filename, $spreadsheet);
return $this->loadIntoExisting($file, $spreadsheet);
}

/**
Expand Down
38 changes: 27 additions & 11 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,14 @@ public function __construct()

/**
* Validate that the current file is an HTML file.
*
* @param string|resource $file
*/
public function canRead(string $filename): bool
public function canRead($file): bool
{
// Check if file exists
try {
$this->openFile($filename);
$this->openFile($file);
} catch (Exception) {
return false;
}
Expand Down Expand Up @@ -202,14 +204,16 @@ private static function containsTags(string $data): bool

/**
* Loads Spreadsheet from file.
*
* @param string|resource $file
*/
public function loadSpreadsheetFromFile(string $filename): Spreadsheet
public function loadSpreadsheetFromFile($file): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();

// Load into this instance
return $this->loadIntoExisting($filename, $spreadsheet);
return $this->loadIntoExisting($file, $spreadsheet);
}

/**
Expand Down Expand Up @@ -661,20 +665,26 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, int &$r

/**
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
*
* @param string|resource $file
*/
public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet
public function loadIntoExisting($file, Spreadsheet $spreadsheet): Spreadsheet
{
if (is_resource($file)) {
throw new Exception('file as stream not supported');
}

// Validate
if (!$this->canRead($filename)) {
throw new Exception($filename . ' is an Invalid HTML file.');
if (!$this->canRead($file)) {
throw new Exception($file . ' is an Invalid HTML file.');
}

// Create a new DOM object
$dom = new DOMDocument();

// Reload the HTML file into the DOM object
try {
$convert = $this->getSecurityScannerOrThrow()->scanFile($filename);
$convert = $this->getSecurityScannerOrThrow()->scanFile($file);
$lowend = "\u{80}";
$highend = "\u{10ffff}";
$regexp = "/[$lowend-$highend]/u";
Expand All @@ -686,7 +696,7 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp
$loaded = false;
}
if ($loaded === false) {
throw new Exception('Failed to load ' . $filename . ' as a DOM Document', 0, $e ?? null);
throw new Exception('Failed to load ' . $file . ' as a DOM Document', 0, $e ?? null);
}
self::loadProperties($dom, $spreadsheet);

Expand Down Expand Up @@ -1152,12 +1162,18 @@ private function setBorderStyle(Style $cellStyle, string $styleValue, string $ty

/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
*
* @param string|resource $file
*/
public function listWorksheetInfo(string $filename): array
public function listWorksheetInfo($file): array
{
if (is_resource($file)) {
throw new Exception('file as stream not supported');
}

$info = [];
$spreadsheet = new Spreadsheet();
$this->loadIntoExisting($filename, $spreadsheet);
$this->loadIntoExisting($file, $spreadsheet);
foreach ($spreadsheet->getAllSheets() as $sheet) {
$newEntry = ['worksheetName' => $sheet->getTitle()];
$newEntry['lastColumnLetter'] = $sheet->getHighestDataColumn();
Expand Down
Loading

0 comments on commit 19ec1b5

Please sign in to comment.