xh.drush.inc

  1. 8.0.x commands/xh.drush.inc
  2. 7.x commands/xh.drush.inc
  3. master commands/xh.drush.inc

Functions

Namesort descending Description
xh_drush_exit Implements hook_drush_exit().
xh_drush_help_alter Implements hook_drush_help_alter().
xh_drush_init Implements hook_drush_init().
xh_enabled
xh_link Returns the XHProf link.

File

commands/xh.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. */
  5. use Drush\Log\LogLevel;
  6. /**
  7. * Implements hook_drush_help_alter().
  8. */
  9. function xh_drush_help_alter(&$command) {
  10. if ($command['command'] == 'global-options') {
  11. // Do not include these in options in standard help.
  12. if ($command['#brief'] === FALSE) {
  13. $command['options']['xh'] = array(
  14. 'description' => 'Enable profiling via XHProf',
  15. );
  16. $command['sub-options']['xh']['xh-link'] = 'URL to your XHProf report site.';
  17. $command['sub-options']['xh']['xh-path'] = 'Absolute path to the xhprof project.';
  18. }
  19. }
  20. }
  21. function xh_enabled() {
  22. if (drush_get_option('xh')) {
  23. if (!extension_loaded('xhprof')) {
  24. return drush_set_error('You must enable the xhprof PHP extension in your CLI PHP in order to profile.');
  25. }
  26. if (!drush_get_option('xh-path', '')) {
  27. return drush_set_error('You must provide the xh-path option in your drushrc or on the CLI in order to profile.');
  28. }
  29. return TRUE;
  30. }
  31. }
  32. /**
  33. * Implements hook_drush_init().
  34. */
  35. function xh_drush_init() {
  36. if (xh_enabled()) {
  37. if ($path = drush_get_option('xh-path', '')) {
  38. include_once $path . '/xhprof_lib/utils/xhprof_lib.php';
  39. include_once $path . '/xhprof_lib/utils/xhprof_runs.php';
  40. xhprof_enable(); // @todo support custom flags
  41. }
  42. }
  43. }
  44. /**
  45. * Implements hook_drush_exit().
  46. */
  47. function xh_drush_exit() {
  48. if (xh_enabled()) {
  49. $args = func_get_args();
  50. $namespace = 'Drush'; // variable_get('site_name', '');
  51. $xhprof_data = xhprof_disable();
  52. $xhprof_runs = new XHProfRuns_Default();
  53. $run_id = $xhprof_runs->save_run($xhprof_data, $namespace);
  54. if ($url = xh_link($run_id)) {
  55. drush_log(dt('XHProf run saved. View report at !url', array('!url' => $url)), LogLevel::OK);
  56. }
  57. }
  58. }
  59. /**
  60. * Returns the XHProf link.
  61. */
  62. function xh_link($run_id) {
  63. if ($xhprof_url = trim(drush_get_option('xh-link'))) {
  64. $namespace = 'Drush'; //variable_get('site_name', '');
  65. return $xhprof_url . '/index.php?run=' . urlencode($run_id) . '&source=' . urlencode($namespace);
  66. }
  67. else {
  68. drush_log('Configure xh_link in order to see a link to the XHProf report for this request instead of this message.');
  69. }
  70. }