-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-stream-class.php
248 lines (229 loc) · 7.06 KB
/
wp-stream-class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* WordPress Stream Helpers
*
* This class provies methods that are used by WordPress Wrappers.
*
* @package WP_Stream_Wrappers
* @author Jon Stacey <jon@jonsview.com>
* @copyright 2010 Jon Stacey
* @license http://wordpress.org/about/gpl/
* @link http://github.com/jmstacey/wp-stream-wrappers
* @version 1.0.0
* @since 1.0.0
*/
/**
* WordPress stream helper methods
*
* This class provides useful helper methods for streams.
* A stream is referenced as scheme://target. The methods contained within
* this class are static and can be used without instantiation.
*
* @package WP_Stream_Wrappers
* @author Jon Stacey <jon@jonsview.com>
* @version 1.0.0
* @link http://www.php.net/manual/en/intro.stream.php
* @since 1.0.0
*/
class WP_Stream {
/**
* Returns the scheme of a stream
*
* A stream is referenced as "scheme://target".
*
* Example usage of this static method:
* <code>
* $uri = "local://example.txt"
* $ret = WP_Stream::scheme($uri);
* // $ret is "local"
* </code>
*
* @param string $uri
* the stream URI referenced as "scheme://target".
* @return mixed
* the name of the scheme, or false if there is no scheme.
*
* @access public
* @static
* @since 1.0.0
*/
public static function scheme($uri) {
$components = explode('://', $uri, 2);
return count($components) == 2 ? $components[0] : false;
}
/**
* Returns the target of a stream
*
* A stream is referenced as "scheme://target".
*
* Example usage of this static method:
* <code>
* $uri = "local://foobar/example.txt"
* $ret = WP_Stream::target($uri);
* // $ret is "foobar/example.txt"
* </code>
*
* @param string $uri
* the stream URI referenced as "scheme://target".
* @return string
* the target (a.k.a. path) of the stream. An empty string ('') is
* returned if the stream does not have an explicit target, such as
* in the case of "scheme://"
*
* @access public
* @static
* @since 1.0.0
*/
public static function target($uri) {
list($scheme, $target) = explode('://', $uri, 2);
// Remove unnecessary leading and traling slashes.
return trim($target, '\/');
}
/**
* Returns a new instance of the wrapper responsible for given stream URI
*
* Example use:
* <code>
* // Option 1 - in this case the wrapper instance is initialized with the
* // given full URI "local://example.txt"
* $uri = "local://example.txt"
* $instance = WP_Stream::wrapper_instance($uri);
*
* // Option 2 - in this case only the scheme (in proper stream reference
* // form), so the wrapper instance is initialized without a
* // target "local://".
* $scheme = "local"
* $instance = WP_Stream:wrapper_instance($scheme);
* </code>
* In both options in the above example, an instance of
* WP_Local_Stream_Wrapper is returned.
*
* Note: Even if a scheme is used, or a URI without a target, the instance
* URI will be initialized as a full stream. That is, the instance URI
* will be "scheme://", which has both a scheme and target despite target
* being empty.
*
* @param string $uri
* the stream URI. For example, both "local://example.txt" or
* "local://" are acceptable.
* @return object
* a new stream wrapper instance for the given URI. Returns false if
* a registered wrapper cannot be found.
*
* @access public
* @static
* @since 1.0.0
*/
public static function new_wrapper_instance($uri) {
$scheme = WP_Stream::scheme($uri);
$class_name = WP_Stream::wrapper_class_name($scheme);
if (class_exists($class_name)) {
$instance = new $class_name();
$instance->set_uri($uri);
return $instance;
} else {
return false;
}
}
/**
* Returns ths class name of the wrapper implementation for given scheme
*
* @param string $scheme
* the stream scheme.
* @return string
* the class name of the registered wrapper handler, or false if
* there is no registered handler.
*
* @access public
* @static
* @since 1.0.0
*/
public static function wrapper_class_name($scheme) {
$wrappers = WP_Stream_Wrapper_Registry::get_stream_wrappers();
return empty($wrappers[$scheme]) ? false : $wrappers[$scheme]['class'];
}
/**
* Checks the validity of a given stream wrapper scheme
*
* Confirms that there is a registered stream handler for the given
* scheme and that it is callable. This is useful if you want to confirm
* a valid scheme without creating a new instance of the registered
* wrapper.
*
* @param string $scheme
* the stream scheme.
* @return bool
* true if the scheme is valid, or false if the scheme does not have
* a registered handler.
*
* @access public
* @static
* @since 1.0.0
*/
public static function scheme_valid($scheme) {
$class_name = WP_Stream::wrapper_class_name($scheme);
if (class_exists($class_name)) {
return true;
} else {
return false;
}
}
/**
* Normalizes a stream URI by making it syntactically correct
*
* The following actions are performed on the stream URI that is
* returned.
*
* - Removing leading slashes from target.
* - Removing duplicate path separators from target.
*
* @param string $uri
* the stream URI to normalize.
* @return string
* the normalized stream URI after the modifications listed in the
* function description have been performed.
*
* @access public
* @static
* @since 1.0.0
*/
public static function normalize($uri) {
$scheme = WP_Stream::scheme($uri);
if ($scheme && WP_Stream::scheme_valid($scheme)) {
$target = WP_Stream::target($uri);
if ($target !== false) {
$target = self::_clean_path_components($target);
$uri = $scheme . '://' . $target;
}
}
return $uri;
}
/**
* Removes superfluous separators from given path
*
* For example, and input string of:
* "dir1/dir2//dir3/dir4"
* would return "dir1/dir2/dir3/dir4"
*
* @param string $path
* the path to remove superfluous separators from.
* @return string
* the cleaned path.
*
* @access private
* @static
* @see WP_Stream::normalize()
* @since 1.0.0
*/
private static function _clean_path_components($path) {
$components = explode('/', $path);
$path = '';
foreach ($components as $c) {
if (strlen($c) > 0) {
$path .= '/' . $c;
}
}
return ltrim($path, '/');
}
}
?>