|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class LibFsStream - stream utility functions |
|
9
|
|
|
* |
|
10
|
|
|
* @package Ktomk\Pipelines |
|
11
|
|
|
*/ |
|
12
|
|
|
class LibFsStream |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* similar to a readable file, a readable path allows stream-urls |
|
16
|
|
|
* as well, e.g. php://stdin or php://fd/0 but only for local |
|
17
|
|
|
* streams. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $path |
|
20
|
|
|
* |
|
21
|
|
|
* @return bool |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public static function isReadable($path) |
|
24
|
|
|
{ |
|
25
|
1 |
|
if (LibFs::isReadableFile($path)) { |
|
26
|
1 |
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
if (!is_string($path)) { |
|
|
|
|
|
|
30
|
1 |
|
return false; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
return LibFs::canFopen($path, 'rb'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $path |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public static function isUri($path) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$scheme = parse_url($path, PHP_URL_SCHEME); |
|
44
|
2 |
|
if (null === $scheme) { |
|
45
|
2 |
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
return in_array($scheme, stream_get_wrappers(), true); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* map a file path to a stream path (if applicable) |
|
53
|
|
|
* |
|
54
|
|
|
* some file paths aren't readable for PHP, e.g. "-" for |
|
55
|
|
|
* standard in or out or device files from process substitution. |
|
56
|
|
|
* |
|
57
|
|
|
* this method maps such paths to php:// stream uris that PHP can |
|
58
|
|
|
* process better (e.g. file_get_contents() works) |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $file |
|
61
|
|
|
* @param null|string $dashRepresent use NULL to not do any '-' processing |
|
62
|
|
|
* |
|
63
|
|
|
* @return string original $file if there was no mapping, php:// URI otherwise |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public static function mapFile($file, $dashRepresent = 'php://stdin') |
|
66
|
|
|
{ |
|
67
|
1 |
|
if (self::isUri($file)) { |
|
68
|
1 |
|
return $file; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
$path = $file; |
|
72
|
1 |
|
(null !== $dashRepresent) && ('-' === $file) && $path = $dashRepresent; |
|
73
|
|
|
|
|
74
|
|
|
/* @link https://bugs.php.net/bug.php?id=53465 */ |
|
75
|
1 |
|
$path = preg_replace('(^/(?:proc/self|dev)/(fd/\d+))', 'php://\1', $path); |
|
76
|
|
|
|
|
77
|
1 |
|
return stream_is_local($path) ? $path : $file; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|