| 1 |
<?php |
| 2 |
|
| 3 |
namespace OpenSpout\Reader\XLSX; |
| 4 |
|
| 5 |
use OpenSpout\Common\Exception\IOException; |
| 6 |
use OpenSpout\Common\Helper\GlobalFunctionsHelper; |
| 7 |
use OpenSpout\Common\Manager\OptionsManagerInterface; |
| 8 |
use OpenSpout\Reader\Common\Creator\InternalEntityFactoryInterface; |
| 9 |
use OpenSpout\Reader\Common\Entity\Options; |
| 10 |
use OpenSpout\Reader\ReaderAbstract; |
| 11 |
use OpenSpout\Reader\XLSX\Creator\InternalEntityFactory; |
| 12 |
use OpenSpout\Reader\XLSX\Creator\ManagerFactory; |
| 13 |
|
| 14 |
/** |
| 15 |
* This class provides support to read data from a XLSX file. |
| 16 |
*/ |
| 17 |
class Reader extends ReaderAbstract |
| 18 |
{ |
| 19 |
/** @var ManagerFactory */ |
| 20 |
protected $managerFactory; |
| 21 |
|
| 22 |
/** @var \ZipArchive */ |
| 23 |
protected $zip; |
| 24 |
|
| 25 |
/** @var \OpenSpout\Reader\XLSX\Manager\SharedStringsManager Manages shared strings */ |
| 26 |
protected $sharedStringsManager; |
| 27 |
|
| 28 |
/** @var SheetIterator To iterator over the XLSX sheets */ |
| 29 |
protected $sheetIterator; |
| 30 |
|
| 31 |
public function __construct( |
| 32 |
OptionsManagerInterface $optionsManager, |
| 33 |
GlobalFunctionsHelper $globalFunctionsHelper, |
| 34 |
InternalEntityFactoryInterface $entityFactory, |
| 35 |
ManagerFactory $managerFactory |
| 36 |
) { |
| 37 |
parent::__construct($optionsManager, $globalFunctionsHelper, $entityFactory); |
| 38 |
$this->managerFactory = $managerFactory; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param string $tempFolder Temporary folder where the temporary files will be created |
| 43 |
* |
| 44 |
* @return Reader |
| 45 |
*/ |
| 46 |
public function setTempFolder($tempFolder) |
| 47 |
{ |
| 48 |
$this->optionsManager->setOption(Options::TEMP_FOLDER, $tempFolder); |
| 49 |
|
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns whether stream wrappers are supported. |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
protected function doesSupportStreamWrapper() |
| 59 |
{ |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Opens the file at the given file path to make it ready to be read. |
| 65 |
* It also parses the sharedStrings.xml file to get all the shared strings available in memory |
| 66 |
* and fetches all the available sheets. |
| 67 |
* |
| 68 |
* @param string $filePath Path of the file to be read |
| 69 |
* |
| 70 |
* @throws \OpenSpout\Common\Exception\IOException If the file at the given path or its content cannot be read |
| 71 |
* @throws \OpenSpout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file |
| 72 |
*/ |
| 73 |
protected function openReader($filePath) |
| 74 |
{ |
| 75 |
/** @var InternalEntityFactory $entityFactory */ |
| 76 |
$entityFactory = $this->entityFactory; |
| 77 |
|
| 78 |
$this->zip = $entityFactory->createZipArchive(); |
| 79 |
|
| 80 |
if (true === $this->zip->open($filePath)) { |
| 81 |
$tempFolder = $this->optionsManager->getOption(Options::TEMP_FOLDER); |
| 82 |
$this->sharedStringsManager = $this->managerFactory->createSharedStringsManager($filePath, $tempFolder, $entityFactory); |
| 83 |
|
| 84 |
if ($this->sharedStringsManager->hasSharedStrings()) { |
| 85 |
// Extracts all the strings from the sheets for easy access in the future |
| 86 |
$this->sharedStringsManager->extractSharedStrings(); |
| 87 |
} |
| 88 |
|
| 89 |
$this->sheetIterator = $entityFactory->createSheetIterator( |
| 90 |
$filePath, |
| 91 |
$this->optionsManager, |
| 92 |
$this->sharedStringsManager |
| 93 |
); |
| 94 |
} else { |
| 95 |
throw new IOException("Could not open {$filePath} for reading."); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns an iterator to iterate over sheets. |
| 101 |
* |
| 102 |
* @return SheetIterator To iterate over sheets |
| 103 |
*/ |
| 104 |
protected function getConcreteSheetIterator() |
| 105 |
{ |
| 106 |
return $this->sheetIterator; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Closes the reader. To be used after reading the file. |
| 111 |
*/ |
| 112 |
protected function closeReader() |
| 113 |
{ |
| 114 |
if (null !== $this->zip) { |
| 115 |
$this->zip->close(); |
| 116 |
} |
| 117 |
|
| 118 |
if (null !== $this->sharedStringsManager) { |
| 119 |
$this->sharedStringsManager->cleanup(); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|