| 1 |
<?php |
| 2 |
|
| 3 |
namespace Box\Spout\Reader; |
| 4 |
|
| 5 |
use Box\Spout\Common\Exception\IOException; |
| 6 |
use Box\Spout\Reader\Exception\ReaderNotOpenedException; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class AbstractReader |
| 10 |
* |
| 11 |
* @package Box\Spout\Reader |
| 12 |
* @abstract |
| 13 |
*/ |
| 14 |
abstract class AbstractReader implements ReaderInterface |
| 15 |
{ |
| 16 |
/** @var bool Indicates whether the stream is currently open */ |
| 17 |
protected $isStreamOpened = false; |
| 18 |
|
| 19 |
/** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ |
| 20 |
protected $globalFunctionsHelper; |
| 21 |
|
| 22 |
/** @var \Box\Spout\Reader\Common\ReaderOptions Reader's customized options */ |
| 23 |
protected $options; |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns the reader's current options |
| 27 |
* |
| 28 |
* @return \Box\Spout\Reader\Common\ReaderOptions |
| 29 |
*/ |
| 30 |
abstract protected function getOptions(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns whether stream wrappers are supported |
| 34 |
* |
| 35 |
* @return bool |
| 36 |
*/ |
| 37 |
abstract protected function doesSupportStreamWrapper(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Opens the file at the given file path to make it ready to be read |
| 41 |
* |
| 42 |
* @param string $filePath Path of the file to be read |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
abstract protected function openReader($filePath); |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns an iterator to iterate over sheets. |
| 49 |
* |
| 50 |
* @return \Iterator To iterate over sheets |
| 51 |
*/ |
| 52 |
abstract protected function getConcreteSheetIterator(); |
| 53 |
|
| 54 |
/** |
| 55 |
* Closes the reader. To be used after reading the file. |
| 56 |
* |
| 57 |
* @return AbstractReader |
| 58 |
*/ |
| 59 |
abstract protected function closeReader(); |
| 60 |
|
| 61 |
/** |
| 62 |
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper |
| 63 |
* @return AbstractReader |
| 64 |
*/ |
| 65 |
public function setGlobalFunctionsHelper($globalFunctionsHelper) |
| 66 |
{ |
| 67 |
$this->globalFunctionsHelper = $globalFunctionsHelper; |
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Sets whether date/time values should be returned as PHP objects or be formatted as strings. |
| 73 |
* |
| 74 |
* @api |
| 75 |
* @param bool $shouldFormatDates |
| 76 |
* @return AbstractReader |
| 77 |
*/ |
| 78 |
public function setShouldFormatDates($shouldFormatDates) |
| 79 |
{ |
| 80 |
$this->getOptions()->setShouldFormatDates($shouldFormatDates); |
| 81 |
return $this; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Sets whether empty rows should be returned or skipped. |
| 86 |
* |
| 87 |
* @api |
| 88 |
* @param bool $shouldPreserveEmptyRows |
| 89 |
* @return AbstractReader |
| 90 |
*/ |
| 91 |
public function setShouldPreserveEmptyRows($shouldPreserveEmptyRows) |
| 92 |
{ |
| 93 |
$this->getOptions()->setShouldPreserveEmptyRows($shouldPreserveEmptyRows); |
| 94 |
return $this; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Prepares the reader to read the given file. It also makes sure |
| 99 |
* that the file exists and is readable. |
| 100 |
* |
| 101 |
* @api |
| 102 |
* @param string $filePath Path of the file to be read |
| 103 |
* @return void |
| 104 |
* @throws \Box\Spout\Common\Exception\IOException If the file at the given path does not exist, is not readable or is corrupted |
| 105 |
*/ |
| 106 |
public function open($filePath) |
| 107 |
{ |
| 108 |
if ($this->isStreamWrapper($filePath) && (!$this->doesSupportStreamWrapper() || !$this->isSupportedStreamWrapper($filePath))) { |
| 109 |
throw new IOException("Could not open $filePath for reading! Stream wrapper used is not supported for this type of file."); |
| 110 |
} |
| 111 |
|
| 112 |
if (!$this->isPhpStream($filePath)) { |
| 113 |
// we skip the checks if the provided file path points to a PHP stream |
| 114 |
if (!$this->globalFunctionsHelper->file_exists($filePath)) { |
| 115 |
throw new IOException("Could not open $filePath for reading! File does not exist."); |
| 116 |
} else if (!$this->globalFunctionsHelper->is_readable($filePath)) { |
| 117 |
throw new IOException("Could not open $filePath for reading! File is not readable."); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
try { |
| 122 |
$fileRealPath = $this->getFileRealPath($filePath); |
| 123 |
$this->openReader($fileRealPath); |
| 124 |
$this->isStreamOpened = true; |
| 125 |
} catch (\Exception $exception) { |
| 126 |
throw new IOException("Could not open $filePath for reading! ({$exception->getMessage()})"); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns the real path of the given path. |
| 132 |
* If the given path is a valid stream wrapper, returns the path unchanged. |
| 133 |
* |
| 134 |
* @param string $filePath |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
protected function getFileRealPath($filePath) |
| 138 |
{ |
| 139 |
if ($this->isSupportedStreamWrapper($filePath)) { |
| 140 |
return $filePath; |
| 141 |
} |
| 142 |
|
| 143 |
// Need to use realpath to fix "Can't open file" on some Windows setup |
| 144 |
return realpath($filePath); |
| 145 |
} |
| 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 |
* @return string|null The stream wrapper scheme or NULL if not a stream wrapper |
| 153 |
*/ |
| 154 |
protected function getStreamWrapperScheme($filePath) |
| 155 |
{ |
| 156 |
$streamScheme = null; |
| 157 |
if (preg_match('/^(\w+):\/\//', $filePath, $matches)) { |
| 158 |
$streamScheme = $matches[1]; |
| 159 |
} |
| 160 |
return $streamScheme; |
| 161 |
} |
| 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 |
* @return bool Whether the given path is an unsupported stream wrapper |
| 169 |
*/ |
| 170 |
protected function isStreamWrapper($filePath) |
| 171 |
{ |
| 172 |
return ($this->getStreamWrapperScheme($filePath) !== null); |
| 173 |
} |
| 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 |
* @return bool Whether the given path is an supported stream wrapper |
| 182 |
*/ |
| 183 |
protected function isSupportedStreamWrapper($filePath) |
| 184 |
{ |
| 185 |
$streamScheme = $this->getStreamWrapperScheme($filePath); |
| 186 |
return ($streamScheme !== null) ? |
| 187 |
in_array($streamScheme, $this->globalFunctionsHelper->stream_get_wrappers()) : |
| 188 |
true; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Checks if a path is a PHP stream (like php://output, php://memory, ...) |
| 193 |
* |
| 194 |
* @param string $filePath Path of the file to be read |
| 195 |
* @return bool Whether the given path maps to a PHP stream |
| 196 |
*/ |
| 197 |
protected function isPhpStream($filePath) |
| 198 |
{ |
| 199 |
$streamScheme = $this->getStreamWrapperScheme($filePath); |
| 200 |
return ($streamScheme === 'php'); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Returns an iterator to iterate over sheets. |
| 205 |
* |
| 206 |
* @api |
| 207 |
* @return \Iterator To iterate over sheets |
| 208 |
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException If called before opening the reader |
| 209 |
*/ |
| 210 |
public function getSheetIterator() |
| 211 |
{ |
| 212 |
if (!$this->isStreamOpened) { |
| 213 |
throw new ReaderNotOpenedException('Reader should be opened first.'); |
| 214 |
} |
| 215 |
|
| 216 |
return $this->getConcreteSheetIterator(); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Closes the reader, preventing any additional reading |
| 221 |
* |
| 222 |
* @api |
| 223 |
* @return void |
| 224 |
*/ |
| 225 |
public function close() |
| 226 |
{ |
| 227 |
if ($this->isStreamOpened) { |
| 228 |
$this->closeReader(); |
| 229 |
|
| 230 |
$sheetIterator = $this->getConcreteSheetIterator(); |
| 231 |
if ($sheetIterator) { |
| 232 |
$sheetIterator->end(); |
| 233 |
} |
| 234 |
|
| 235 |
$this->isStreamOpened = false; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|