GH#36 Moved function to correct file

This commit is contained in:
Sidsector9 2017-10-11 13:27:14 +05:30
parent 806ea09778
commit cb164727b5
3 changed files with 58 additions and 49 deletions

View file

@ -8,8 +8,8 @@ Feature: Basic profile usage
"""
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>]
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.
"""

View file

@ -91,15 +91,11 @@ 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 );
$order = Utils\get_flag_value( $assoc_args, 'order', 'ASC' );
$orderby = Utils\get_flag_value( $assoc_args, 'orderby' );
if ( Utils\get_flag_value( $assoc_args, 'fields' ) ) {
$set_fields = explode( ',', Utils\get_flag_value( $assoc_args, 'fields' ) );
}
$valid_stages = array( 'bootstrap', 'main_query', 'template' );
if ( $focus && ( true !== $focus && ! in_array( $focus, $valid_stages, true ) ) ) {
WP_CLI::error( 'Invalid stage. Must be one of ' . implode( ', ', $valid_stages ) . ', or use --all.' );
@ -147,41 +143,7 @@ class Command {
$loggers = self::shine_spotlight( $loggers, $metrics );
}
if ( $orderby ) {
usort( $loggers, function( $a, $b ) use ( $order, $orderby ) {
if ( 'ASC' === $order ) {
if ( is_numeric( $a->$orderby ) && is_numeric( $b->$orderby ) ) {
return $this->compare_float( $a->$orderby, $b->$orderby );
} else {
return strcmp( $a->$orderby, $b->$orderby );
}
} elseif ( 'DESC' === $order ) {
if ( is_numeric( $b->$orderby ) && is_numeric( $a->$orderby ) ) {
return $this->compare_float( $b->$orderby, $a->$orderby );
} else {
return strcmp( $b->$orderby, $a->$orderby );
}
}
});
}
$formatter->display_items( $loggers );
}
/**
* Function to compare floats.
*
* @param double $a Floating number.
* @param double $b Floating number.
*/
private function compare_float( $a, $b ) {
if ( abs( $a - $b ) < 0.00000001 ) {
return 0;
} elseif ( ( $a - $b ) < 0 ) {
return -1;
} else {
return 1;
}
$formatter->display_items( $order, $orderby, $loggers );
}
/**
@ -209,6 +171,13 @@ class Command {
*
* [--format=<format>]
* : Render output in a particular format.
*
* [--order=<order>]
* : Ascending or Descending order. ASC|DESC.
*
* [--orderby=<orderby>]
* : Order by fields.
*
* ---
* default: table
* options:
@ -224,6 +193,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();
@ -255,7 +227,7 @@ class Command {
if ( Utils\get_flag_value( $assoc_args, 'spotlight' ) ) {
$loggers = self::shine_spotlight( $loggers, $metrics );
}
$formatter->display_items( $loggers );
$formatter->display_items( $order, $orderby, $loggers );
}
/**

View file

@ -42,21 +42,39 @@ class Formatter {
*
* @param array $items
*/
public function display_items( $items, $include_total = true ) {
public function display_items( $order, $orderby, $items, $include_total = true ) {
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,25 @@ 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 ) {
if ( 'ASC' === $order ) {
if ( is_numeric( $a->$orderby ) && is_numeric( $b->$orderby ) ) {
return $this->compare_float( $a->$orderby, $b->$orderby );
} else {
return strcmp( $a->$orderby, $b->$orderby );
}
} elseif ( 'DESC' === $order ) {
if ( is_numeric( $b->$orderby ) && is_numeric( $a->$orderby ) ) {
return $this->compare_float( $b->$orderby, $a->$orderby );
} else {
return strcmp( $b->$orderby, $a->$orderby );
}
}
});
}
$location_index = array_search( 'location', $fields );
foreach ( $items as $item ) {
$values = array_values( \WP_CLI\Utils\pick_fields( $item, $fields ) );