| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\OpenSpout\Common\Helper; |
| 4 |
|
| 5 |
/** |
| 6 |
* This class wraps global functions to facilitate testing. |
| 7 |
* |
| 8 |
* @codeCoverageIgnore |
| 9 |
*/ |
| 10 |
class GlobalFunctionsHelper |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Wrapper around global function fopen(). |
| 14 |
* |
| 15 |
* @see fopen() |
| 16 |
* |
| 17 |
* @param string $fileName |
| 18 |
* @param string $mode |
| 19 |
* |
| 20 |
* @return bool|resource |
| 21 |
*/ |
| 22 |
public function fopen($fileName, $mode) |
| 23 |
{ |
| 24 |
return \fopen($fileName, $mode); |
| 25 |
} |
| 26 |
/** |
| 27 |
* Wrapper around global function fgets(). |
| 28 |
* |
| 29 |
* @see fgets() |
| 30 |
* |
| 31 |
* @param resource $handle |
| 32 |
* @param null|int $length |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public function fgets($handle, $length = null) |
| 37 |
{ |
| 38 |
return \fgets($handle, $length); |
| 39 |
} |
| 40 |
/** |
| 41 |
* Wrapper around global function fputs(). |
| 42 |
* |
| 43 |
* @see fputs() |
| 44 |
* |
| 45 |
* @param resource $handle |
| 46 |
* @param string $string |
| 47 |
* |
| 48 |
* @return int |
| 49 |
*/ |
| 50 |
public function fputs($handle, $string) |
| 51 |
{ |
| 52 |
return \fwrite($handle, $string); |
| 53 |
} |
| 54 |
/** |
| 55 |
* Wrapper around global function fflush(). |
| 56 |
* |
| 57 |
* @see fflush() |
| 58 |
* |
| 59 |
* @param resource $handle |
| 60 |
* |
| 61 |
* @return bool |
| 62 |
*/ |
| 63 |
public function fflush($handle) |
| 64 |
{ |
| 65 |
return \fflush($handle); |
| 66 |
} |
| 67 |
/** |
| 68 |
* Wrapper around global function fseek(). |
| 69 |
* |
| 70 |
* @see fseek() |
| 71 |
* |
| 72 |
* @param resource $handle |
| 73 |
* @param int $offset |
| 74 |
* |
| 75 |
* @return int |
| 76 |
*/ |
| 77 |
public function fseek($handle, $offset) |
| 78 |
{ |
| 79 |
return \fseek($handle, $offset); |
| 80 |
} |
| 81 |
/** |
| 82 |
* Wrapper around global function fgetcsv(). |
| 83 |
* |
| 84 |
* @see fgetcsv() |
| 85 |
* |
| 86 |
* @param resource $handle |
| 87 |
* @param null|int $length |
| 88 |
* @param null|string $delimiter |
| 89 |
* @param null|string $enclosure |
| 90 |
* |
| 91 |
* @return array|false |
| 92 |
*/ |
| 93 |
public function fgetcsv($handle, $length = null, $delimiter = null, $enclosure = null) |
| 94 |
{ |
| 95 |
/** |
| 96 |
* PHP uses '\' as the default escape character. This is not RFC-4180 compliant... |
| 97 |
* To fix that, simply disable the escape character. |
| 98 |
* |
| 99 |
* @see https://bugs.php.net/bug.php?id=43225 |
| 100 |
* @see http://tools.ietf.org/html/rfc4180 |
| 101 |
*/ |
| 102 |
$escapeCharacter = \PHP_VERSION_ID >= 70400 ? '' : "\x00"; |
| 103 |
return \fgetcsv($handle, $length, $delimiter, $enclosure, $escapeCharacter); |
| 104 |
} |
| 105 |
/** |
| 106 |
* Wrapper around global function fputcsv(). |
| 107 |
* |
| 108 |
* @see fputcsv() |
| 109 |
* |
| 110 |
* @param resource $handle |
| 111 |
* @param null|string $delimiter |
| 112 |
* @param null|string $enclosure |
| 113 |
* |
| 114 |
* @return false|int |
| 115 |
*/ |
| 116 |
public function fputcsv($handle, array $fields, $delimiter = null, $enclosure = null) |
| 117 |
{ |
| 118 |
/** |
| 119 |
* PHP uses '\' as the default escape character. This is not RFC-4180 compliant... |
| 120 |
* To fix that, simply disable the escape character. |
| 121 |
* |
| 122 |
* @see https://bugs.php.net/bug.php?id=43225 |
| 123 |
* @see http://tools.ietf.org/html/rfc4180 |
| 124 |
*/ |
| 125 |
$escapeCharacter = \PHP_VERSION_ID >= 70400 ? '' : "\x00"; |
| 126 |
return \fputcsv($handle, $fields, $delimiter, $enclosure, $escapeCharacter); |
| 127 |
} |
| 128 |
/** |
| 129 |
* Wrapper around global function fwrite(). |
| 130 |
* |
| 131 |
* @see fwrite() |
| 132 |
* |
| 133 |
* @param resource $handle |
| 134 |
* @param string $string |
| 135 |
* |
| 136 |
* @return int |
| 137 |
*/ |
| 138 |
public function fwrite($handle, $string) |
| 139 |
{ |
| 140 |
return \fwrite($handle, $string); |
| 141 |
} |
| 142 |
/** |
| 143 |
* Wrapper around global function fclose(). |
| 144 |
* |
| 145 |
* @see fclose() |
| 146 |
* |
| 147 |
* @param resource $handle |
| 148 |
* |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
public function fclose($handle) |
| 152 |
{ |
| 153 |
return \fclose($handle); |
| 154 |
} |
| 155 |
/** |
| 156 |
* Wrapper around global function rewind(). |
| 157 |
* |
| 158 |
* @see rewind() |
| 159 |
* |
| 160 |
* @param resource $handle |
| 161 |
* |
| 162 |
* @return bool |
| 163 |
*/ |
| 164 |
public function rewind($handle) |
| 165 |
{ |
| 166 |
return \rewind($handle); |
| 167 |
} |
| 168 |
/** |
| 169 |
* Wrapper around global function file_exists(). |
| 170 |
* |
| 171 |
* @see file_exists() |
| 172 |
* |
| 173 |
* @param string $fileName |
| 174 |
* |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
public function file_exists($fileName) |
| 178 |
{ |
| 179 |
return \file_exists($fileName); |
| 180 |
} |
| 181 |
/** |
| 182 |
* Wrapper around global function file_get_contents(). |
| 183 |
* |
| 184 |
* @see file_get_contents() |
| 185 |
* |
| 186 |
* @param string $filePath |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
public function file_get_contents($filePath) |
| 191 |
{ |
| 192 |
$realFilePath = $this->convertToUseRealPath($filePath); |
| 193 |
return \file_get_contents($realFilePath); |
| 194 |
} |
| 195 |
/** |
| 196 |
* Wrapper around global function feof(). |
| 197 |
* |
| 198 |
* @see feof() |
| 199 |
* |
| 200 |
* @param resource $handle |
| 201 |
* |
| 202 |
* @return bool |
| 203 |
*/ |
| 204 |
public function feof($handle) |
| 205 |
{ |
| 206 |
return \feof($handle); |
| 207 |
} |
| 208 |
/** |
| 209 |
* Wrapper around global function is_readable(). |
| 210 |
* |
| 211 |
* @see is_readable() |
| 212 |
* |
| 213 |
* @param string $fileName |
| 214 |
* |
| 215 |
* @return bool |
| 216 |
*/ |
| 217 |
public function is_readable($fileName) |
| 218 |
{ |
| 219 |
return \is_readable($fileName); |
| 220 |
} |
| 221 |
/** |
| 222 |
* Wrapper around global function basename(). |
| 223 |
* |
| 224 |
* @see basename() |
| 225 |
* |
| 226 |
* @param string $path |
| 227 |
* @param string $suffix |
| 228 |
* |
| 229 |
* @return string |
| 230 |
*/ |
| 231 |
public function basename($path, $suffix = '') |
| 232 |
{ |
| 233 |
return \basename($path, $suffix); |
| 234 |
} |
| 235 |
/** |
| 236 |
* Wrapper around global function header(). |
| 237 |
* |
| 238 |
* @see header() |
| 239 |
* |
| 240 |
* @param string $string |
| 241 |
*/ |
| 242 |
public function header($string) |
| 243 |
{ |
| 244 |
\header($string); |
| 245 |
} |
| 246 |
/** |
| 247 |
* Wrapper around global function ob_end_clean(). |
| 248 |
* |
| 249 |
* @see ob_end_clean() |
| 250 |
*/ |
| 251 |
public function ob_end_clean() |
| 252 |
{ |
| 253 |
if (\ob_get_length() > 0) { |
| 254 |
\ob_end_clean(); |
| 255 |
} |
| 256 |
} |
| 257 |
/** |
| 258 |
* Wrapper around global function iconv(). |
| 259 |
* |
| 260 |
* @see iconv() |
| 261 |
* |
| 262 |
* @param string $string The string to be converted |
| 263 |
* @param string $sourceEncoding The encoding of the source string |
| 264 |
* @param string $targetEncoding The encoding the source string should be converted to |
| 265 |
* |
| 266 |
* @return bool|string the converted string or FALSE on failure |
| 267 |
*/ |
| 268 |
public function iconv($string, $sourceEncoding, $targetEncoding) |
| 269 |
{ |
| 270 |
return \iconv($sourceEncoding, $targetEncoding, $string); |
| 271 |
} |
| 272 |
/** |
| 273 |
* Wrapper around global function mb_convert_encoding(). |
| 274 |
* |
| 275 |
* @see mb_convert_encoding() |
| 276 |
* |
| 277 |
* @param string $string The string to be converted |
| 278 |
* @param string $sourceEncoding The encoding of the source string |
| 279 |
* @param string $targetEncoding The encoding the source string should be converted to |
| 280 |
* |
| 281 |
* @return bool|string the converted string or FALSE on failure |
| 282 |
*/ |
| 283 |
public function mb_convert_encoding($string, $sourceEncoding, $targetEncoding) |
| 284 |
{ |
| 285 |
return \mb_convert_encoding($string, $targetEncoding, $sourceEncoding); |
| 286 |
} |
| 287 |
/** |
| 288 |
* Wrapper around global function stream_get_wrappers(). |
| 289 |
* |
| 290 |
* @see stream_get_wrappers() |
| 291 |
* |
| 292 |
* @return array |
| 293 |
*/ |
| 294 |
public function stream_get_wrappers() |
| 295 |
{ |
| 296 |
return \stream_get_wrappers(); |
| 297 |
} |
| 298 |
/** |
| 299 |
* Wrapper around global function function_exists(). |
| 300 |
* |
| 301 |
* @see function_exists() |
| 302 |
* |
| 303 |
* @param string $functionName |
| 304 |
* |
| 305 |
* @return bool |
| 306 |
*/ |
| 307 |
public function function_exists($functionName) |
| 308 |
{ |
| 309 |
return \function_exists($functionName); |
| 310 |
} |
| 311 |
/** |
| 312 |
* Updates the given file path to use a real path. |
| 313 |
* This is to avoid issues on some Windows setup. |
| 314 |
* |
| 315 |
* @param string $filePath File path |
| 316 |
* |
| 317 |
* @return string The file path using a real path |
| 318 |
*/ |
| 319 |
protected function convertToUseRealPath($filePath) |
| 320 |
{ |
| 321 |
$realFilePath = $filePath; |
| 322 |
if ($this->isZipStream($filePath)) { |
| 323 |
if (\preg_match('/zip:\\/\\/(.*)#(.*)/', $filePath, $matches)) { |
| 324 |
$documentPath = $matches[1]; |
| 325 |
$documentInsideZipPath = $matches[2]; |
| 326 |
$realFilePath = 'zip://' . \realpath($documentPath) . '#' . $documentInsideZipPath; |
| 327 |
} |
| 328 |
} else { |
| 329 |
$realFilePath = \realpath($filePath); |
| 330 |
} |
| 331 |
return $realFilePath; |
| 332 |
} |
| 333 |
/** |
| 334 |
* Returns whether the given path is a zip stream. |
| 335 |
* |
| 336 |
* @param string $path Path pointing to a document |
| 337 |
* |
| 338 |
* @return bool TRUE if path is a zip stream, FALSE otherwise |
| 339 |
*/ |
| 340 |
protected function isZipStream($path) |
| 341 |
{ |
| 342 |
return 0 === \strpos($path, 'zip://'); |
| 343 |
} |
| 344 |
} |
| 345 |
|