Merge pull request #23817 from civicrm/5.51
[civicrm-core.git] / Civi / API / Subscriber / DebugSubscriber.php
index 88f529f972e916ee85726dbc48100473f9bb351f..0e6be75478e74dd606e2923cd00bf66955aee798 100644 (file)
@@ -11,7 +11,6 @@
 
 namespace Civi\API\Subscriber;
 
-use Civi\API\Events;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 /**
@@ -25,13 +24,36 @@ class DebugSubscriber implements EventSubscriberInterface {
    */
   private $debugLog;
 
+  /**
+   * @var bool
+   */
+  private $enableStats;
+
+  public function __construct() {
+    $version = phpversion('xdebug');
+    switch ($version ? substr($version, 0, 2) : NULL) {
+      case '2.':
+        $this->enableStats = function_exists('xdebug_time_index');
+        break;
+
+      case '3.':
+        $xdebugMode = explode(',', ini_get('xdebug.mode'));
+        $this->enableStats = in_array('develop', $xdebugMode);
+        break;
+
+      default:
+        $this->enableStats = FALSE;
+        break;
+    }
+  }
+
   /**
    * @return array
    */
   public static function getSubscribedEvents() {
     return [
-      Events::PREPARE => ['onApiPrepare', 999],
-      Events::RESPOND => ['onApiRespond', -999],
+      'civi.api.prepare' => ['onApiPrepare', 999],
+      'civi.api.respond' => ['onApiRespond', -999],
     ];
   }
 
@@ -71,7 +93,7 @@ class DebugSubscriber implements EventSubscriberInterface {
       if (isset($this->debugLog) && $this->debugLog->getMessages()) {
         $debug['log'] = $this->debugLog->getMessages();
       }
-      if (function_exists('xdebug_time_index')) {
+      if ($this->enableStats) {
         $debug['peakMemory'] = xdebug_peak_memory_usage();
         $debug['memory'] = xdebug_memory_usage();
         $debug['timeIndex'] = xdebug_time_index();