PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.19
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.19
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / vendor / openspout / openspout / src / Reader / ReaderAbstract.php

ReaderAbstract.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.19, at vendor/openspout/openspout/src/Reader/ReaderAbstract.php

202 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\OpenSpout\Reader;
4
5 use FluentCart\OpenSpout\Common\Exception\IOException;
6 use FluentCart\OpenSpout\Common\Helper\GlobalFunctionsHelper;
7 use FluentCart\OpenSpout\Common\Manager\OptionsManagerInterface;
8 use FluentCart\OpenSpout\Reader\Common\Creator\InternalEntityFactoryInterface;
9 use FluentCart\OpenSpout\Reader\Common\Entity\Options;
10 use FluentCart\OpenSpout\Reader\Exception\ReaderNotOpenedException;
11 abstract class ReaderAbstract implements ReaderInterface
12 {
13 /** @var bool Indicates whether the stream is currently open */
14 protected $isStreamOpened = \false;
15 /** @var InternalEntityFactoryInterface Factory to create entities */
16 protected $entityFactory;
17 /** @var \OpenSpout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */
18 protected $globalFunctionsHelper;
19 /** @var OptionsManagerInterface Writer options manager */
20 protected $optionsManager;
21 public function __construct(OptionsManagerInterface $optionsManager, GlobalFunctionsHelper $globalFunctionsHelper, InternalEntityFactoryInterface $entityFactory)
22 {
23 $this->optionsManager = $optionsManager;
24 $this->globalFunctionsHelper = $globalFunctionsHelper;
25 $this->entityFactory = $entityFactory;
26 }
27 /**
28 * Sets whether date/time values should be returned as PHP objects or be formatted as strings.
29 *
30 * @param bool $shouldFormatDates
31 *
32 * @return ReaderAbstract
33 */
34 public function setShouldFormatDates($shouldFormatDates)
35 {
36 $this->optionsManager->setOption(Options::SHOULD_FORMAT_DATES, $shouldFormatDates);
37 return $this;
38 }
39 /**
40 * Sets whether empty rows should be returned or skipped.
41 *
42 * @param bool $shouldPreserveEmptyRows
43 *
44 * @return ReaderAbstract
45 */
46 public function setShouldPreserveEmptyRows($shouldPreserveEmptyRows)
47 {
48 $this->optionsManager->setOption(Options::SHOULD_PRESERVE_EMPTY_ROWS, $shouldPreserveEmptyRows);
49 return $this;
50 }
51 /**
52 * Prepares the reader to read the given file. It also makes sure
53 * that the file exists and is readable.
54 *
55 * @param string $filePath Path of the file to be read
56 *
57 * @throws \OpenSpout\Common\Exception\IOException If the file at the given path does not exist, is not readable or is corrupted
58 */
59 public function open($filePath)
60 {
61 if ($this->isStreamWrapper($filePath) && (!$this->doesSupportStreamWrapper() || !$this->isSupportedStreamWrapper($filePath))) {
62 throw new IOException("Could not open {$filePath} for reading! Stream wrapper used is not supported for this type of file.");
63 }
64 if (!$this->isPhpStream($filePath)) {
65 // we skip the checks if the provided file path points to a PHP stream
66 if (!$this->globalFunctionsHelper->file_exists($filePath)) {
67 throw new IOException("Could not open {$filePath} for reading! File does not exist.");
68 }
69 if (!$this->globalFunctionsHelper->is_readable($filePath)) {
70 throw new IOException("Could not open {$filePath} for reading! File is not readable.");
71 }
72 }
73 try {
74 $fileRealPath = $this->getFileRealPath($filePath);
75 $this->openReader($fileRealPath);
76 $this->isStreamOpened = \true;
77 } catch (\Exception $exception) {
78 throw new IOException("Could not open {$filePath} for reading! ({$exception->getMessage()})");
79 }
80 }
81 /**
82 * Returns an iterator to iterate over sheets.
83 *
84 * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException If called before opening the reader
85 *
86 * @return SheetIteratorInterface To iterate over sheets
87 */
88 public function getSheetIterator()
89 {
90 if (!$this->isStreamOpened) {
91 throw new ReaderNotOpenedException('Reader should be opened first.');
92 }
93 return $this->getConcreteSheetIterator();
94 }
95 /**
96 * Closes the reader, preventing any additional reading.
97 */
98 public function close()
99 {
100 if ($this->isStreamOpened) {
101 $this->closeReader();
102 $sheetIterator = $this->getConcreteSheetIterator();
103 if (null !== $sheetIterator) {
104 $sheetIterator->end();
105 }
106 $this->isStreamOpened = \false;
107 }
108 }
109 /**
110 * Returns whether stream wrappers are supported.
111 *
112 * @return bool
113 */
114 protected abstract function doesSupportStreamWrapper();
115 /**
116 * Opens the file at the given file path to make it ready to be read.
117 *
118 * @param string $filePath Path of the file to be read
119 */
120 protected abstract function openReader($filePath);
121 /**
122 * Returns an iterator to iterate over sheets.
123 *
124 * @return SheetIteratorInterface To iterate over sheets
125 */
126 protected abstract function getConcreteSheetIterator();
127 /**
128 * Closes the reader. To be used after reading the file.
129 */
130 protected abstract function closeReader();
131 /**
132 * Returns the real path of the given path.
133 * If the given path is a valid stream wrapper, returns the path unchanged.
134 *
135 * @param string $filePath
136 *
137 * @return string
138 */
139 protected function getFileRealPath($filePath)
140 {
141 if ($this->isSupportedStreamWrapper($filePath)) {
142 return $filePath;
143 }
144 // Need to use realpath to fix "Can't open file" on some Windows setup
145 return \realpath($filePath);
146 }
147 /**
148 * Returns the scheme of the custom stream wrapper, if the path indicates a stream wrapper is used.
149 * For example, php://temp => php, s3://path/to/file => s3...
150 *
151 * @param string $filePath Path of the file to be read
152 *
153 * @return null|string The stream wrapper scheme or NULL if not a stream wrapper
154 */
155 protected function getStreamWrapperScheme($filePath)
156 {
157 $streamScheme = null;
158 if (\preg_match('/^(\\w+):\\/\\//', $filePath, $matches)) {
159 $streamScheme = $matches[1];
160 }
161 return $streamScheme;
162 }
163 /**
164 * Checks if the given path is an unsupported stream wrapper
165 * (like local path, php://temp, mystream://foo/bar...).
166 *
167 * @param string $filePath Path of the file to be read
168 *
169 * @return bool Whether the given path is an unsupported stream wrapper
170 */
171 protected function isStreamWrapper($filePath)
172 {
173 return null !== $this->getStreamWrapperScheme($filePath);
174 }
175 /**
176 * Checks if the given path is an supported stream wrapper
177 * (like php://temp, mystream://foo/bar...).
178 * If the given path is a local path, returns true.
179 *
180 * @param string $filePath Path of the file to be read
181 *
182 * @return bool Whether the given path is an supported stream wrapper
183 */
184 protected function isSupportedStreamWrapper($filePath)
185 {
186 $streamScheme = $this->getStreamWrapperScheme($filePath);
187 return null !== $streamScheme ? \in_array($streamScheme, $this->globalFunctionsHelper->stream_get_wrappers(), \true) : \true;
188 }
189 /**
190 * Checks if a path is a PHP stream (like php://output, php://memory, ...).
191 *
192 * @param string $filePath Path of the file to be read
193 *
194 * @return bool Whether the given path maps to a PHP stream
195 */
196 protected function isPhpStream($filePath)
197 {
198 $streamScheme = $this->getStreamWrapperScheme($filePath);
199 return 'php' === $streamScheme;
200 }
201 }
202