Merge pull request #16753 from agh1/consolidated-php-version
[civicrm-core.git] / Civi / API / Subscriber / XDebugSubscriber.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\API\Subscriber;
13
14 use Civi\API\Events;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17 /**
18 * Class XDebugSubscriber
19 * @package Civi\API\Subscriber
20 */
21 class XDebugSubscriber implements EventSubscriberInterface {
22
23 /**
24 * @return array
25 */
26 public static function getSubscribedEvents() {
27 return [
28 Events::RESPOND => ['onApiRespond', Events::W_LATE],
29 ];
30 }
31
32 /**
33 * @param \Civi\API\Event\RespondEvent $event
34 * API response event.
35 */
36 public function onApiRespond(\Civi\API\Event\RespondEvent $event) {
37 $apiRequest = $event->getApiRequest();
38 $result = $event->getResponse();
39 if (
40 function_exists('xdebug_time_index')
41 && !empty($apiRequest['params']['debug'])
42 && (empty($apiRequest['params']['check_permissions']) || \CRM_Core_Permission::check('view debug output'))
43 ) {
44 if (is_a($result, '\Civi\Api4\Generic\Result')) {
45 $result->debug = $result->debug ?? [];
46 $debug =& $result->debug;
47 }
48 // result would not be an array for api3 getvalue
49 elseif (is_array($result)) {
50 $result['xdebug'] = $result['xdebug'] ?? [];
51 $debug =& $result['xdebug'];
52 }
53 else {
54 return;
55 }
56 $debug['peakMemory'] = xdebug_peak_memory_usage();
57 $debug['memory'] = xdebug_memory_usage();
58 $debug['timeIndex'] = xdebug_time_index();
59 $event->setResponse($result);
60 }
61 }
62
63 }