<?php
function wp_is_stream( $path ) {
if ( false === strpos( $path, '://' ) ) {
// $path isn't a stream
return false;
}
$wrappers = stream_get_wrappers();
$wrappers = array_map( 'preg_quote', $wrappers );
$wrappers_re = '(' . join( '|', $wrappers ) . ')';
return preg_match( "!^$wrappers_re://!", $path ) === 1;
}
function wp_is_stream2( $path ) {
$scheme_separator = strpos( $path, '://' );
if ( false === $scheme_separator ) {
// $path isn't a stream
return false;
}
$stream = substr( $path, 0, $scheme_separator );
return in_array( $stream, stream_get_wrappers(), true );
}
print( "Testing:\n" );
$tests = [
'test',
'php://test',
'http://test',
'unknown://test',
'://',
'unknown://php://test',
];
foreach ( $tests as $test ) {
printf(
"%-20s - wp_is_stream() => %-5s - wp_is_stream2() => %s\n",
$test,
wp_is_stream( $test ) ? 'true' : 'false',
wp_is_stream2( $test ) ? 'true' : 'false'
);
}
$test_stream = 'php://test/stream';
$iterations = 1000000;
$wp_start = microtime( true );
for( $i = 0; $i < $iterations; $i++ ) {
$result = wp_is_stream( $test_stream );
}
$wp_end = microtime( true );
$wp_start2 = microtime( true );
for( $i = 0; $i < $iterations; $i++ ) {
$result = wp_is_stream2( $test_stream );
}
$wp_end2 = microtime( true );
$wp_time = $wp_end - $wp_start;
$wp_time2 = $wp_end2 - $wp_start2;
print( "\nBenchmarking:\n" );
printf(
"wp_is_stream() => %f seconds for %d iterations\n" .
"wp_is_stream2() => %f seconds for %d iterations\n",
$wp_time,
$iterations,
$wp_time2,
$iterations
);
printf(
"Refactor improved execution time by %d percent.\n",
( $wp_time - $wp_time2 ) / $wp_time * 100
);
Abusive script
This script was stopped while abusing our resources
preferences:
55.74 ms | 413 KiB | 5 Q