Merge pull request #42 from runcommand/rename-scope-stage

Rename scopes to stages
This commit is contained in:
Daniel Bachhuber 2016-08-28 14:56:57 -07:00 committed by GitHub
commit 4171283359

View file

@ -11,8 +11,8 @@ use WP_CLI\Utils;
class Command { class Command {
private $loggers = array(); private $loggers = array();
private $focus_scope; private $focus_stage;
private $scope_hooks = array(); private $stage_hooks = array();
private $focus_hook; private $focus_hook;
private $current_filter_callbacks = array(); private $current_filter_callbacks = array();
private $focus_query_offset = 0; private $focus_query_offset = 0;
@ -26,7 +26,7 @@ class Command {
* ``` * ```
* $ wp profile * $ wp profile
* +------------+----------------+-------------+------------+------------+-----------+ * +------------+----------------+-------------+------------+------------+-----------+
* | scope | execution_time | query_count | query_time | hook_count | hook_time | * | stage | execution_time | query_count | query_time | hook_count | hook_time |
* +------------+----------------+-------------+------------+------------+-----------+ * +------------+----------------+-------------+------------+------------+-----------+
* | total | 2.6685s | 196 | 0.0274s | 10723 | 0.2173s | * | total | 2.6685s | 196 | 0.0274s | 10723 | 0.2173s |
* | bootstrap | 2.2609s | 15 | 0.0037s | 2836 | 0.1166s | * | bootstrap | 2.2609s | 15 | 0.0037s | 2836 | 0.1166s |
@ -40,8 +40,8 @@ class Command {
* [--url=<url>] * [--url=<url>]
* : Execute a request against a specified URL. Defaults to the home URL. * : Execute a request against a specified URL. Defaults to the home URL.
* *
* [--scope=<scope>] * [--stage=<stage>]
* : Drill down into a specific scope. * : Drill down into a specific stage.
* --- * ---
* options: * options:
* - bootstrap * - bootstrap
@ -71,7 +71,7 @@ class Command {
public function __invoke( $args, $assoc_args ) { public function __invoke( $args, $assoc_args ) {
global $wpdb; global $wpdb;
$this->focus_scope = Utils\get_flag_value( $assoc_args, 'scope' ); $this->focus_stage = Utils\get_flag_value( $assoc_args, 'stage' );
$this->focus_hook = Utils\get_flag_value( $assoc_args, 'hook' ); $this->focus_hook = Utils\get_flag_value( $assoc_args, 'hook' );
if ( ! isset( WP_CLI::get_runner()->config['url'] ) ) { if ( ! isset( WP_CLI::get_runner()->config['url'] ) ) {
@ -91,7 +91,7 @@ class Command {
// pass through // pass through
} }
if ( $this->focus_scope ) { if ( $this->focus_stage ) {
$fields = array( $fields = array(
'hook', 'hook',
'time', 'time',
@ -117,7 +117,7 @@ class Command {
); );
} else { } else {
$fields = array( $fields = array(
'scope', 'stage',
'time', 'time',
'query_time', 'query_time',
'query_count', 'query_count',
@ -145,7 +145,7 @@ class Command {
} }
$current_filter = current_filter(); $current_filter = current_filter();
if ( in_array( $current_filter, $this->scope_hooks ) ) { if ( in_array( $current_filter, $this->stage_hooks ) ) {
$pseudo_hook = "before {$current_filter}"; $pseudo_hook = "before {$current_filter}";
if ( isset( $this->loggers[ $pseudo_hook ] ) ) { if ( isset( $this->loggers[ $pseudo_hook ] ) ) {
$this->loggers[ $pseudo_hook ]->stop(); $this->loggers[ $pseudo_hook ]->stop();
@ -223,11 +223,11 @@ class Command {
} }
$current_filter = current_filter(); $current_filter = current_filter();
if ( in_array( $current_filter, $this->scope_hooks ) ) { if ( in_array( $current_filter, $this->stage_hooks ) ) {
$this->loggers[ $current_filter ]->stop(); $this->loggers[ $current_filter ]->stop();
$key = array_search( $current_filter, $this->scope_hooks ); $key = array_search( $current_filter, $this->stage_hooks );
if ( false !== $key && isset( $this->scope_hooks[$key+1] ) ) { if ( false !== $key && isset( $this->stage_hooks[$key+1] ) ) {
$pseudo_hook = "before {$this->scope_hooks[$key+1]}"; $pseudo_hook = "before {$this->stage_hooks[$key+1]}";
$this->loggers[ $pseudo_hook ] = new Logger( 'hook', '' ); $this->loggers[ $pseudo_hook ] = new Logger( 'hook', '' );
$this->loggers[ $pseudo_hook ]->start(); $this->loggers[ $pseudo_hook ]->start();
} }
@ -262,8 +262,8 @@ class Command {
private function load_wordpress_with_template() { private function load_wordpress_with_template() {
global $wp_query; global $wp_query;
if ( 'bootstrap' === $this->focus_scope ) { if ( 'bootstrap' === $this->focus_stage ) {
$this->set_scope_hooks( array( $this->set_stage_hooks( array(
'muplugins_loaded', 'muplugins_loaded',
'plugins_loaded', 'plugins_loaded',
'setup_theme', 'setup_theme',
@ -271,51 +271,51 @@ class Command {
'init', 'init',
'wp_loaded', 'wp_loaded',
) ); ) );
} else if ( ! $this->focus_scope && ! $this->focus_hook ) { } else if ( ! $this->focus_stage && ! $this->focus_hook ) {
$logger = new Logger( 'scope', 'bootstrap' ); $logger = new Logger( 'stage', 'bootstrap' );
$logger->start(); $logger->start();
} }
WP_CLI::get_runner()->load_wordpress(); WP_CLI::get_runner()->load_wordpress();
if ( ! $this->focus_scope && ! $this->focus_hook ) { if ( ! $this->focus_stage && ! $this->focus_hook ) {
$logger->stop(); $logger->stop();
$this->loggers[] = $logger; $this->loggers[] = $logger;
} }
// Set up main_query main WordPress query. // Set up main_query main WordPress query.
if ( 'main_query' === $this->focus_scope ) { if ( 'main_query' === $this->focus_stage ) {
$this->set_scope_hooks( array( $this->set_stage_hooks( array(
'parse_request', 'parse_request',
'send_headers', 'send_headers',
'pre_get_posts', 'pre_get_posts',
'the_posts', 'the_posts',
'wp', 'wp',
) ); ) );
} else if ( ! $this->focus_scope && ! $this->focus_hook ) { } else if ( ! $this->focus_stage && ! $this->focus_hook ) {
$logger = new Logger( 'scope', 'main_query' ); $logger = new Logger( 'stage', 'main_query' );
$logger->start(); $logger->start();
} }
wp(); wp();
if ( ! $this->focus_scope && ! $this->focus_hook ) { if ( ! $this->focus_stage && ! $this->focus_hook ) {
$logger->stop(); $logger->stop();
$this->loggers[] = $logger; $this->loggers[] = $logger;
} }
define( 'WP_USE_THEMES', true ); define( 'WP_USE_THEMES', true );
// Template is normally loaded in global scope, so we need to replicate // Template is normally loaded in global stage, so we need to replicate
foreach( $GLOBALS as $key => $value ) { foreach( $GLOBALS as $key => $value ) {
global $$key; global $$key;
} }
// Load the theme template. // Load the theme template.
if ( ! $this->focus_scope && ! $this->focus_hook ) { if ( ! $this->focus_stage && ! $this->focus_hook ) {
$logger = new Logger( 'scope', 'template' ); $logger = new Logger( 'stage', 'template' );
$logger->start(); $logger->start();
} }
ob_start(); ob_start();
require_once( ABSPATH . WPINC . '/template-loader.php' ); require_once( ABSPATH . WPINC . '/template-loader.php' );
ob_get_clean(); ob_get_clean();
if ( ! $this->focus_scope && ! $this->focus_hook ) { if ( ! $this->focus_stage && ! $this->focus_hook ) {
$logger->stop(); $logger->stop();
$this->loggers[] = $logger; $this->loggers[] = $logger;
} }
@ -339,10 +339,10 @@ class Command {
} }
/** /**
* Set the hooks for the current scope * Set the hooks for the current stage
*/ */
private function set_scope_hooks( $hooks ) { private function set_stage_hooks( $hooks ) {
$this->scope_hooks = $hooks; $this->stage_hooks = $hooks;
$pseudo_hook = "before {$hooks[0]}"; $pseudo_hook = "before {$hooks[0]}";
$this->loggers[ $pseudo_hook ] = new Logger( 'hook', '' ); $this->loggers[ $pseudo_hook ] = new Logger( 'hook', '' );
$this->loggers[ $pseudo_hook ]->start(); $this->loggers[ $pseudo_hook ]->start();