DebugSubscriber - Fix compatibility with XDebug 2/3
[civicrm-core.git] / Civi / API / Subscriber / DebugSubscriber.php
CommitLineData
0661f62b
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
41498ac5 4 | Copyright CiviCRM LLC. All rights reserved. |
0661f62b 5 | |
41498ac5
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
0661f62b 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
0661f62b
TO
11
12namespace Civi\API\Subscriber;
46bcf597 13
0661f62b
TO
14use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
6550386a
EM
16/**
17 * Class XDebugSubscriber
18 * @package Civi\API\Subscriber
19 */
2aafb0fc
CW
20class DebugSubscriber implements EventSubscriberInterface {
21
22 /**
23 * @var \Civi\API\LogObserver
24 */
25 private $debugLog;
34f3bbd9 26
189cc9ff
TO
27 /**
28 * @var bool
29 */
30 private $enableStats;
31
32 public function __construct() {
33 $version = phpversion('xdebug');
34 switch ($version ? substr($version, 0, 2) : NULL) {
35 case '2.':
36 $this->enableStats = function_exists('xdebug_time_index');
37 break;
38
39 case '3.':
40 $xdebugMode = explode(',', ini_get('xdebug.mode'));
41 $this->enableStats = in_array('develop', $xdebugMode);
42 break;
43
44 default:
45 $this->enableStats = FALSE;
46 break;
47 }
48 }
49
6550386a
EM
50 /**
51 * @return array
52 */
0661f62b 53 public static function getSubscribedEvents() {
c64f69d9 54 return [
39b870b8
TO
55 'civi.api.prepare' => ['onApiPrepare', 999],
56 'civi.api.respond' => ['onApiRespond', -999],
c64f69d9 57 ];
0661f62b
TO
58 }
59
2aafb0fc
CW
60 public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) {
61 $apiRequest = $event->getApiRequest();
62 if (!isset($this->debugLog)
63 && !empty($apiRequest['params']['debug'])
64 && (empty($apiRequest['params']['check_permissions']) || \CRM_Core_Permission::check('view debug output'))
65 ) {
66 $this->debugLog = new \Civi\API\LogObserver();
67 \CRM_Core_Error::createDebugLogger()->attach($this->debugLog);
68 }
69 }
70
6550386a
EM
71 /**
72 * @param \Civi\API\Event\RespondEvent $event
8882ff5c 73 * API response event.
6550386a 74 */
8882ff5c 75 public function onApiRespond(\Civi\API\Event\RespondEvent $event) {
0661f62b
TO
76 $apiRequest = $event->getApiRequest();
77 $result = $event->getResponse();
2aafb0fc 78 if (!empty($apiRequest['params']['debug'])
b65fa6dc 79 && (empty($apiRequest['params']['check_permissions']) || \CRM_Core_Permission::check('view debug output'))
0661f62b 80 ) {
b65fa6dc
CW
81 if (is_a($result, '\Civi\Api4\Generic\Result')) {
82 $result->debug = $result->debug ?? [];
83 $debug =& $result->debug;
84 }
85 // result would not be an array for api3 getvalue
86 elseif (is_array($result)) {
87 $result['xdebug'] = $result['xdebug'] ?? [];
88 $debug =& $result['xdebug'];
89 }
90 else {
91 return;
92 }
2aafb0fc
CW
93 if (isset($this->debugLog) && $this->debugLog->getMessages()) {
94 $debug['log'] = $this->debugLog->getMessages();
95 }
189cc9ff 96 if ($this->enableStats) {
2aafb0fc
CW
97 $debug['peakMemory'] = xdebug_peak_memory_usage();
98 $debug['memory'] = xdebug_memory_usage();
99 $debug['timeIndex'] = xdebug_time_index();
100 }
0661f62b
TO
101 $event->setResponse($result);
102 }
103 }
96025800 104
6550386a 105}