Skip to content

Commit 579145e

Browse files
author
Mark Baker
authored
Wrap actual file loading in protected methods, while just providing a stub load() method in the BaseReader abstract. This is to allow a wrapper in the file loader logic across all Readers, with a try/catch block that can be used to handle special loader settings. (#2620)
1 parent 16953e2 commit 579145e

File tree

10 files changed

+35
-47
lines changed

10 files changed

+35
-47
lines changed

src/PhpSpreadsheet/Reader/BaseReader.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Reader;
44

5+
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
56
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
67
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
78
use PhpOffice\PhpSpreadsheet\Shared\File;
9+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
810

911
abstract class BaseReader implements IReader
1012
{
@@ -144,25 +146,41 @@ protected function processFlags(int $flags): void
144146
}
145147
}
146148

149+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
150+
{
151+
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method');
152+
}
153+
154+
/**
155+
* Loads Spreadsheet from file.
156+
*/
157+
public function load(string $filename, int $flags = 0): Spreadsheet
158+
{
159+
$this->processFlags($flags);
160+
161+
try {
162+
return $this->loadSpreadsheetFromFile($filename);
163+
} catch (ReaderException $e) {
164+
throw $e;
165+
}
166+
}
167+
147168
/**
148169
* Open file for reading.
149-
*
150-
* @param string $filename
151170
*/
152-
protected function openFile($filename): void
171+
protected function openFile(string $filename): void
153172
{
173+
$fileHandle = false;
154174
if ($filename) {
155175
File::assertFile($filename);
156176

157177
// Open file
158178
$fileHandle = fopen($filename, 'rb');
159-
} else {
160-
$fileHandle = false;
161179
}
162-
if ($fileHandle !== false) {
163-
$this->fileHandle = $fileHandle;
164-
} else {
180+
if ($fileHandle === false) {
165181
throw new ReaderException('Could not open file ' . $filename . ' for reading.');
166182
}
183+
184+
$this->fileHandle = $fileHandle;
167185
}
168186
}

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,9 @@ public function listWorksheetInfo(string $filename): array
236236

237237
/**
238238
* Loads Spreadsheet from file.
239-
*
240-
* @return Spreadsheet
241239
*/
242-
public function load(string $filename, int $flags = 0)
240+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
243241
{
244-
$this->processFlags($flags);
245-
246242
// Create new Spreadsheet
247243
$spreadsheet = new Spreadsheet();
248244

src/PhpSpreadsheet/Reader/Gnumeric.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,9 @@ private static function testSimpleXml($value): SimpleXMLElement
227227

228228
/**
229229
* Loads Spreadsheet from file.
230-
*
231-
* @return Spreadsheet
232230
*/
233-
public function load(string $filename, int $flags = 0)
231+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
234232
{
235-
$this->processFlags($flags);
236-
237233
// Create new Spreadsheet
238234
$spreadsheet = new Spreadsheet();
239235
$spreadsheet->removeSheetByIndex(0);

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
2020
use Throwable;
2121

22-
/** PhpSpreadsheet root directory */
2322
class Html extends BaseReader
2423
{
2524
/**
@@ -201,13 +200,9 @@ private static function containsTags(string $data): bool
201200

202201
/**
203202
* Loads Spreadsheet from file.
204-
*
205-
* @return Spreadsheet
206203
*/
207-
public function load(string $filename, int $flags = 0)
204+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
208205
{
209-
$this->processFlags($flags);
210-
211206
// Create new Spreadsheet
212207
$spreadsheet = new Spreadsheet();
213208

src/PhpSpreadsheet/Reader/Ods.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,9 @@ public function listWorksheetInfo($filename)
217217

218218
/**
219219
* Loads PhpSpreadsheet from file.
220-
*
221-
* @return Spreadsheet
222220
*/
223-
public function load(string $filename, int $flags = 0)
221+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
224222
{
225-
$this->processFlags($flags);
226-
227223
// Create new Spreadsheet
228224
$spreadsheet = new Spreadsheet();
229225

src/PhpSpreadsheet/Reader/Slk.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,9 @@ public function listWorksheetInfo($filename)
191191

192192
/**
193193
* Loads PhpSpreadsheet from file.
194-
*
195-
* @return Spreadsheet
196194
*/
197-
public function load(string $filename, int $flags = 0)
195+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
198196
{
199-
$this->processFlags($flags);
200-
201197
// Create new Spreadsheet
202198
$spreadsheet = new Spreadsheet();
203199

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,13 +620,9 @@ public function listWorksheetInfo($filename)
620620

621621
/**
622622
* Loads PhpSpreadsheet from file.
623-
*
624-
* @return Spreadsheet
625623
*/
626-
public function load(string $filename, int $flags = 0)
624+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
627625
{
628-
$this->processFlags($flags);
629-
630626
// Read the OLE file
631627
$this->loadOLE($filename);
632628

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,9 @@ private function getFromZipArchive(ZipArchive $archive, $fileName = '')
384384
/**
385385
* Loads Spreadsheet from file.
386386
*/
387-
public function load(string $filename, int $flags = 0): Spreadsheet
387+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
388388
{
389389
File::assertFile($filename, self::INITIAL_FILE);
390-
$this->processFlags($flags);
391390

392391
// Initialisations
393392
$excel = new Spreadsheet();

src/PhpSpreadsheet/Reader/Xml.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,9 @@ public function listWorksheetInfo($filename)
231231

232232
/**
233233
* Loads Spreadsheet from file.
234-
*
235-
* @return Spreadsheet
236234
*/
237-
public function load(string $filename, int $flags = 0)
235+
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
238236
{
239-
$this->processFlags($flags);
240-
241237
// Create new Spreadsheet
242238
$spreadsheet = new Spreadsheet();
243239
$spreadsheet->removeSheetByIndex(0);

tests/PhpSpreadsheetTests/Calculation/CalclationFunctionListTest.php renamed to tests/PhpSpreadsheetTests/Calculation/CalculationFunctionListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpOffice\PhpSpreadsheet\Spreadsheet;
88
use PHPUnit\Framework\TestCase;
99

10-
class CalclationFunctionListTest extends TestCase
10+
class CalculationFunctionListTest extends TestCase
1111
{
1212
/**
1313
* @var string

0 commit comments

Comments
 (0)