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 #142 from Sidsector9/feature/GH#36
GH#36 Add sort feature by field type and orderby
This commit is contained in:
commit
43353544a7
4 changed files with 129 additions and 13 deletions
|
@ -65,6 +65,25 @@ Feature: Profile the template render stage
|
|||
| wp_footer:after |
|
||||
| total (13) |
|
||||
|
||||
When I run `wp profile stage template --fields=hook --orderby=hook --order=DESC`
|
||||
Then STDOUT should be a table containing rows:
|
||||
| hook |
|
||||
| wp_head:before |
|
||||
| wp_head |
|
||||
| wp_footer:before |
|
||||
| wp_footer:after |
|
||||
| wp_footer |
|
||||
| template_redirect:before |
|
||||
| template_redirect |
|
||||
| template_include:before |
|
||||
| template_include |
|
||||
| loop_start:before |
|
||||
| loop_start |
|
||||
| loop_end:before |
|
||||
| loop_end |
|
||||
| total (13) |
|
||||
|
||||
|
||||
Scenario: Use --all flag to profile all stages
|
||||
Given a WP install
|
||||
|
||||
|
|
|
@ -6,10 +6,10 @@ Feature: Basic profile usage
|
|||
When I run `wp profile`
|
||||
Then STDOUT should be:
|
||||
"""
|
||||
usage: wp profile eval <php-code> [--hook[=<hook>]] [--fields=<fields>] [--format=<format>]
|
||||
or: wp profile eval-file <file> [--hook[=<hook>]] [--fields=<fields>] [--format=<format>]
|
||||
or: wp profile hook [<hook>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>]
|
||||
or: wp profile stage [<stage>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>]
|
||||
usage: wp profile eval <php-code> [--hook[=<hook>]] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<orderby>]
|
||||
or: wp profile eval-file <file> [--hook[=<hook>]] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<orderby>]
|
||||
or: wp profile hook [<hook>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<orderby>]
|
||||
or: wp profile stage [<stage>] [--all] [--spotlight] [--url=<url>] [--fields=<fields>] [--format=<format>] [--order=<order>] [--orderby=<orderby>]
|
||||
|
||||
See 'wp help profile <command>' for more information on a specific command.
|
||||
"""
|
||||
|
|
|
@ -70,6 +70,19 @@ class Command {
|
|||
*
|
||||
* [--format=<format>]
|
||||
* : Render output in a particular format.
|
||||
*
|
||||
* [--order=<order>]
|
||||
* : Ascending or Descending order.
|
||||
* ---
|
||||
* default: ASC
|
||||
* options:
|
||||
* - ASC
|
||||
* - DESC
|
||||
* ---
|
||||
*
|
||||
* [--orderby=<orderby>]
|
||||
* : Order by fields.
|
||||
*
|
||||
* ---
|
||||
* default: table
|
||||
* options:
|
||||
|
@ -84,7 +97,10 @@ class Command {
|
|||
public function stage( $args, $assoc_args ) {
|
||||
global $wpdb;
|
||||
|
||||
$focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );
|
||||
$focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );
|
||||
|
||||
$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
|
||||
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
|
||||
|
||||
$valid_stages = array( 'bootstrap', 'main_query', 'template' );
|
||||
if ( $focus && ( true !== $focus && ! in_array( $focus, $valid_stages, true ) ) ) {
|
||||
|
@ -132,7 +148,8 @@ class Command {
|
|||
if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) {
|
||||
$loggers = self::shine_spotlight( $loggers, $metrics );
|
||||
}
|
||||
$formatter->display_items( $loggers );
|
||||
|
||||
$formatter->display_items( $loggers, true, $order, $orderby );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -160,7 +177,19 @@ class Command {
|
|||
*
|
||||
* [--format=<format>]
|
||||
* : Render output in a particular format.
|
||||
*
|
||||
* [--order=<order>]
|
||||
* : Ascending or Descending order.
|
||||
* ---
|
||||
* default: ASC
|
||||
* options:
|
||||
* - ASC
|
||||
* - DESC
|
||||
* ---
|
||||
*
|
||||
* [--orderby=<orderby>]
|
||||
* : Order by fields.
|
||||
*
|
||||
* default: table
|
||||
* options:
|
||||
* - table
|
||||
|
@ -175,6 +204,9 @@ class Command {
|
|||
|
||||
$focus = Utils\get_flag_value( $assoc_args, 'all', isset( $args[0] ) ? $args[0] : null );
|
||||
|
||||
$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
|
||||
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
|
||||
|
||||
$profiler = new Profiler( 'hook', $focus );
|
||||
$profiler->run();
|
||||
|
||||
|
@ -206,7 +238,7 @@ class Command {
|
|||
if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) {
|
||||
$loggers = self::shine_spotlight( $loggers, $metrics );
|
||||
}
|
||||
$formatter->display_items( $loggers );
|
||||
$formatter->display_items( $loggers, true, $order, $orderby );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -229,6 +261,19 @@ class Command {
|
|||
*
|
||||
* [--format=<format>]
|
||||
* : Render output in a particular format.
|
||||
*
|
||||
* [--order=<order>]
|
||||
* : Ascending or Descending order.
|
||||
* ---
|
||||
* default: ASC
|
||||
* options:
|
||||
* - ASC
|
||||
* - DESC
|
||||
* ---
|
||||
*
|
||||
* [--orderby=<orderby>]
|
||||
* : Order by fields.
|
||||
*
|
||||
* ---
|
||||
* default: table
|
||||
* options:
|
||||
|
@ -242,9 +287,13 @@ class Command {
|
|||
*/
|
||||
public function eval_( $args, $assoc_args ) {
|
||||
$statement = $args[0];
|
||||
|
||||
$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
|
||||
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
|
||||
|
||||
self::profile_eval_ish( $assoc_args, function() use ( $statement ) {
|
||||
eval( $statement );
|
||||
});
|
||||
}, $order, $orderby );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -267,6 +316,19 @@ class Command {
|
|||
*
|
||||
* [--format=<format>]
|
||||
* : Render output in a particular format.
|
||||
*
|
||||
* [--order=<order>]
|
||||
* : Ascending or Descending order.
|
||||
* ---
|
||||
* default: ASC
|
||||
* options:
|
||||
* - ASC
|
||||
* - DESC
|
||||
* ---
|
||||
*
|
||||
* [--orderby=<orderby>]
|
||||
* : Order by fields.
|
||||
*
|
||||
* ---
|
||||
* default: table
|
||||
* options:
|
||||
|
@ -281,13 +343,17 @@ class Command {
|
|||
public function eval_file( $args, $assoc_args ) {
|
||||
|
||||
$file = $args[0];
|
||||
|
||||
$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
|
||||
$orderby = Utils\get_flag_value( $assoc_args, 'orderby', null );
|
||||
|
||||
if ( ! file_exists( $file ) ) {
|
||||
WP_CLI::error( "'$file' does not exist." );
|
||||
}
|
||||
|
||||
self::profile_eval_ish( $assoc_args, function() use ( $file ) {
|
||||
self::include_file( $file );
|
||||
});
|
||||
}, $order, $orderby );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -330,7 +396,7 @@ class Command {
|
|||
'request_count',
|
||||
) );
|
||||
$formatter = new Formatter( $assoc_args, $fields );
|
||||
$formatter->display_items( $loggers, false );
|
||||
$formatter->display_items( $loggers, false, $order, $orderby );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -42,21 +42,39 @@ class Formatter {
|
|||
*
|
||||
* @param array $items
|
||||
*/
|
||||
public function display_items( $items, $include_total = true ) {
|
||||
public function display_items( $items, $include_total = true, $order, $orderby ) {
|
||||
if ( 'table' === $this->args['format'] && empty( $this->args['field'] ) ) {
|
||||
$this->show_table( $items, $this->args['fields'], $include_total );
|
||||
$this->show_table( $order, $orderby, $items, $this->args['fields'], $include_total );
|
||||
} else {
|
||||
$this->formatter->display_items( $items );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to compare floats.
|
||||
*
|
||||
* @param double $a Floating number.
|
||||
* @param double $b Floating number.
|
||||
*/
|
||||
private function compare_float( $a, $b ) {
|
||||
$a = number_format( $a, 4 );
|
||||
$b = number_format( $b, 4 );
|
||||
if ( 0 === $a - $b ) {
|
||||
return 0;
|
||||
} else if ( $a - $b < 0 ) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show items in a \cli\Table.
|
||||
*
|
||||
* @param array $items
|
||||
* @param array $fields
|
||||
*/
|
||||
private function show_table( $items, $fields, $include_total ) {
|
||||
private function show_table( $order, $orderby, $items, $fields, $include_total ) {
|
||||
$table = new \cli\Table();
|
||||
|
||||
$enabled = \cli\Colors::shouldColorize();
|
||||
|
@ -70,6 +88,19 @@ class Formatter {
|
|||
if ( ! is_null( $this->total_cell_index ) ) {
|
||||
$totals[ $this->total_cell_index ] = 'total (' . count( $items ) . ')';
|
||||
}
|
||||
|
||||
if ( $orderby ) {
|
||||
usort( $items, function( $a, $b ) use ( $order, $orderby ) {
|
||||
list( $first, $second ) = 'ASC' === $order ? array( $a, $b ) : array( $b, $a );
|
||||
|
||||
if ( is_numeric( $first->$orderby ) && is_numeric( $second->$orderby ) ) {
|
||||
return $this->compare_float( $first->$orderby, $second->$orderby );
|
||||
}
|
||||
|
||||
return strcmp( $first->$orderby, $second->$orderby );
|
||||
});
|
||||
}
|
||||
|
||||
$location_index = array_search( 'location', $fields );
|
||||
foreach ( $items as $item ) {
|
||||
$values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue