Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Created March 10, 2017 18:54
Show Gist options
  • Save danielbachhuber/411b746d9c2b56506c082148fb26a194 to your computer and use it in GitHub Desktop.
Save danielbachhuber/411b746d9c2b56506c082148fb26a194 to your computer and use it in GitHub Desktop.
<?php
global $start_time;
function log_message( $message ) {
global $start_time;
$elapsed_time = microtime( true ) - $start_time;
WP_CLI::log( sprintf( '[%s] %s', format_log_timestamp( $elapsed_time ), $message ) );
}
function format_log_timestamp( $s ) {
$h = floor( $s / 3600 );
$s -= $h * 3600;
$m = floor( $s / 60 );
$s -= $m * 60;
return $h . ':' . sprintf( '%02d', $m ) . ':' . sprintf( '%02d', $s );
}
function recurse_directory( $path, $type ) {
$ignored_paths = array(
// System directories
'/.ssh/',
'/.git/',
'/.svn/',
'/.subversion/',
'/__MACOSX/',
// Webserver directories
'/cache/',
'/logs/',
'/debuglogs/',
'/Maildir/',
'/tmp/',
// Generic application directories
'/uploads/',
'/themes/',
'/plugins/',
'/modules/',
// Dependency management
'/node_modules/',
'/bower_components/',
'/vendor/',
// Already in a WordPress install
'/wp-admin/',
'/wp-content/',
);
foreach( $ignored_paths as $ignored_path ) {
if ( false !== stripos( $path, $ignored_path ) ) {
log_message( "Matched ignored path. Skipping recursion into '{$path}'" );
return;
}
}
log_message( "Recusing into '{$path}'" );
if ( 'php' === $type ) {
$iterator = new RecursiveDirectoryIterator( $path, FilesystemIterator::SKIP_DOTS );
foreach( $iterator as $file_info ) {
if ( $file_info->isDir() ) {
recurse_directory( $file_info->getPathname(), $type );
}
}
} elseif ( 'find' === $type ) {
$directories = trim( shell_exec( 'find ' . escapeshellarg( $path ) . ' -mindepth 1 -maxdepth 1 -type d' ) );
if ( empty( $directories ) ) {
return;
}
foreach( explode( PHP_EOL, $directories ) as $dir ) {
recurse_directory( $dir, $type );
}
}
};
$start_time = microtime( true );
recurse_directory( realpath( $args[0] ), 'php' );
$end_time = microtime( true ) - $start_time;
log_message( 'PHP recursion complete' );
$start_time = microtime( true );
recurse_directory( realpath( $args[0] ), 'find' );
$end_time = microtime( true ) - $start_time;
log_message( 'find recursion complete' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment