Commit | Line | Data |
---|---|---|
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 | |
12 | namespace Civi\API\Subscriber; | |
46bcf597 | 13 | |
0661f62b TO |
14 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
15 | ||
6550386a EM |
16 | /** |
17 | * Class XDebugSubscriber | |
18 | * @package Civi\API\Subscriber | |
19 | */ | |
2aafb0fc CW |
20 | class DebugSubscriber implements EventSubscriberInterface { |
21 | ||
22 | /** | |
23 | * @var \Civi\API\LogObserver | |
24 | */ | |
25 | private $debugLog; | |
34f3bbd9 | 26 | |
6550386a EM |
27 | /** |
28 | * @return array | |
29 | */ | |
0661f62b | 30 | public static function getSubscribedEvents() { |
c64f69d9 | 31 | return [ |
39b870b8 TO |
32 | 'civi.api.prepare' => ['onApiPrepare', 999], |
33 | 'civi.api.respond' => ['onApiRespond', -999], | |
c64f69d9 | 34 | ]; |
0661f62b TO |
35 | } |
36 | ||
2aafb0fc CW |
37 | public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { |
38 | $apiRequest = $event->getApiRequest(); | |
39 | if (!isset($this->debugLog) | |
40 | && !empty($apiRequest['params']['debug']) | |
41 | && (empty($apiRequest['params']['check_permissions']) || \CRM_Core_Permission::check('view debug output')) | |
42 | ) { | |
43 | $this->debugLog = new \Civi\API\LogObserver(); | |
44 | \CRM_Core_Error::createDebugLogger()->attach($this->debugLog); | |
45 | } | |
46 | } | |
47 | ||
6550386a EM |
48 | /** |
49 | * @param \Civi\API\Event\RespondEvent $event | |
8882ff5c | 50 | * API response event. |
6550386a | 51 | */ |
8882ff5c | 52 | public function onApiRespond(\Civi\API\Event\RespondEvent $event) { |
0661f62b TO |
53 | $apiRequest = $event->getApiRequest(); |
54 | $result = $event->getResponse(); | |
2aafb0fc | 55 | if (!empty($apiRequest['params']['debug']) |
b65fa6dc | 56 | && (empty($apiRequest['params']['check_permissions']) || \CRM_Core_Permission::check('view debug output')) |
0661f62b | 57 | ) { |
b65fa6dc CW |
58 | if (is_a($result, '\Civi\Api4\Generic\Result')) { |
59 | $result->debug = $result->debug ?? []; | |
60 | $debug =& $result->debug; | |
61 | } | |
62 | // result would not be an array for api3 getvalue | |
63 | elseif (is_array($result)) { | |
64 | $result['xdebug'] = $result['xdebug'] ?? []; | |
65 | $debug =& $result['xdebug']; | |
66 | } | |
67 | else { | |
68 | return; | |
69 | } | |
2aafb0fc CW |
70 | if (isset($this->debugLog) && $this->debugLog->getMessages()) { |
71 | $debug['log'] = $this->debugLog->getMessages(); | |
72 | } | |
73 | if (function_exists('xdebug_time_index')) { | |
74 | $debug['peakMemory'] = xdebug_peak_memory_usage(); | |
75 | $debug['memory'] = xdebug_memory_usage(); | |
76 | $debug['timeIndex'] = xdebug_time_index(); | |
77 | } | |
0661f62b TO |
78 | $event->setResponse($result); |
79 | } | |
80 | } | |
96025800 | 81 | |
6550386a | 82 | } |