DebugSubscriber - Fix compatibility with XDebug 2/3
authorTim Otten <totten@civicrm.org>
Tue, 30 Nov 2021 23:54:41 +0000 (15:54 -0800)
committerTim Otten <totten@civicrm.org>
Tue, 30 Nov 2021 23:55:19 +0000 (15:55 -0800)
Civi/API/Subscriber/DebugSubscriber.php

index ac1abad89494c6daaf929c36db7a7cc8446f841b..0e6be75478e74dd606e2923cd00bf66955aee798 100644 (file)
@@ -24,6 +24,29 @@ 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
    */
@@ -70,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();