PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / 1.6.7
Code Profiler – WordPress Performance Profiling and Debugging Made Easy v1.6.7
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.6.7, at lib/class-stream.php

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