mirror of
https://hk.gh-proxy.com/https://github.com/wp-cli/profile-command.git
synced 2025-08-18 06:11:48 +08:00
Merge pull request #94 from runcommand/abstract-profile-class
Abstract profiler to a dedicated class
This commit is contained in:
commit
dc970ab382
2 changed files with 387 additions and 345 deletions
|
@ -7,17 +7,6 @@ use WP_CLI\Utils;
|
|||
|
||||
class Command {
|
||||
|
||||
private $loggers = array();
|
||||
private $focus_stage = null;
|
||||
private $stage_hooks = array();
|
||||
private $focus_hook = null;
|
||||
private $previous_filter = null;
|
||||
private $previous_filter_callbacks = null;
|
||||
private $filter_depth = 0;
|
||||
private $focus_query_offset = 0;
|
||||
|
||||
private static $exception_message = "Need to bail, because can't restore the hooks";
|
||||
|
||||
/**
|
||||
* Profile each stage of the WordPress load process (bootstrap, main_query, template).
|
||||
*
|
||||
|
@ -51,16 +40,17 @@ class Command {
|
|||
public function stage( $args, $assoc_args ) {
|
||||
global $wpdb;
|
||||
|
||||
$this->focus_stage = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );
|
||||
$focus_stage = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );
|
||||
|
||||
$valid_stages = array( 'bootstrap', 'main_query', 'template' );
|
||||
if ( $this->focus_stage && ( true !== $this->focus_stage && ! in_array( $this->focus_stage, $valid_stages, true ) ) ) {
|
||||
if ( $focus_stage && ( true !== $focus_stage && ! in_array( $focus_stage, $valid_stages, true ) ) ) {
|
||||
WP_CLI::error( 'Invalid stage. Must be one of ' . implode( ', ', $valid_stages ) . ', or use --all.' );
|
||||
}
|
||||
|
||||
$this->run_profiler();
|
||||
$profiler = new Profiler( 'stage', $focus_stage );
|
||||
$profiler->run();
|
||||
|
||||
if ( $this->focus_stage ) {
|
||||
if ( $focus_stage ) {
|
||||
$fields = array(
|
||||
'hook',
|
||||
'callback_count',
|
||||
|
@ -89,7 +79,7 @@ class Command {
|
|||
);
|
||||
}
|
||||
$formatter = new Formatter( $assoc_args, $fields );
|
||||
$formatter->display_items( $this->loggers );
|
||||
$formatter->display_items( $profiler->get_loggers() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,12 +111,14 @@ class Command {
|
|||
*/
|
||||
public function hook( $args, $assoc_args ) {
|
||||
|
||||
$this->focus_hook = $args[0];
|
||||
$this->run_profiler();
|
||||
$focus = $args[0];
|
||||
|
||||
$profiler = new Profiler( 'hook', $focus );
|
||||
$profiler->run();
|
||||
|
||||
// 'shutdown' won't actually fire until script completion
|
||||
// but we can mock it
|
||||
if ( 'shutdown' === $this->focus_hook ) {
|
||||
if ( 'shutdown' === $focus ) {
|
||||
do_action( 'shutdown' );
|
||||
remove_all_actions( 'shutdown' );
|
||||
}
|
||||
|
@ -144,7 +136,7 @@ class Command {
|
|||
'request_count',
|
||||
);
|
||||
$formatter = new Formatter( $assoc_args, $fields );
|
||||
$formatter->display_items( $this->loggers );
|
||||
$formatter->display_items( $profiler->get_loggers() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -178,7 +170,8 @@ class Command {
|
|||
*/
|
||||
public function eval_( $args, $assoc_args ) {
|
||||
|
||||
$this->run_profiler();
|
||||
$profiler = new Profiler( false, false );
|
||||
$profiler->run();
|
||||
|
||||
$logger = new Logger();
|
||||
$logger->start();
|
||||
|
@ -235,7 +228,8 @@ class Command {
|
|||
WP_CLI::error( "'$file' does not exist." );
|
||||
}
|
||||
|
||||
$this->run_profiler();
|
||||
$profiler = new Profiler( false, false );
|
||||
$profiler->run();
|
||||
|
||||
$logger = new Logger();
|
||||
$logger->start();
|
||||
|
@ -256,329 +250,6 @@ class Command {
|
|||
$formatter->display_items( array( $logger ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Profiling verbosity at the beginning of every action and filter
|
||||
*/
|
||||
public function wp_hook_begin() {
|
||||
global $wpdb, $wp_filter;
|
||||
|
||||
foreach( Logger::$active_loggers as $logger ) {
|
||||
$logger->start_hook_timer();
|
||||
}
|
||||
|
||||
$current_filter = current_filter();
|
||||
if ( in_array( $current_filter, $this->stage_hooks ) ) {
|
||||
$pseudo_hook = "before {$current_filter}";
|
||||
if ( isset( $this->loggers[ $pseudo_hook ] ) ) {
|
||||
$this->loggers[ $pseudo_hook ]->stop();
|
||||
}
|
||||
$callback_count = 0;
|
||||
if ( isset( $wp_filter[ $current_filter ] ) && is_a( $wp_filter[ $current_filter ], 'WP_Hook' ) ) {
|
||||
if ( is_array( $wp_filter[ $current_filter ]->callbacks ) ) {
|
||||
foreach( $wp_filter[ $current_filter ]->callbacks as $priority => $callbacks ) {
|
||||
$callback_count += count( $callbacks );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ( isset( $wp_filter[ $current_filter ] ) && is_array( $wp_filter[ $current_filter ] ) ) {
|
||||
foreach( $wp_filter[ $current_filter ] as $priority => $callbacks ) {
|
||||
$callback_count += count( $callbacks );
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->loggers[ $current_filter ] = new Logger( array( 'hook' => $current_filter, 'callback_count' => $callback_count ) );
|
||||
$this->loggers[ $current_filter ]->start();
|
||||
}
|
||||
|
||||
if ( ! is_null( $this->previous_filter_callbacks ) && 0 === $this->filter_depth ) {
|
||||
if ( is_a( $wp_filter[ $this->previous_filter ], 'WP_Hook' ) ) {
|
||||
$wp_filter[ $this->previous_filter ]->callbacks = $this->previous_filter_callbacks;
|
||||
} else {
|
||||
$wp_filter[ $this->previous_filter ] = $this->previous_filter_callbacks;
|
||||
}
|
||||
$this->previous_filter_callbacks = null;
|
||||
}
|
||||
|
||||
if ( $this->focus_hook && $current_filter === $this->focus_hook && 0 === $this->filter_depth ) {
|
||||
$this->wrap_current_filter_callbacks( $current_filter );
|
||||
$this->filter_depth = 1;
|
||||
}
|
||||
|
||||
if ( 'shutdown' !== $this->focus_hook ) {
|
||||
WP_CLI::add_wp_hook( $current_filter, array( $this, 'wp_hook_end' ), 9999 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap current filter callbacks with a timer
|
||||
*/
|
||||
private function wrap_current_filter_callbacks( $current_filter ) {
|
||||
global $wp_filter;
|
||||
|
||||
if ( ! isset( $wp_filter[ $current_filter ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->previous_filter = $current_filter;
|
||||
if ( is_a( $wp_filter[ $current_filter ], 'WP_Hook' ) ) {
|
||||
$callbacks = $this->previous_filter_callbacks = $wp_filter[ $current_filter ]->callbacks;
|
||||
} else {
|
||||
$callbacks = $this->previous_filter_callbacks = $wp_filter[ $current_filter ];
|
||||
}
|
||||
|
||||
if ( ! is_array( $callbacks ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach( $callbacks as $priority => $priority_callbacks ) {
|
||||
foreach( $priority_callbacks as $i => $the_ ) {
|
||||
$callbacks[ $priority ][ $i ] = array(
|
||||
'function' => function() use( $the_, $i ) {
|
||||
if ( ! isset( $this->loggers[ $i ] ) ) {
|
||||
list( $callback, $location ) = self::get_name_location_from_callback( $the_['function'] );
|
||||
$definition = array(
|
||||
'callback' => $callback,
|
||||
'location' => $location,
|
||||
);
|
||||
$this->loggers[ $i ] = new Logger( $definition );
|
||||
}
|
||||
$this->loggers[ $i ]->start();
|
||||
$value = call_user_func_array( $the_['function'], func_get_args() );
|
||||
$this->loggers[ $i ]->stop();
|
||||
return $value;
|
||||
},
|
||||
'accepted_args' => $the_['accepted_args'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_a( $wp_filter[ $current_filter ], 'WP_Hook' ) ) {
|
||||
$wp_filter[ $current_filter ]->callbacks = $callbacks;
|
||||
} else {
|
||||
$wp_filter[ $current_filter ] = $callbacks;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Profiling verbosity at the end of every action and filter
|
||||
*/
|
||||
public function wp_hook_end( $filter_value = null ) {
|
||||
global $wpdb, $wp_filter;
|
||||
|
||||
foreach( Logger::$active_loggers as $logger ) {
|
||||
$logger->stop_hook_timer();
|
||||
}
|
||||
|
||||
$current_filter = current_filter();
|
||||
if ( $this->focus_hook && $current_filter === $this->focus_hook ) {
|
||||
$this->filter_depth = 0;
|
||||
}
|
||||
|
||||
if ( in_array( $current_filter, $this->stage_hooks ) ) {
|
||||
$this->loggers[ $current_filter ]->stop();
|
||||
$key = array_search( $current_filter, $this->stage_hooks );
|
||||
if ( false !== $key && isset( $this->stage_hooks[$key+1] ) ) {
|
||||
$pseudo_hook = "before {$this->stage_hooks[$key+1]}";
|
||||
$this->loggers[ $pseudo_hook ] = new Logger( array( 'hook' => '' ) );
|
||||
$this->loggers[ $pseudo_hook ]->start();
|
||||
} else {
|
||||
$pseudo_hook = 'wp_profile_last_hook';
|
||||
$this->loggers[ $pseudo_hook ] = new Logger( array( 'hook' => '' ) );
|
||||
$this->loggers[ $pseudo_hook ]->start();
|
||||
}
|
||||
}
|
||||
|
||||
return $filter_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Profiling request time for any active Loggers
|
||||
*/
|
||||
public function wp_request_begin( $filter_value = null ) {
|
||||
foreach( Logger::$active_loggers as $logger ) {
|
||||
$logger->start_request_timer();
|
||||
}
|
||||
return $filter_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Profiling request time for any active Loggers
|
||||
*/
|
||||
public function wp_request_end( $filter_value = null ) {
|
||||
foreach( Logger::$active_loggers as $logger ) {
|
||||
$logger->stop_request_timer();
|
||||
}
|
||||
return $filter_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the profiler against WordPress
|
||||
*/
|
||||
private function run_profiler() {
|
||||
WP_CLI::add_wp_hook( 'muplugins_loaded', function(){
|
||||
if ( $url = WP_CLI::get_runner()->config['url'] ) {
|
||||
WP_CLI::set_url( trailingslashit( $url ) );
|
||||
} else {
|
||||
WP_CLI::set_url( home_url( '/' ) );
|
||||
}
|
||||
});
|
||||
WP_CLI::add_hook( 'after_wp_config_load', function() {
|
||||
if ( defined( 'SAVEQUERIES' ) && ! SAVEQUERIES ) {
|
||||
WP_CLI::error( "'SAVEQUERIES' is defined as false, and must be true. Please check your wp-config.php" );
|
||||
}
|
||||
if ( ! defined( 'SAVEQUERIES' ) ) {
|
||||
define( 'SAVEQUERIES', true );
|
||||
}
|
||||
});
|
||||
WP_CLI::add_wp_hook( 'all', array( $this, 'wp_hook_begin' ) );
|
||||
WP_CLI::add_wp_hook( 'pre_http_request', array( $this, 'wp_request_begin' ) );
|
||||
WP_CLI::add_wp_hook( 'http_api_debug', array( $this, 'wp_request_end' ) );
|
||||
$this->load_wordpress_with_template();
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs through the entirety of the WP bootstrap process
|
||||
*/
|
||||
private function load_wordpress_with_template() {
|
||||
global $wp_query;
|
||||
|
||||
$stage_hooks = array(
|
||||
'bootstrap' => array(
|
||||
'muplugins_loaded',
|
||||
'plugins_loaded',
|
||||
'setup_theme',
|
||||
'after_setup_theme',
|
||||
'init',
|
||||
'wp_loaded',
|
||||
),
|
||||
'main_query' => array(
|
||||
'parse_request',
|
||||
'send_headers',
|
||||
'pre_get_posts',
|
||||
'the_posts',
|
||||
'wp',
|
||||
),
|
||||
'template' => array(
|
||||
'template_redirect',
|
||||
'template_include',
|
||||
'wp_head',
|
||||
'loop_start',
|
||||
'loop_end',
|
||||
'wp_footer',
|
||||
),
|
||||
);
|
||||
if ( true === $this->focus_stage ) {
|
||||
$hooks = array();
|
||||
foreach( $stage_hooks as $stage_hook ) {
|
||||
$hooks = array_merge( $hooks, $stage_hook );
|
||||
}
|
||||
$this->set_stage_hooks( $hooks );
|
||||
}
|
||||
|
||||
if ( 'bootstrap' === $this->focus_stage ) {
|
||||
$this->set_stage_hooks( $stage_hooks['bootstrap'] );
|
||||
} else if ( ! $this->focus_stage && ! $this->focus_hook ) {
|
||||
$logger = new Logger( array( 'stage' => 'bootstrap' ) );
|
||||
$logger->start();
|
||||
}
|
||||
WP_CLI::get_runner()->load_wordpress();
|
||||
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
|
||||
$this->loggers['wp_profile_last_hook']->stop();
|
||||
}
|
||||
if ( ! $this->focus_stage && ! $this->focus_hook ) {
|
||||
$logger->stop();
|
||||
$this->loggers[] = $logger;
|
||||
}
|
||||
|
||||
// Set up main_query main WordPress query.
|
||||
if ( 'main_query' === $this->focus_stage ) {
|
||||
$this->set_stage_hooks( $stage_hooks['main_query'] );
|
||||
} else if ( ! $this->focus_stage && ! $this->focus_hook ) {
|
||||
$logger = new Logger( array( 'stage' => 'main_query' ) );
|
||||
$logger->start();
|
||||
}
|
||||
wp();
|
||||
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
|
||||
$this->loggers['wp_profile_last_hook']->stop();
|
||||
}
|
||||
if ( ! $this->focus_stage && ! $this->focus_hook ) {
|
||||
$logger->stop();
|
||||
$this->loggers[] = $logger;
|
||||
}
|
||||
|
||||
define( 'WP_USE_THEMES', true );
|
||||
|
||||
// Template is normally loaded in global stage, so we need to replicate
|
||||
foreach( $GLOBALS as $key => $value ) {
|
||||
global $$key;
|
||||
}
|
||||
|
||||
// Load the theme template.
|
||||
if ( 'template' === $this->focus_stage ) {
|
||||
$this->set_stage_hooks( $stage_hooks['template'] );
|
||||
} else if ( ! $this->focus_stage && ! $this->focus_hook ) {
|
||||
$logger = new Logger( array( 'stage' => 'template' ) );
|
||||
$logger->start();
|
||||
}
|
||||
ob_start();
|
||||
require_once( ABSPATH . WPINC . '/template-loader.php' );
|
||||
ob_get_clean();
|
||||
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
|
||||
$this->loggers['wp_profile_last_hook']->stop();
|
||||
}
|
||||
if ( ! $this->focus_stage && ! $this->focus_hook ) {
|
||||
$logger->stop();
|
||||
$this->loggers[] = $logger;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a human-readable name from a callback
|
||||
*/
|
||||
private static function get_name_location_from_callback( $callback ) {
|
||||
$name = $location = '';
|
||||
$reflection = false;
|
||||
if ( is_array( $callback ) && is_object( $callback[0] ) ) {
|
||||
$reflection = new \ReflectionMethod( $callback[0], $callback[1] );
|
||||
$name = get_class( $callback[0] ) . '->' . $callback[1] . '()';
|
||||
} elseif ( is_array( $callback ) && method_exists( $callback[0], $callback[1] ) ) {
|
||||
$reflection = new \ReflectionMethod( $callback[0], $callback[1] );
|
||||
$name = $callback[0] . '::' . $callback[1] . '()';
|
||||
} elseif ( is_object( $callback ) && is_a( $callback, 'Closure' ) ) {
|
||||
$reflection = new \ReflectionFunction( $callback );
|
||||
$name = 'function(){}';
|
||||
} else if ( is_string( $callback ) ) {
|
||||
$reflection = new \ReflectionFunction( $callback );
|
||||
$name = $callback . '()';
|
||||
}
|
||||
if ( $reflection ) {
|
||||
$location = $reflection->getFileName() . ':' . $reflection->getStartLine();
|
||||
if ( 0 === stripos( $location, WP_PLUGIN_DIR ) ) {
|
||||
$location = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $location );
|
||||
} else if ( 0 === stripos( $location, WPMU_PLUGIN_DIR ) ) {
|
||||
$location = str_replace( trailingslashit( dirname( WPMU_PLUGIN_DIR ) ), '', $location );
|
||||
} else if ( 0 === stripos( $location, get_theme_root() ) ) {
|
||||
$location = str_replace( trailingslashit( get_theme_root() ), '', $location );
|
||||
} else if ( 0 === stripos( $location, ABSPATH . 'wp-admin/' ) ) {
|
||||
$location = str_replace( ABSPATH, '', $location );
|
||||
} else if ( 0 === stripos( $location, ABSPATH . 'wp-includes/' ) ) {
|
||||
$location = str_replace( ABSPATH, '', $location );
|
||||
}
|
||||
}
|
||||
return array( $name, $location );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hooks for the current stage
|
||||
*/
|
||||
private function set_stage_hooks( $hooks ) {
|
||||
$this->stage_hooks = $hooks;
|
||||
$pseudo_hook = "before {$hooks[0]}";
|
||||
$this->loggers[ $pseudo_hook ] = new Logger( array( 'hook' => '' ) );
|
||||
$this->loggers[ $pseudo_hook ]->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Include a file without exposing it to current scope
|
||||
*
|
||||
|
|
371
inc/class-profiler.php
Normal file
371
inc/class-profiler.php
Normal file
|
@ -0,0 +1,371 @@
|
|||
<?php
|
||||
|
||||
namespace runcommand\Profile;
|
||||
|
||||
use WP_CLI;
|
||||
|
||||
class Profiler {
|
||||
|
||||
private $type;
|
||||
private $focus;
|
||||
private $loggers = array();
|
||||
private $stage_hooks = array(
|
||||
'bootstrap' => array(
|
||||
'muplugins_loaded',
|
||||
'plugins_loaded',
|
||||
'setup_theme',
|
||||
'after_setup_theme',
|
||||
'init',
|
||||
'wp_loaded',
|
||||
),
|
||||
'main_query' => array(
|
||||
'parse_request',
|
||||
'send_headers',
|
||||
'pre_get_posts',
|
||||
'the_posts',
|
||||
'wp',
|
||||
),
|
||||
'template' => array(
|
||||
'template_redirect',
|
||||
'template_include',
|
||||
'wp_head',
|
||||
'loop_start',
|
||||
'loop_end',
|
||||
'wp_footer',
|
||||
),
|
||||
);
|
||||
private $current_stage_hooks = array();
|
||||
private $previous_filter = null;
|
||||
private $previous_filter_callbacks = null;
|
||||
private $filter_depth = 0;
|
||||
|
||||
public function __construct( $type, $focus ) {
|
||||
$this->type = $type;
|
||||
$this->focus = $focus;
|
||||
}
|
||||
|
||||
public function get_loggers() {
|
||||
return $this->loggers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the profiler against WordPress
|
||||
*/
|
||||
public function run() {
|
||||
WP_CLI::add_wp_hook( 'muplugins_loaded', function(){
|
||||
if ( $url = WP_CLI::get_runner()->config['url'] ) {
|
||||
WP_CLI::set_url( trailingslashit( $url ) );
|
||||
} else {
|
||||
WP_CLI::set_url( home_url( '/' ) );
|
||||
}
|
||||
});
|
||||
WP_CLI::add_hook( 'after_wp_config_load', function() {
|
||||
if ( defined( 'SAVEQUERIES' ) && ! SAVEQUERIES ) {
|
||||
WP_CLI::error( "'SAVEQUERIES' is defined as false, and must be true. Please check your wp-config.php" );
|
||||
}
|
||||
if ( ! defined( 'SAVEQUERIES' ) ) {
|
||||
define( 'SAVEQUERIES', true );
|
||||
}
|
||||
});
|
||||
WP_CLI::add_wp_hook( 'all', array( $this, 'wp_hook_begin' ) );
|
||||
WP_CLI::add_wp_hook( 'pre_http_request', array( $this, 'wp_request_begin' ) );
|
||||
WP_CLI::add_wp_hook( 'http_api_debug', array( $this, 'wp_request_end' ) );
|
||||
$this->load_wordpress_with_template();
|
||||
}
|
||||
|
||||
/**
|
||||
* Profiling verbosity at the beginning of every action and filter
|
||||
*/
|
||||
public function wp_hook_begin() {
|
||||
|
||||
foreach( Logger::$active_loggers as $logger ) {
|
||||
$logger->start_hook_timer();
|
||||
}
|
||||
|
||||
$current_filter = current_filter();
|
||||
if ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks ) ) {
|
||||
$pseudo_hook = "before {$current_filter}";
|
||||
if ( isset( $this->loggers[ $pseudo_hook ] ) ) {
|
||||
$this->loggers[ $pseudo_hook ]->stop();
|
||||
}
|
||||
$callback_count = 0;
|
||||
$callbacks = self::get_filter_callbacks( $current_filter );
|
||||
if ( false !== $callbacks ) {
|
||||
foreach( $callbacks as $priority => $cbs ) {
|
||||
$callback_count += count( $cbs );
|
||||
}
|
||||
}
|
||||
$this->loggers[ $current_filter ] = new Logger( array( 'hook' => $current_filter, 'callback_count' => $callback_count ) );
|
||||
$this->loggers[ $current_filter ]->start();
|
||||
}
|
||||
|
||||
if ( ! is_null( $this->previous_filter_callbacks ) && 0 === $this->filter_depth ) {
|
||||
self::set_filter_callbacks( $this->previous_filter, $this->previous_filter_callbacks );
|
||||
$this->previous_filter_callbacks = null;
|
||||
}
|
||||
|
||||
if ( 'hook' === $this->type && $current_filter === $this->focus && 0 === $this->filter_depth ) {
|
||||
$this->wrap_current_filter_callbacks( $current_filter );
|
||||
$this->filter_depth = 1;
|
||||
}
|
||||
|
||||
if ( ! ( 'hook' === $this->type && 'shutdown' === $this->focus ) ) {
|
||||
WP_CLI::add_wp_hook( $current_filter, array( $this, 'wp_hook_end' ), 9999 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap current filter callbacks with a timer
|
||||
*/
|
||||
private function wrap_current_filter_callbacks( $current_filter ) {
|
||||
|
||||
$callbacks = self::get_filter_callbacks( $current_filter );
|
||||
if ( false === $callbacks ) {
|
||||
return;
|
||||
}
|
||||
$this->previous_filter = $current_filter;
|
||||
$this->previous_filter_callbacks = $callbacks;
|
||||
|
||||
foreach( $callbacks as $priority => $priority_callbacks ) {
|
||||
foreach( $priority_callbacks as $i => $the_ ) {
|
||||
$callbacks[ $priority ][ $i ] = array(
|
||||
'function' => function() use( $the_, $i ) {
|
||||
if ( ! isset( $this->loggers[ $i ] ) ) {
|
||||
list( $callback, $location ) = self::get_name_location_from_callback( $the_['function'] );
|
||||
$definition = array(
|
||||
'callback' => $callback,
|
||||
'location' => $location,
|
||||
);
|
||||
$this->loggers[ $i ] = new Logger( $definition );
|
||||
}
|
||||
$this->loggers[ $i ]->start();
|
||||
$value = call_user_func_array( $the_['function'], func_get_args() );
|
||||
$this->loggers[ $i ]->stop();
|
||||
return $value;
|
||||
},
|
||||
'accepted_args' => $the_['accepted_args'],
|
||||
);
|
||||
}
|
||||
}
|
||||
self::set_filter_callbacks( $current_filter, $callbacks );
|
||||
}
|
||||
|
||||
/**
|
||||
* Profiling verbosity at the end of every action and filter
|
||||
*/
|
||||
public function wp_hook_end( $filter_value = null ) {
|
||||
|
||||
foreach( Logger::$active_loggers as $logger ) {
|
||||
$logger->stop_hook_timer();
|
||||
}
|
||||
|
||||
$current_filter = current_filter();
|
||||
if ( 'hook' === $this->type && $current_filter === $this->focus ) {
|
||||
$this->filter_depth = 0;
|
||||
}
|
||||
|
||||
if ( 'stage' === $this->type && in_array( $current_filter, $this->current_stage_hooks ) ) {
|
||||
$this->loggers[ $current_filter ]->stop();
|
||||
$key = array_search( $current_filter, $this->current_stage_hooks );
|
||||
if ( false !== $key && isset( $this->current_stage_hooks[ $key + 1 ] ) ) {
|
||||
$pseudo_hook = "before {$this->current_stage_hooks[$key+1]}";
|
||||
$this->loggers[ $pseudo_hook ] = new Logger( array( 'hook' => '' ) );
|
||||
$this->loggers[ $pseudo_hook ]->start();
|
||||
} else {
|
||||
$pseudo_hook = 'wp_profile_last_hook';
|
||||
$this->loggers[ $pseudo_hook ] = new Logger( array( 'hook' => '' ) );
|
||||
$this->loggers[ $pseudo_hook ]->start();
|
||||
}
|
||||
}
|
||||
|
||||
return $filter_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Profiling request time for any active Loggers
|
||||
*/
|
||||
public function wp_request_begin( $filter_value = null ) {
|
||||
foreach( Logger::$active_loggers as $logger ) {
|
||||
$logger->start_request_timer();
|
||||
}
|
||||
return $filter_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Profiling request time for any active Loggers
|
||||
*/
|
||||
public function wp_request_end( $filter_value = null ) {
|
||||
foreach( Logger::$active_loggers as $logger ) {
|
||||
$logger->stop_request_timer();
|
||||
}
|
||||
return $filter_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs through the entirety of the WP bootstrap process
|
||||
*/
|
||||
private function load_wordpress_with_template() {
|
||||
|
||||
if ( 'stage' === $this->type && true === $this->focus ) {
|
||||
$hooks = array();
|
||||
foreach( $this->stage_hooks as $stage_hook ) {
|
||||
$hooks = array_merge( $hooks, $stage_hook );
|
||||
}
|
||||
$this->set_stage_hooks( $hooks );
|
||||
}
|
||||
|
||||
if ( 'stage' === $this->type ) {
|
||||
if ( 'bootstrap' === $this->focus ) {
|
||||
$this->set_stage_hooks( $this->stage_hooks['bootstrap'] );
|
||||
} else if ( ! $this->focus ) {
|
||||
$logger = new Logger( array( 'stage' => 'bootstrap' ) );
|
||||
$logger->start();
|
||||
}
|
||||
}
|
||||
WP_CLI::get_runner()->load_wordpress();
|
||||
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
|
||||
$this->loggers['wp_profile_last_hook']->stop();
|
||||
}
|
||||
if ( 'stage' === $this->type && ! $this->focus ) {
|
||||
$logger->stop();
|
||||
$this->loggers[] = $logger;
|
||||
}
|
||||
|
||||
// Set up main_query main WordPress query.
|
||||
if ( 'stage' === $this->type ) {
|
||||
if ( 'main_query' === $this->focus ) {
|
||||
$this->set_stage_hooks( $this->stage_hooks['main_query'] );
|
||||
} else if ( ! $this->focus ) {
|
||||
$logger = new Logger( array( 'stage' => 'main_query' ) );
|
||||
$logger->start();
|
||||
}
|
||||
}
|
||||
wp();
|
||||
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
|
||||
$this->loggers['wp_profile_last_hook']->stop();
|
||||
}
|
||||
if ( 'stage' === $this->type && ! $this->focus ) {
|
||||
$logger->stop();
|
||||
$this->loggers[] = $logger;
|
||||
}
|
||||
|
||||
define( 'WP_USE_THEMES', true );
|
||||
|
||||
// Template is normally loaded in global stage, so we need to replicate
|
||||
foreach( $GLOBALS as $key => $value ) {
|
||||
global $$key;
|
||||
}
|
||||
|
||||
// Load the theme template.
|
||||
if ( 'stage' === $this->type ) {
|
||||
if ( 'template' === $this->focus ) {
|
||||
$this->set_stage_hooks( $this->stage_hooks['template'] );
|
||||
} else if ( ! $this->focus ) {
|
||||
$logger = new Logger( array( 'stage' => 'template' ) );
|
||||
$logger->start();
|
||||
}
|
||||
}
|
||||
ob_start();
|
||||
require_once( ABSPATH . WPINC . '/template-loader.php' );
|
||||
ob_get_clean();
|
||||
if ( isset( $this->loggers['wp_profile_last_hook'] ) && $this->loggers['wp_profile_last_hook']->running() ) {
|
||||
$this->loggers['wp_profile_last_hook']->stop();
|
||||
}
|
||||
if ( 'stage' === $this->type && ! $this->focus ) {
|
||||
$logger->stop();
|
||||
$this->loggers[] = $logger;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a human-readable name from a callback
|
||||
*/
|
||||
private static function get_name_location_from_callback( $callback ) {
|
||||
$name = $location = '';
|
||||
$reflection = false;
|
||||
if ( is_array( $callback ) && is_object( $callback[0] ) ) {
|
||||
$reflection = new \ReflectionMethod( $callback[0], $callback[1] );
|
||||
$name = get_class( $callback[0] ) . '->' . $callback[1] . '()';
|
||||
} elseif ( is_array( $callback ) && method_exists( $callback[0], $callback[1] ) ) {
|
||||
$reflection = new \ReflectionMethod( $callback[0], $callback[1] );
|
||||
$name = $callback[0] . '::' . $callback[1] . '()';
|
||||
} elseif ( is_object( $callback ) && is_a( $callback, 'Closure' ) ) {
|
||||
$reflection = new \ReflectionFunction( $callback );
|
||||
$name = 'function(){}';
|
||||
} else if ( is_string( $callback ) ) {
|
||||
$reflection = new \ReflectionFunction( $callback );
|
||||
$name = $callback . '()';
|
||||
}
|
||||
if ( $reflection ) {
|
||||
$location = $reflection->getFileName() . ':' . $reflection->getStartLine();
|
||||
if ( 0 === stripos( $location, WP_PLUGIN_DIR ) ) {
|
||||
$location = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $location );
|
||||
} else if ( 0 === stripos( $location, WPMU_PLUGIN_DIR ) ) {
|
||||
$location = str_replace( trailingslashit( dirname( WPMU_PLUGIN_DIR ) ), '', $location );
|
||||
} else if ( 0 === stripos( $location, get_theme_root() ) ) {
|
||||
$location = str_replace( trailingslashit( get_theme_root() ), '', $location );
|
||||
} else if ( 0 === stripos( $location, ABSPATH . 'wp-admin/' ) ) {
|
||||
$location = str_replace( ABSPATH, '', $location );
|
||||
} else if ( 0 === stripos( $location, ABSPATH . 'wp-includes/' ) ) {
|
||||
$location = str_replace( ABSPATH, '', $location );
|
||||
}
|
||||
}
|
||||
return array( $name, $location );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hooks for the current stage
|
||||
*/
|
||||
private function set_stage_hooks( $hooks ) {
|
||||
$this->current_stage_hooks = $hooks;
|
||||
$pseudo_hook = "before {$hooks[0]}";
|
||||
$this->loggers[ $pseudo_hook ] = new Logger( array( 'hook' => '' ) );
|
||||
$this->loggers[ $pseudo_hook ]->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the callbacks for a given filter
|
||||
*
|
||||
* @param string
|
||||
* @return array|false
|
||||
*/
|
||||
private static function get_filter_callbacks( $filter ) {
|
||||
global $wp_filter;
|
||||
|
||||
if ( ! isset( $wp_filter[ $filter ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( is_a( $wp_filter[ $filter ], 'WP_Hook' ) ) {
|
||||
$callbacks = $wp_filter[ $filter ]->callbacks;
|
||||
} else {
|
||||
$callbacks = $wp_filter[ $filter ];
|
||||
}
|
||||
if ( is_array( $callbacks ) ) {
|
||||
return $callbacks;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the callbacks for a given filter
|
||||
*
|
||||
* @param string $filter
|
||||
* @param mixed $callbacks
|
||||
*/
|
||||
private static function set_filter_callbacks( $filter, $callbacks ) {
|
||||
global $wp_filter;
|
||||
|
||||
if ( ! isset( $wp_filter[ $filter ] ) && class_exists( 'WP_Hook' ) ) {
|
||||
$wp_filter[ $filter ] = new \WP_Hook;
|
||||
}
|
||||
|
||||
if ( is_a( $wp_filter[ $filter ], 'WP_Hook' ) ) {
|
||||
$wp_filter[ $filter ]->callbacks = $callbacks;
|
||||
} else {
|
||||
$wp_filter[ $filter ] = $callbacks;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue