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

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