Skip to content

Commit 38a69cd

Browse files
committed
change IReader functions to allow passing of resources
1 parent db35a41 commit 38a69cd

File tree

11 files changed

+287
-112
lines changed

11 files changed

+287
-112
lines changed

src/PhpSpreadsheet/Reader/BaseReader.php

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

163-
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
163+
/**
164+
* @param resource|string $file
165+
*/
166+
protected function loadSpreadsheetFromFile($file): Spreadsheet
164167
{
165168
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method');
166169
}
167170

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

179183
try {
180-
return $this->loadSpreadsheetFromFile($filename);
184+
return $this->loadSpreadsheetFromFile($file);
181185
} catch (ReaderException $e) {
182186
throw $e;
183187
}
184188
}
185189

186190
/**
187191
* Open file for reading.
192+
*
193+
* @param resource|string $file
188194
*/
189-
protected function openFile(string $filename): void
195+
protected function openFile($file): void
190196
{
191197
$fileHandle = false;
192-
if ($filename) {
193-
File::assertFile($filename);
198+
if (is_string($file)) {
199+
$filename = $file;
200+
File::assertFile($file);
194201

195202
// Open file
196-
$fileHandle = fopen($filename, 'rb');
203+
$fileHandle = fopen($file, 'rb');
204+
} elseif (is_resource($file)) {
205+
$filename = 'stream';
206+
$fileHandle = $file;
197207
}
198208
if ($fileHandle === false) {
199209
throw new ReaderException('Could not open file ' . $filename . ' for reading.');
@@ -204,8 +214,10 @@ protected function openFile(string $filename): void
204214

205215
/**
206216
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
217+
*
218+
* @param resource|string $file
207219
*/
208-
public function listWorksheetInfo(string $filename): array
220+
public function listWorksheetInfo($file): array
209221
{
210222
throw new PhpSpreadsheetException('Reader classes must implement their own listWorksheetInfo() method');
211223
}
@@ -215,11 +227,13 @@ public function listWorksheetInfo(string $filename): array
215227
* possibly without parsing the whole file to a Spreadsheet object.
216228
* Readers will often have a more efficient method with which
217229
* they can override this method.
230+
*
231+
* @param resource|string $file
218232
*/
219-
public function listWorksheetNames(string $filename): array
233+
public function listWorksheetNames($file): array
220234
{
221235
$returnArray = [];
222-
$info = $this->listWorksheetInfo($filename);
236+
$info = $this->listWorksheetInfo($file);
223237
foreach ($info as $infoArray) {
224238
if (isset($infoArray['worksheetName'])) {
225239
$returnArray[] = $infoArray['worksheetName'];

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,13 @@ protected function inferSeparator(): void
216216

217217
/**
218218
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
219+
*
220+
* @param resource|string $file
219221
*/
220-
public function listWorksheetInfo(string $filename): array
222+
public function listWorksheetInfo($file): array
221223
{
222224
// Open file
223-
$this->openFileOrMemory($filename);
225+
$this->openFileOrMemory($file);
224226
$fileHandle = $this->fileHandle;
225227

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

255257
/**
256258
* Loads Spreadsheet from file.
259+
*
260+
* @param resource|string $file
257261
*/
258-
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
262+
protected function loadSpreadsheetFromFile($file): Spreadsheet
259263
{
260264
// Create new Spreadsheet
261265
$spreadsheet = new Spreadsheet();
262266

263267
// Load into this instance
264-
return $this->loadIntoExisting($filename, $spreadsheet);
268+
return $this->loadIntoExisting($file, $spreadsheet);
265269
}
266270

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

279-
private function openFileOrMemory(string $filename): void
283+
/**
284+
* @param resource|string $filename
285+
*/
286+
private function openFileOrMemory($filename): void
280287
{
281288
// Open file
282289
$fhandle = $this->canRead($filename);
@@ -345,6 +352,8 @@ private function openDataUri(string $filename): void
345352

346353
/**
347354
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
355+
*
356+
* @param resource|string $file
348357
*/
349358
public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet
350359
{
@@ -353,6 +362,8 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp
353362

354363
/**
355364
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
365+
*
366+
* @param resource|string $file
356367
*/
357368
private function loadStringOrFile(string $filename, Spreadsheet $spreadsheet, bool $dataUri): Spreadsheet
358369
{
@@ -539,25 +550,25 @@ public function getEscapeCharacter(): string
539550
/**
540551
* Can the current IReader read the file?
541552
*/
542-
public function canRead(string $filename): bool
553+
public function canRead($file): bool
543554
{
544555
// Check if file exists
545556
try {
546-
$this->openFile($filename);
557+
$this->openFile($file);
547558
} catch (ReaderException) {
548559
return false;
549560
}
550561

551562
fclose($this->fileHandle);
552563

553564
// Trust file extension if any
554-
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
565+
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
555566
if (in_array($extension, ['csv', 'tsv'])) {
556567
return true;
557568
}
558569

559570
// Attempt to guess mimetype
560-
$type = mime_content_type($filename);
571+
$type = mime_content_type($file);
561572
$supportedTypes = [
562573
'application/csv',
563574
'text/csv',

src/PhpSpreadsheet/Reader/Gnumeric.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ public function __construct()
7676

7777
/**
7878
* Can the current IReader read the file?
79+
*
80+
* @param resource|string $file
7981
*/
80-
public function canRead(string $filename): bool
82+
public function canRead($file): bool
8183
{
84+
if (is_resource($file)) {
85+
return false; // TODO
86+
}
87+
8288
$data = null;
83-
if (File::testFileNoThrow($filename)) {
84-
$data = $this->gzfileGetContents($filename);
89+
if (File::testFileNoThrow($file)) {
90+
$data = $this->gzfileGetContents($file);
8591
if (!str_contains($data, self::NAMESPACE_GNM)) {
8692
$data = '';
8793
}
@@ -99,16 +105,22 @@ private static function matchXml(XMLReader $xml, string $expectedLocalName): boo
99105

100106
/**
101107
* Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
108+
*
109+
* @param resource|string $file
102110
*/
103-
public function listWorksheetNames(string $filename): array
111+
public function listWorksheetNames($file): array
104112
{
105-
File::assertFile($filename);
106-
if (!$this->canRead($filename)) {
107-
throw new Exception($filename . ' is an invalid Gnumeric file.');
113+
if (is_resource($file)) {
114+
throw new Exception('file as stream not supported');
115+
}
116+
117+
File::assertFile($file);
118+
if (!$this->canRead($file)) {
119+
throw new Exception($file . ' is an invalid Gnumeric file.');
108120
}
109121

110122
$xml = new XMLReader();
111-
$contents = $this->gzfileGetContents($filename);
123+
$contents = $this->gzfileGetContents($file);
112124
$xml->xml($contents, null, Settings::getLibXmlLoaderOptions());
113125
$xml->setParserProperty(2, true);
114126

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

129141
/**
130142
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
143+
*
144+
* @param resource|string $file
131145
*/
132-
public function listWorksheetInfo(string $filename): array
146+
public function listWorksheetInfo($file): array
133147
{
134-
File::assertFile($filename);
135-
if (!$this->canRead($filename)) {
136-
throw new Exception($filename . ' is an invalid Gnumeric file.');
148+
if (is_resource($file)) {
149+
throw new Exception('file as stream not supported');
150+
}
151+
152+
File::assertFile($file);
153+
if (!$this->canRead($file)) {
154+
throw new Exception($file . ' is an invalid Gnumeric file.');
137155
}
138156

139157
$xml = new XMLReader();
140-
$contents = $this->gzfileGetContents($filename);
158+
$contents = $this->gzfileGetContents($file);
141159
$xml->xml($contents, null, Settings::getLibXmlLoaderOptions());
142160
$xml->setParserProperty(2, true);
143161

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

230248
/**
231249
* Loads Spreadsheet from file.
250+
*
251+
* @param resource|string $file
232252
*/
233-
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
253+
protected function loadSpreadsheetFromFile($file): Spreadsheet
234254
{
255+
if (is_resource($file)) {
256+
throw new Exception('file as stream not supported');
257+
}
258+
235259
// Create new Spreadsheet
236260
$spreadsheet = new Spreadsheet();
237261
$spreadsheet->removeSheetByIndex(0);
238262

239263
// Load into this instance
240-
return $this->loadIntoExisting($filename, $spreadsheet);
264+
return $this->loadIntoExisting($file, $spreadsheet);
241265
}
242266

243267
/**

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,14 @@ public function __construct()
138138

139139
/**
140140
* Validate that the current file is an HTML file.
141+
*
142+
* @param resource|string $file
141143
*/
142-
public function canRead(string $filename): bool
144+
public function canRead($file): bool
143145
{
144146
// Check if file exists
145147
try {
146-
$this->openFile($filename);
148+
$this->openFile($file);
147149
} catch (Exception) {
148150
return false;
149151
}
@@ -202,14 +204,16 @@ private static function containsTags(string $data): bool
202204

203205
/**
204206
* Loads Spreadsheet from file.
207+
*
208+
* @param resource|string $file
205209
*/
206-
public function loadSpreadsheetFromFile(string $filename): Spreadsheet
210+
public function loadSpreadsheetFromFile($file): Spreadsheet
207211
{
208212
// Create new Spreadsheet
209213
$spreadsheet = new Spreadsheet();
210214

211215
// Load into this instance
212-
return $this->loadIntoExisting($filename, $spreadsheet);
216+
return $this->loadIntoExisting($file, $spreadsheet);
213217
}
214218

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

662666
/**
663667
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
668+
*
669+
* @param resource|string $file
664670
*/
665-
public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet
671+
public function loadIntoExisting($file, Spreadsheet $spreadsheet): Spreadsheet
666672
{
673+
if (is_resource($file)) {
674+
throw new Exception('file as stream not supported');
675+
}
676+
667677
// Validate
668-
if (!$this->canRead($filename)) {
669-
throw new Exception($filename . ' is an Invalid HTML file.');
678+
if (!$this->canRead($file)) {
679+
throw new Exception($file . ' is an Invalid HTML file.');
670680
}
671681

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

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

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

11531163
/**
11541164
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
1165+
*
1166+
* @param resource|string $file
11551167
*/
1156-
public function listWorksheetInfo(string $filename): array
1168+
public function listWorksheetInfo($file): array
11571169
{
1170+
if (is_resource($file)) {
1171+
throw new Exception('file as stream not supported');
1172+
}
1173+
11581174
$info = [];
11591175
$spreadsheet = new Spreadsheet();
1160-
$this->loadIntoExisting($filename, $spreadsheet);
1176+
$this->loadIntoExisting($file, $spreadsheet);
11611177
foreach ($spreadsheet->getAllSheets() as $sheet) {
11621178
$newEntry = ['worksheetName' => $sheet->getTitle()];
11631179
$newEntry['lastColumnLetter'] = $sheet->getHighestDataColumn();

0 commit comments

Comments
 (0)