1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace OpenSpout\Reader\XLSX;
|
|
|
6 |
|
|
|
7 |
use OpenSpout\Common\Exception\IOException;
|
|
|
8 |
use OpenSpout\Common\Helper\Escaper\XLSX;
|
|
|
9 |
use OpenSpout\Reader\AbstractReader;
|
|
|
10 |
use OpenSpout\Reader\Exception\NoSheetsFoundException;
|
|
|
11 |
use OpenSpout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
|
|
|
12 |
use OpenSpout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactoryInterface;
|
|
|
13 |
use OpenSpout\Reader\XLSX\Manager\SharedStringsCaching\MemoryLimit;
|
|
|
14 |
use OpenSpout\Reader\XLSX\Manager\SharedStringsManager;
|
|
|
15 |
use OpenSpout\Reader\XLSX\Manager\SheetManager;
|
|
|
16 |
use OpenSpout\Reader\XLSX\Manager\WorkbookRelationshipsManager;
|
|
|
17 |
use ZipArchive;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* @extends AbstractReader<SheetIterator>
|
|
|
21 |
*/
|
|
|
22 |
final class Reader extends AbstractReader
|
|
|
23 |
{
|
|
|
24 |
private ZipArchive $zip;
|
|
|
25 |
|
|
|
26 |
/** @var SharedStringsManager Manages shared strings */
|
|
|
27 |
private SharedStringsManager $sharedStringsManager;
|
|
|
28 |
|
|
|
29 |
/** @var SheetIterator To iterator over the XLSX sheets */
|
|
|
30 |
private SheetIterator $sheetIterator;
|
|
|
31 |
|
|
|
32 |
private readonly Options $options;
|
|
|
33 |
private readonly CachingStrategyFactoryInterface $cachingStrategyFactory;
|
|
|
34 |
|
|
|
35 |
public function __construct(
|
|
|
36 |
?Options $options = null,
|
|
|
37 |
?CachingStrategyFactoryInterface $cachingStrategyFactory = null
|
|
|
38 |
) {
|
|
|
39 |
$this->options = $options ?? new Options();
|
|
|
40 |
|
|
|
41 |
if (null === $cachingStrategyFactory) {
|
|
|
42 |
$memoryLimit = \ini_get('memory_limit');
|
|
|
43 |
$cachingStrategyFactory = new CachingStrategyFactory(new MemoryLimit($memoryLimit));
|
|
|
44 |
}
|
|
|
45 |
$this->cachingStrategyFactory = $cachingStrategyFactory;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public function getSheetIterator(): SheetIterator
|
|
|
49 |
{
|
|
|
50 |
$this->ensureStreamOpened();
|
|
|
51 |
|
|
|
52 |
return $this->sheetIterator;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Returns whether stream wrappers are supported.
|
|
|
57 |
*/
|
|
|
58 |
protected function doesSupportStreamWrapper(): bool
|
|
|
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 IOException If the file at the given path or its content cannot be read
|
|
|
71 |
* @throws NoSheetsFoundException If there are no sheets in the file
|
|
|
72 |
*/
|
|
|
73 |
protected function openReader(string $filePath): void
|
|
|
74 |
{
|
|
|
75 |
$this->zip = new ZipArchive();
|
|
|
76 |
|
|
|
77 |
if (true !== $this->zip->open($filePath)) {
|
|
|
78 |
throw new IOException("Could not open {$filePath} for reading.");
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$this->sharedStringsManager = new SharedStringsManager(
|
|
|
82 |
$filePath,
|
|
|
83 |
$this->options,
|
|
|
84 |
new WorkbookRelationshipsManager($filePath),
|
|
|
85 |
$this->cachingStrategyFactory
|
|
|
86 |
);
|
|
|
87 |
|
|
|
88 |
if ($this->sharedStringsManager->hasSharedStrings()) {
|
|
|
89 |
// Extracts all the strings from the sheets for easy access in the future
|
|
|
90 |
$this->sharedStringsManager->extractSharedStrings();
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$this->sheetIterator = new SheetIterator(
|
|
|
94 |
new SheetManager(
|
|
|
95 |
$filePath,
|
|
|
96 |
$this->options,
|
|
|
97 |
$this->sharedStringsManager,
|
|
|
98 |
new XLSX()
|
|
|
99 |
)
|
|
|
100 |
);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Closes the reader. To be used after reading the file.
|
|
|
105 |
*/
|
|
|
106 |
protected function closeReader(): void
|
|
|
107 |
{
|
|
|
108 |
$this->zip->close();
|
|
|
109 |
$this->sharedStringsManager->cleanup();
|
|
|
110 |
}
|
|
|
111 |
}
|