@@ -221,6 +221,9 @@ public function hook( $args, $assoc_args ) {
221221 * <php-code>
222222 * : The code to execute, as a string.
223223 *
224+ * [--hook[=<hook>]]
225+ * : Focus on key metrics for all hooks, or callbacks on a specific hook.
226+ *
224227 * [--fields=<fields>]
225228 * : Display one or more fields.
226229 *
@@ -235,31 +238,13 @@ public function hook( $args, $assoc_args ) {
235238 * - csv
236239 * ---
237240 *
238- * @when before_wp_load
239241 * @subcommand eval
240242 */
241243 public function eval_ ( $ args , $ assoc_args ) {
242-
243- $ profiler = new Profiler ( false , false );
244- $ profiler ->run ();
245-
246- $ logger = new Logger ();
247- $ logger ->start ();
248- eval ( $ args [0 ] );
249- $ logger ->stop ();
250-
251- $ fields = array (
252- 'time ' ,
253- 'query_time ' ,
254- 'query_count ' ,
255- 'cache_ratio ' ,
256- 'cache_hits ' ,
257- 'cache_misses ' ,
258- 'request_time ' ,
259- 'request_count ' ,
260- );
261- $ formatter = new Formatter ( $ assoc_args , $ fields );
262- $ formatter ->display_items ( array ( $ logger ), false );
244+ $ statement = $ args [0 ];
245+ self ::profile_eval_ish ( $ assoc_args , function () use ( $ statement ) {
246+ eval ( $ statement );
247+ });
263248 }
264249
265250 /**
@@ -274,6 +259,9 @@ public function eval_( $args, $assoc_args ) {
274259 * <file>
275260 * : The path to the PHP file to execute and profile.
276261 *
262+ * [--hook[=<hook>]]
263+ * : Focus on key metrics for all hooks, or callbacks on a specific hook.
264+ *
277265 * [--fields=<fields>]
278266 * : Display one or more fields.
279267 *
@@ -288,7 +276,6 @@ public function eval_( $args, $assoc_args ) {
288276 * - csv
289277 * ---
290278 *
291- * @when before_wp_load
292279 * @subcommand eval-file
293280 */
294281 public function eval_file ( $ args , $ assoc_args ) {
@@ -298,15 +285,41 @@ public function eval_file( $args, $assoc_args ) {
298285 WP_CLI ::error ( "' $ file' does not exist. " );
299286 }
300287
301- $ profiler = new Profiler ( false , false );
302- $ profiler ->run ();
303-
304- $ logger = new Logger ();
305- $ logger ->start ();
306- self ::include_file ( $ file );
307- $ logger ->stop ();
288+ self ::profile_eval_ish ( $ assoc_args , function () use ( $ file ) {
289+ self ::include_file ( $ file );
290+ });
291+ }
308292
309- $ fields = array (
293+ /**
294+ * Profile an eval or eval-file statement.
295+ */
296+ private static function profile_eval_ish ( $ assoc_args , $ profile_callback ) {
297+ $ hook = Utils \get_flag_value ( $ assoc_args , 'hook ' );
298+ $ type = $ focus = false ;
299+ $ fields = array ();
300+ if ( $ hook ) {
301+ $ type = 'hook ' ;
302+ if ( true !== $ hook ) {
303+ $ focus = $ hook ;
304+ $ fields [] = 'callback ' ;
305+ $ fields [] = 'location ' ;
306+ } else {
307+ $ fields [] = 'hook ' ;
308+ }
309+ }
310+ $ profiler = new Profiler ( $ type , $ focus );
311+ $ profiler ->run ();
312+ if ( $ hook ) {
313+ $ profile_callback ();
314+ $ loggers = $ profiler ->get_loggers ();
315+ } else {
316+ $ logger = new Logger ();
317+ $ logger ->start ();
318+ $ profile_callback ();
319+ $ logger ->stop ();
320+ $ loggers = array ( $ logger );
321+ }
322+ $ fields = array_merge ( $ fields , array (
310323 'time ' ,
311324 'query_time ' ,
312325 'query_count ' ,
@@ -315,9 +328,9 @@ public function eval_file( $args, $assoc_args ) {
315328 'cache_misses ' ,
316329 'request_time ' ,
317330 'request_count ' ,
318- );
331+ ) ) ;
319332 $ formatter = new Formatter ( $ assoc_args , $ fields );
320- $ formatter ->display_items ( array ( $ logger ) , false );
333+ $ formatter ->display_items ( $ loggers , false );
321334 }
322335
323336 /**
0 commit comments