Merge pull request #16837 from tunbola/case-api-case-clients
[civicrm-core.git] / Civi / API / Subscriber / XDebugSubscriber.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 Civi\API\Events;
15use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
6550386a
EM
17/**
18 * Class XDebugSubscriber
19 * @package Civi\API\Subscriber
20 */
0661f62b 21class XDebugSubscriber implements EventSubscriberInterface {
34f3bbd9 22
6550386a
EM
23 /**
24 * @return array
25 */
0661f62b 26 public static function getSubscribedEvents() {
c64f69d9
CW
27 return [
28 Events::RESPOND => ['onApiRespond', Events::W_LATE],
29 ];
0661f62b
TO
30 }
31
6550386a
EM
32 /**
33 * @param \Civi\API\Event\RespondEvent $event
8882ff5c 34 * API response event.
6550386a 35 */
8882ff5c 36 public function onApiRespond(\Civi\API\Event\RespondEvent $event) {
0661f62b
TO
37 $apiRequest = $event->getApiRequest();
38 $result = $event->getResponse();
b65fa6dc
CW
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'))
0661f62b 43 ) {
b65fa6dc
CW
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();
0661f62b
TO
59 $event->setResponse($result);
60 }
61 }
96025800 62
6550386a 63}