Skip to content

Commit 4cca3ef

Browse files
committed
change IReader functions to allow passing of resources
1 parent db35a41 commit 4cca3ef

File tree

11 files changed

+110
-106
lines changed

11 files changed

+110
-106
lines changed

src/PhpSpreadsheet/Reader/BaseReader.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ protected function processFlags(int $flags): void
160160
}
161161
}
162162

163-
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
163+
protected function loadSpreadsheetFromFile($file): Spreadsheet
164164
{
165165
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method');
166166
}
@@ -172,12 +172,12 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
172172
* that should be loaded, but which won't be loaded by default, using these values:
173173
* IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file
174174
*/
175-
public function load(string $filename, int $flags = 0): Spreadsheet
175+
public function load($file, int $flags = 0): Spreadsheet
176176
{
177177
$this->processFlags($flags);
178178

179179
try {
180-
return $this->loadSpreadsheetFromFile($filename);
180+
return $this->loadSpreadsheetFromFile($file);
181181
} catch (ReaderException $e) {
182182
throw $e;
183183
}
@@ -186,14 +186,18 @@ public function load(string $filename, int $flags = 0): Spreadsheet
186186
/**
187187
* Open file for reading.
188188
*/
189-
protected function openFile(string $filename): void
189+
protected function openFile($file): void
190190
{
191191
$fileHandle = false;
192-
if ($filename) {
193-
File::assertFile($filename);
192+
if (is_string($file)) {
193+
$filename = $file;
194+
File::assertFile($file);
194195

195196
// Open file
196-
$fileHandle = fopen($filename, 'rb');
197+
$fileHandle = fopen($file, 'rb');
198+
} else {
199+
$filename = 'stream';
200+
$fileHandle = $file;
197201
}
198202
if ($fileHandle === false) {
199203
throw new ReaderException('Could not open file ' . $filename . ' for reading.');
@@ -205,7 +209,7 @@ protected function openFile(string $filename): void
205209
/**
206210
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
207211
*/
208-
public function listWorksheetInfo(string $filename): array
212+
public function listWorksheetInfo($file): array
209213
{
210214
throw new PhpSpreadsheetException('Reader classes must implement their own listWorksheetInfo() method');
211215
}
@@ -216,10 +220,10 @@ public function listWorksheetInfo(string $filename): array
216220
* Readers will often have a more efficient method with which
217221
* they can override this method.
218222
*/
219-
public function listWorksheetNames(string $filename): array
223+
public function listWorksheetNames($file): array
220224
{
221225
$returnArray = [];
222-
$info = $this->listWorksheetInfo($filename);
226+
$info = $this->listWorksheetInfo($file);
223227
foreach ($info as $infoArray) {
224228
if (isset($infoArray['worksheetName'])) {
225229
$returnArray[] = $infoArray['worksheetName'];

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ protected function inferSeparator(): void
217217
/**
218218
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
219219
*/
220-
public function listWorksheetInfo(string $filename): array
220+
public function listWorksheetInfo($file): array
221221
{
222222
// Open file
223-
$this->openFileOrMemory($filename);
223+
$this->openFileOrMemory($file);
224224
$fileHandle = $this->fileHandle;
225225

226226
// Skip BOM, if any
@@ -255,13 +255,13 @@ public function listWorksheetInfo(string $filename): array
255255
/**
256256
* Loads Spreadsheet from file.
257257
*/
258-
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
258+
protected function loadSpreadsheetFromFile($file): Spreadsheet
259259
{
260260
// Create new Spreadsheet
261261
$spreadsheet = new Spreadsheet();
262262

263263
// Load into this instance
264-
return $this->loadIntoExisting($filename, $spreadsheet);
264+
return $this->loadIntoExisting($file, $spreadsheet);
265265
}
266266

267267
/**
@@ -539,25 +539,25 @@ public function getEscapeCharacter(): string
539539
/**
540540
* Can the current IReader read the file?
541541
*/
542-
public function canRead(string $filename): bool
542+
public function canRead($file): bool
543543
{
544544
// Check if file exists
545545
try {
546-
$this->openFile($filename);
546+
$this->openFile($file);
547547
} catch (ReaderException) {
548548
return false;
549549
}
550550

551551
fclose($this->fileHandle);
552552

553553
// Trust file extension if any
554-
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
554+
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
555555
if (in_array($extension, ['csv', 'tsv'])) {
556556
return true;
557557
}
558558

559559
// Attempt to guess mimetype
560-
$type = mime_content_type($filename);
560+
$type = mime_content_type($file);
561561
$supportedTypes = [
562562
'application/csv',
563563
'text/csv',

src/PhpSpreadsheet/Reader/Gnumeric.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ public function __construct()
7777
/**
7878
* Can the current IReader read the file?
7979
*/
80-
public function canRead(string $filename): bool
80+
public function canRead($file): bool
8181
{
8282
$data = null;
83-
if (File::testFileNoThrow($filename)) {
84-
$data = $this->gzfileGetContents($filename);
83+
if (File::testFileNoThrow($file)) {
84+
$data = $this->gzfileGetContents($file);
8585
if (!str_contains($data, self::NAMESPACE_GNM)) {
8686
$data = '';
8787
}
@@ -100,15 +100,15 @@ private static function matchXml(XMLReader $xml, string $expectedLocalName): boo
100100
/**
101101
* Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
102102
*/
103-
public function listWorksheetNames(string $filename): array
103+
public function listWorksheetNames($file): array
104104
{
105-
File::assertFile($filename);
106-
if (!$this->canRead($filename)) {
107-
throw new Exception($filename . ' is an invalid Gnumeric file.');
105+
File::assertFile($file);
106+
if (!$this->canRead($file)) {
107+
throw new Exception($file . ' is an invalid Gnumeric file.');
108108
}
109109

110110
$xml = new XMLReader();
111-
$contents = $this->gzfileGetContents($filename);
111+
$contents = $this->gzfileGetContents($file);
112112
$xml->xml($contents, null, Settings::getLibXmlLoaderOptions());
113113
$xml->setParserProperty(2, true);
114114

@@ -129,15 +129,15 @@ public function listWorksheetNames(string $filename): array
129129
/**
130130
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
131131
*/
132-
public function listWorksheetInfo(string $filename): array
132+
public function listWorksheetInfo($file): array
133133
{
134-
File::assertFile($filename);
135-
if (!$this->canRead($filename)) {
136-
throw new Exception($filename . ' is an invalid Gnumeric file.');
134+
File::assertFile($file);
135+
if (!$this->canRead($file)) {
136+
throw new Exception($file . ' is an invalid Gnumeric file.');
137137
}
138138

139139
$xml = new XMLReader();
140-
$contents = $this->gzfileGetContents($filename);
140+
$contents = $this->gzfileGetContents($file);
141141
$xml->xml($contents, null, Settings::getLibXmlLoaderOptions());
142142
$xml->setParserProperty(2, true);
143143

@@ -230,14 +230,14 @@ private static function testSimpleXml(mixed $value): SimpleXMLElement
230230
/**
231231
* Loads Spreadsheet from file.
232232
*/
233-
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
233+
protected function loadSpreadsheetFromFile($file): Spreadsheet
234234
{
235235
// Create new Spreadsheet
236236
$spreadsheet = new Spreadsheet();
237237
$spreadsheet->removeSheetByIndex(0);
238238

239239
// Load into this instance
240-
return $this->loadIntoExisting($filename, $spreadsheet);
240+
return $this->loadIntoExisting($file, $spreadsheet);
241241
}
242242

243243
/**

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ public function __construct()
139139
/**
140140
* Validate that the current file is an HTML file.
141141
*/
142-
public function canRead(string $filename): bool
142+
public function canRead($file): bool
143143
{
144144
// Check if file exists
145145
try {
146-
$this->openFile($filename);
146+
$this->openFile($file);
147147
} catch (Exception) {
148148
return false;
149149
}
@@ -203,13 +203,13 @@ private static function containsTags(string $data): bool
203203
/**
204204
* Loads Spreadsheet from file.
205205
*/
206-
public function loadSpreadsheetFromFile(string $filename): Spreadsheet
206+
public function loadSpreadsheetFromFile($file): Spreadsheet
207207
{
208208
// Create new Spreadsheet
209209
$spreadsheet = new Spreadsheet();
210210

211211
// Load into this instance
212-
return $this->loadIntoExisting($filename, $spreadsheet);
212+
return $this->loadIntoExisting($file, $spreadsheet);
213213
}
214214

215215
/**
@@ -1153,11 +1153,11 @@ private function setBorderStyle(Style $cellStyle, string $styleValue, string $ty
11531153
/**
11541154
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
11551155
*/
1156-
public function listWorksheetInfo(string $filename): array
1156+
public function listWorksheetInfo($file): array
11571157
{
11581158
$info = [];
11591159
$spreadsheet = new Spreadsheet();
1160-
$this->loadIntoExisting($filename, $spreadsheet);
1160+
$this->loadIntoExisting($file, $spreadsheet);
11611161
foreach ($spreadsheet->getAllSheets() as $sheet) {
11621162
$newEntry = ['worksheetName' => $sheet->getTitle()];
11631163
$newEntry['lastColumnLetter'] = $sheet->getHighestDataColumn();

src/PhpSpreadsheet/Reader/IReader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct();
2121
/**
2222
* Can the current IReader read the file?
2323
*/
24-
public function canRead(string $filename): bool;
24+
public function canRead($file): bool;
2525

2626
/**
2727
* Read data only?
@@ -132,7 +132,7 @@ public function setReadFilter(IReadFilter $readFilter);
132132
/**
133133
* Loads PhpSpreadsheet from file.
134134
*
135-
* @param string $filename The name of the file to load
135+
* @param $filename The name of the file to load
136136
* @param int $flags Flags that can change the behaviour of the Writer:
137137
* self::LOAD_WITH_CHARTS Load any charts that are defined (if the Reader supports Charts)
138138
* self::READ_DATA_ONLY Read only data, not style or structure information, from the file
@@ -141,5 +141,5 @@ public function setReadFilter(IReadFilter $readFilter);
141141
*
142142
* @return Spreadsheet
143143
*/
144-
public function load(string $filename, int $flags = 0);
144+
public function load($file, int $flags = 0);
145145
}

src/PhpSpreadsheet/Reader/Ods.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public function __construct()
4242
/**
4343
* Can the current IReader read the file?
4444
*/
45-
public function canRead(string $filename): bool
45+
public function canRead($file): bool
4646
{
4747
$mimeType = 'UNKNOWN';
4848

4949
// Load file
5050

51-
if (File::testFileNoThrow($filename, '')) {
51+
if (File::testFileNoThrow($file, '')) {
5252
$zip = new ZipArchive();
53-
if ($zip->open($filename) === true) {
53+
if ($zip->open($file) === true) {
5454
// check if it is an OOXML archive
5555
$stat = $zip->statName('mimetype');
5656
if (!empty($stat) && ($stat['size'] <= 255)) {
@@ -89,15 +89,15 @@ public function canRead(string $filename): bool
8989
*
9090
* @return string[]
9191
*/
92-
public function listWorksheetNames(string $filename): array
92+
public function listWorksheetNames($file): array
9393
{
94-
File::assertFile($filename, self::INITIAL_FILE);
94+
File::assertFile($file, self::INITIAL_FILE);
9595

9696
$worksheetNames = [];
9797

9898
$xml = new XMLReader();
9999
$xml->xml(
100-
$this->getSecurityScannerOrThrow()->scanFile('zip://' . realpath($filename) . '#' . self::INITIAL_FILE),
100+
$this->getSecurityScannerOrThrow()->scanFile('zip://' . realpath($file) . '#' . self::INITIAL_FILE),
101101
null,
102102
Settings::getLibXmlLoaderOptions()
103103
);
@@ -136,15 +136,15 @@ public function listWorksheetNames(string $filename): array
136136
/**
137137
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
138138
*/
139-
public function listWorksheetInfo(string $filename): array
139+
public function listWorksheetInfo($file): array
140140
{
141-
File::assertFile($filename, self::INITIAL_FILE);
141+
File::assertFile($file, self::INITIAL_FILE);
142142

143143
$worksheetInfo = [];
144144

145145
$xml = new XMLReader();
146146
$xml->xml(
147-
$this->getSecurityScannerOrThrow()->scanFile('zip://' . realpath($filename) . '#' . self::INITIAL_FILE),
147+
$this->getSecurityScannerOrThrow()->scanFile('zip://' . realpath($file) . '#' . self::INITIAL_FILE),
148148
null,
149149
Settings::getLibXmlLoaderOptions()
150150
);
@@ -229,14 +229,14 @@ private static function getXmlName(XMLReader $xml): string
229229
/**
230230
* Loads PhpSpreadsheet from file.
231231
*/
232-
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
232+
protected function loadSpreadsheetFromFile($file): Spreadsheet
233233
{
234234
// Create new Spreadsheet
235235
$spreadsheet = new Spreadsheet();
236236
$spreadsheet->removeSheetByIndex(0);
237237

238238
// Load into this instance
239-
return $this->loadIntoExisting($filename, $spreadsheet);
239+
return $this->loadIntoExisting($file, $spreadsheet);
240240
}
241241

242242
/**

src/PhpSpreadsheet/Reader/Slk.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public function __construct()
6666
/**
6767
* Validate that the current file is a SYLK file.
6868
*/
69-
public function canRead(string $filename): bool
69+
public function canRead($file): bool
7070
{
7171
try {
72-
$this->openFile($filename);
72+
$this->openFile($file);
7373
} catch (ReaderException) {
7474
return false;
7575
}
@@ -133,15 +133,15 @@ public function getInputEncoding()
133133
/**
134134
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
135135
*/
136-
public function listWorksheetInfo(string $filename): array
136+
public function listWorksheetInfo($file): array
137137
{
138138
// Open file
139-
$this->canReadOrBust($filename);
139+
$this->canReadOrBust($file);
140140
$fileHandle = $this->fileHandle;
141141
rewind($fileHandle);
142142

143143
$worksheetInfo = [];
144-
$worksheetInfo[0]['worksheetName'] = basename($filename, '.slk');
144+
$worksheetInfo[0]['worksheetName'] = basename($file, '.slk');
145145

146146
// loop through one row (line) at a time in the file
147147
$rowIndex = 0;
@@ -189,13 +189,13 @@ public function listWorksheetInfo(string $filename): array
189189
/**
190190
* Loads PhpSpreadsheet from file.
191191
*/
192-
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
192+
protected function loadSpreadsheetFromFile($file): Spreadsheet
193193
{
194194
// Create new Spreadsheet
195195
$spreadsheet = new Spreadsheet();
196196

197197
// Load into this instance
198-
return $this->loadIntoExisting($filename, $spreadsheet);
198+
return $this->loadIntoExisting($file, $spreadsheet);
199199
}
200200

201201
private const COLOR_ARRAY = [

0 commit comments

Comments
 (0)