PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.4
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.4
1.9.5 1.9.4 1.9.3 trunk 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 All 40 releases
code-profiler / lib / class-stream.php

class-stream.php in Code Profiler – WordPress Performance Profiling and Debugging Made Easy 1.4, at lib/class-stream.php

354 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 +=====================================================================+
4 | ____ _ ____ __ _ _ |
5 | / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ |
6 | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| |
7 | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | |
8 | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| |
9 | |
10 | (c) Jerome Bruandet ~ https://code-profiler.com/ |
11 +=====================================================================+
12 */
13
14 if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); }
15
16 // =====================================================================
17
18 class CodeProfiler_Stream {
19
20 static $io_stats = [
21 'cast' => 0, 'chgrp' => 0,
22 'chmod' => 0, 'chown' => 0,
23 'close' => 0, 'closedir' => 0,
24 'eof' => 0, 'flush' => 0,
25 'lock' => 0, 'mkdir' => 0,
26 'open' => 0, 'opendir' => 0,
27 'readdir' => 0, 'rewinddir' => 0,
28 'read' => 0, 'rename' => 0,
29 'rmdir' => 0, 'seek' => 0,
30 'set_option'=> 0, 'stat' => 0,
31 'tell' => 0, 'truncate' => 0,
32 'write' => 0, 'touch' => 0,
33 'unlink' => 0, 'url_stat' => 0
34 ];
35 static $io_read = 0;
36 static $io_write = 0;
37 static $strlen = 18;
38 public $context;
39 public $resource;
40 protected static $protocol = 'file';
41 private $script;
42
43
44 public static function start() {
45 stream_wrapper_unregister( self::$protocol );
46 stream_wrapper_register( self::$protocol, CodeProfiler_Stream::class );
47 }
48
49
50 public static function stop() {
51 stream_wrapper_restore( self::$protocol );
52 }
53
54 /*
55 * Opens file or URL
56 */
57 public function stream_open( $path, $mode, $options, &$opened_path ) {
58 self::$io_stats['open']++;
59 self::stop();
60 if ( isset( $this->context) ) {
61 $this->resource = fopen( $path, $mode, $options, $this->context );
62 } else {
63 $this->resource = fopen( $path, $mode, $options );
64 }
65 self::start();
66 $this->script = "{$path[-4]}{$path[-3]}{$path[-2]}{$path[-1]}" == '.php';
67 return $this->resource !== false;
68 }
69
70 /*
71 * Close a resource
72 */
73 public function stream_close() {
74 self::$io_stats['close']++;
75 return fclose( $this->resource );
76 }
77
78 /*
79 * Tests for end-of-file on a file pointer
80 */
81 public function stream_eof() {
82 self::$io_stats['eof']++;
83 return feof( $this->resource );
84 }
85
86 /*
87 * Flushes the output
88 */
89 public function stream_flush() {
90 self::$io_stats['flush']++;
91 return fflush( $this->resource );
92 }
93
94 /*
95 * Seeks to specific location in a stream
96 */
97 public function stream_seek( $offset, $whence = SEEK_SET ) {
98 self::$io_stats['seek']++;
99 return fseek( $this->resource, $offset, $whence ) === 0;
100 }
101
102 /*
103 * Delete a file
104 */
105 public function unlink( $path ) {
106 self::$io_stats['unlink']++;
107 self::stop();
108 if ( isset( $this->context ) ) {
109 $res = unlink( $path, $this->context );
110 } else {
111 $res = unlink( $path );
112 }
113 self::start();
114 return $res;
115 }
116
117 /*
118 * Renames a file or directory
119 */
120 public function rename( $path_from, $path_to ) {
121 self::$io_stats['rename']++;
122 self::stop();
123 if ( isset( $this->context ) ) {
124 $res = rename( $path_from, $path_to, $this->context );
125 } else {
126 $res = rename( $path_from, $path_to );
127 }
128 self::start();
129 return $res;
130 }
131
132 /*
133 * Removes a directory
134 */
135 public function rmdir( $path, $options ) {
136 self::$io_stats['rmdir']++;
137 self::stop();
138 if ( isset( $this->context ) ) {
139 $res = rmdir( $path, $this->context );
140 } else {
141 $res = rmdir( $path );
142 }
143 self::start();
144 return $res;
145 }
146
147 /*
148 * Retrieve the underlaying resource
149 */
150 public function stream_cast( $cast_as ) {
151 self::$io_stats['cast']++;
152 return $this->resource;
153 }
154
155 /*
156 * Advisory file locking
157 */
158 public function stream_lock( $operation ) {
159 self::$io_stats['lock']++;
160 if (! $operation ) {
161 $operation = LOCK_EX;
162 }
163 return flock( $this->resource, $operation );
164 }
165
166 /*
167 * Truncate stream
168 */
169 public function stream_truncate( $new_size ) {
170 self::$io_stats['truncate']++;
171 return ftruncate( $this->resource, $new_size );
172 }
173
174
175 public function stream_write( $data ) {
176 self::$io_stats['write']++;
177 $write = fwrite( $this->resource, $data );
178 self::$io_write += $write;
179 return $write;
180 }
181
182 /*
183 * Change stream options
184 */
185 public function stream_set_option( $option, $arg1, $arg2 ) {
186 self::$io_stats['set_option']++;
187 switch ( $option ) {
188 case STREAM_OPTION_BLOCKING:
189 return stream_set_blocking( $this->resource, $arg1 );
190 case STREAM_OPTION_READ_TIMEOUT:
191 return stream_set_timeout( $this->resource, $arg1, $arg2 );
192 case STREAM_OPTION_WRITE_BUFFER:
193 return stream_set_write_buffer( $this->resource, $arg1 );
194 case STREAM_OPTION_READ_BUFFER:
195 return stream_set_read_buffer( $this->resource, $arg1 );
196 default:
197 return false;
198 }
199 }
200
201 /*
202 * Change stream metadata
203 */
204 public function stream_metadata( $path, $option, $value ) {
205 self::stop();
206 $res = false;
207 switch ( $option ) {
208 case STREAM_META_ACCESS:
209 $res = chmod( $path, $value );
210 self::$io_stats['chmod']++;
211 break;
212 case STREAM_META_GROUP:
213 case STREAM_META_GROUP_NAME:
214 $res = chgrp( $path, $value );
215 self::$io_stats['chgrp']++;
216 break;
217 case STREAM_META_OWNER:
218 case STREAM_META_OWNER_NAME:
219 $res = chown( $path, $value );
220 self::$io_stats['chown']++;
221 break;
222 case STREAM_META_TOUCH:
223 if (! empty( $value ) ) {
224 $res = touch( $path, $value[0], $value[1] );
225 } else {
226 $res = touch( $path );
227 }
228 self::$io_stats['touch']++;
229 break;
230 }
231 self::start();
232 return $res;
233 }
234
235 /*
236 * Create a directory
237 */
238 public function mkdir( $path, $mode, $options ) {
239 self::$io_stats['mkdir']++;
240 self::stop();
241 if ( isset( $this->context ) ) {
242 $res = mkdir( $path, $mode, $options, $this->context );
243 } else {
244 $res = mkdir( $path, $mode, $options );
245 }
246 self::start();
247 return $res;
248 }
249
250 /*
251 * Open directory handle
252 */
253 public function dir_opendir( $path, $options ) {
254 self::$io_stats['opendir']++;
255 self::stop();
256 if ( isset( $this->context ) ) {
257 $this->resource = opendir( $path, $this->context );
258 } else {
259 $this->resource = opendir( $path );
260 }
261 self::start();
262 return $this->resource;
263 }
264
265 /*
266 * Close directory handle
267 */
268 public function dir_closedir() {
269 self::$io_stats['closedir']++;
270 // closedir returns no value
271 return closedir( $this->resource );
272 }
273
274 /*
275 * Read entry from directory handle
276 */
277 public function dir_readdir() {
278 self::$io_stats['readdir']++;
279 return readdir( $this->resource );
280 }
281
282 /*
283 * Rewind directory handle
284 */
285 public function dir_rewinddir() {
286 self::$io_stats['rewinddir']++;
287 return rewinddir( $this->resource );
288 }
289
290 /*
291 * Retrieve information about a file
292 */
293 public function url_stat( $path, $flags ) {
294 self::$io_stats['url_stat']++;
295 self::stop();
296 // Catch error and exception
297 set_error_handler( function() {} );
298 try {
299 $res = stat( $path );
300 } catch ( \Exception $e ) {
301 $res = null;
302 }
303 restore_error_handler();
304 self::start();
305 return $res;
306 }
307
308 /*
309 * Read from stream & enable ticks
310 */
311 public function stream_read( $count ) {
312 self::$io_stats['read']++;
313 self::stop();
314 if ( $this->script && ftell( $this->resource ) == 0 ) {
315 self::start();
316 $read = fread( $this->resource, $count - self::$strlen );
317 self::$io_read += strlen( $read );
318 return preg_replace( '/<\?php/i', '<?php declare(ticks=1);', $read, 1 );
319 }
320 self::start();
321 $read = fread( $this->resource, $count );
322 self::$io_read += strlen( $read );
323 return $read;
324 }
325
326 /*
327 * Retrieve information about a file resource
328 */
329 public function stream_stat() {
330 self::$io_stats['stat']++;
331 $res = fstat( $this->resource );
332 if ( $this->script ) {
333 $res['size'] += self::$strlen;
334 $res[7] += self::$strlen;
335 }
336 return $res;
337 }
338
339 /*
340 * Retrieve the current position of a stream
341 */
342 function stream_tell() {
343 self::$io_stats['tell']++;
344 $res = ftell( $this->resource );
345 if ( $this->script ) {
346 $res += self::$strlen;
347 }
348 return $res;
349 }
350 }
351
352 // =====================================================================
353 // EOF
354