Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / xh.drush.inc
1 <?php
2
3 /**
4  * @file
5  */
6
7 use Drush\Log\LogLevel;
8
9 define('XH_PROFILE_MEMORY', FALSE);
10
11 define('XH_PROFILE_CPU', FALSE);
12
13 define('XH_PROFILE_BUILTINS', TRUE);
14
15 /**
16  * Implements hook_drush_help_alter().
17  */
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',
24       );
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).';
30     }
31   }
32 }
33
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.');
38     }
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.');
41     }
42     return TRUE;
43   }
44 }
45
46 /**
47  * Determines flags for xhprof_enable based on drush options.
48  */
49 function xh_flags() {
50   $flags = 0;
51   if (!drush_get_option('xh-profile-builtins', XH_PROFILE_BUILTINS)) {
52     $flags |= XHPROF_FLAGS_NO_BUILTINS;
53   }
54   if (drush_get_option('xh-profile-cpu', XH_PROFILE_CPU)) {
55     $flags |= XHPROF_FLAGS_CPU;
56   }
57   if (drush_get_option('xh-profile-memory', XH_PROFILE_MEMORY)) {
58     $flags |= XHPROF_FLAGS_MEMORY;
59   }
60   return $flags;
61 }
62
63 /**
64  * Implements hook_drush_init().
65  */
66 function xh_drush_init() {
67   if (xh_enabled()) {
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());
72     }
73   }
74 }
75
76 /**
77  * Implements hook_drush_exit().
78  */
79 function xh_drush_exit() {
80   if (xh_enabled()) {
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);
88     }
89   }
90 }
91
92 /**
93  * Returns the XHProf link.
94  */
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);
99   }
100   else {
101     drush_log('Configure xh_link in order to see a link to the XHProf report for this request instead of this message.');
102   }
103 }