7 use Drush\Log\LogLevel;
9 define('XH_PROFILE_MEMORY', FALSE);
11 define('XH_PROFILE_CPU', FALSE);
13 define('XH_PROFILE_BUILTINS', TRUE);
16 * Implements hook_drush_help_alter().
18 function xh_drush_help_alter(&$command) {
19 if ($command['command'] == 'global-options') {
20 // Do not include these in options in standard help.
21 if ($command['#brief'] === FALSE) {
22 $command['options']['xh'] = array(
23 'description' => 'Enable profiling via XHProf',
25 $command['sub-options']['xh']['xh-link'] = 'URL to your XHProf report site.';
26 $command['sub-options']['xh']['xh-path'] = 'Absolute path to the xhprof project.';
27 $command['sub-options']['xh']['xh-profile-builtins'] = 'Profile built-in PHP functions (defaults to TRUE).';
28 $command['sub-options']['xh']['xh-profile-cpu'] = 'Profile CPU (defaults to FALSE).';
29 $command['sub-options']['xh']['xh-profile-memory'] = 'Profile Memory (defaults to FALSE).';
34 function xh_enabled() {
35 if (drush_get_option('xh')) {
36 if (!extension_loaded('xhprof')) {
37 return drush_set_error('You must enable the xhprof PHP extension in your CLI PHP in order to profile.');
39 if (!drush_get_option('xh-path', '')) {
40 return drush_set_error('You must provide the xh-path option in your drushrc or on the CLI in order to profile.');
47 * Determines flags for xhprof_enable based on drush options.
51 if (!drush_get_option('xh-profile-builtins', XH_PROFILE_BUILTINS)) {
52 $flags |= XHPROF_FLAGS_NO_BUILTINS;
54 if (drush_get_option('xh-profile-cpu', XH_PROFILE_CPU)) {
55 $flags |= XHPROF_FLAGS_CPU;
57 if (drush_get_option('xh-profile-memory', XH_PROFILE_MEMORY)) {
58 $flags |= XHPROF_FLAGS_MEMORY;
64 * Implements hook_drush_init().
66 function xh_drush_init() {
68 if ($path = drush_get_option('xh-path', '')) {
69 include_once $path . '/xhprof_lib/utils/xhprof_lib.php';
70 include_once $path . '/xhprof_lib/utils/xhprof_runs.php';
71 xhprof_enable(xh_flags());
77 * Implements hook_drush_exit().
79 function xh_drush_exit() {
81 $args = func_get_args();
82 $namespace = 'Drush'; // variable_get('site_name', '');
83 $xhprof_data = xhprof_disable();
84 $xhprof_runs = new XHProfRuns_Default();
85 $run_id = $xhprof_runs->save_run($xhprof_data, $namespace);
86 if ($url = xh_link($run_id)) {
87 drush_log(dt('XHProf run saved. View report at !url', array('!url' => $url)), LogLevel::OK);
93 * Returns the XHProf link.
95 function xh_link($run_id) {
96 if ($xhprof_url = trim(drush_get_option('xh-link'))) {
97 $namespace = 'Drush'; //variable_get('site_name', '');
98 return $xhprof_url . '/index.php?run=' . urlencode($run_id) . '&source=' . urlencode($namespace);
101 drush_log('Configure xh_link in order to see a link to the XHProf report for this request instead of this message.');