From 32f72d83311fd169daa810f7a2eccebe1f304717 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 28 Jan 2020 17:12:29 -0500 Subject: [PATCH] Api4 - add more debug output --- Civi/Api4/Generic/AbstractAction.php | 29 ++++++++++++++++++++++++- Civi/Api4/Generic/BasicBatchAction.php | 1 + Civi/Api4/Generic/BasicCreateAction.php | 1 + Civi/Api4/Generic/BasicGetAction.php | 1 + Civi/Api4/Generic/BasicSaveAction.php | 1 + Civi/Api4/Generic/BasicUpdateAction.php | 1 + 6 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Civi/Api4/Generic/AbstractAction.php b/Civi/Api4/Generic/AbstractAction.php index 4168537718..31a9fd4a29 100644 --- a/Civi/Api4/Generic/AbstractAction.php +++ b/Civi/Api4/Generic/AbstractAction.php @@ -75,6 +75,11 @@ abstract class AbstractAction implements \ArrayAccess { /** * Add debugging info to the api result. * + * When enabled, the $result->debug will be populated with information about the api call, + * including sql queries executed. + * + * Note: with checkPermissions enabled, debug info will only be returned if the user has "view debug output" permission. + * * @var bool */ protected $debug = FALSE; @@ -116,7 +121,7 @@ abstract class AbstractAction implements \ArrayAccess { */ private $_id; - protected $_debugOutput = []; + public $_debugOutput = []; /** * Action constructor. @@ -213,6 +218,7 @@ abstract class AbstractAction implements \ArrayAccess { $kernel = \Civi::service('civi_api_kernel'); $result = $kernel->runRequest($this); if ($this->debug && (!$this->checkPermissions || \CRM_Core_Permission::check('view debug output'))) { + $result->debug['actionClass'] = get_class($this); $result->debug = array_merge($result->debug, $this->_debugOutput); } else { @@ -465,4 +471,25 @@ abstract class AbstractAction implements \ArrayAccess { return (bool) trim(\CRM_Core_Smarty::singleton()->fetchWith('string:' . $tpl, $vars)); } + /** + * When in debug mode, this logs the callback function being used by a Basic*Action class. + * + * @param callable $callable + */ + protected function addCallbackToDebugOutput($callable) { + if ($this->debug && empty($this->_debugOutput['callback'])) { + if (is_scalar($callable)) { + $this->_debugOutput['callback'] = (string) $callable; + } + elseif (is_array($callable)) { + foreach ($callable as $key => $unit) { + $this->_debugOutput['callback'][$key] = is_object($unit) ? get_class($unit) : (string) $unit; + } + } + elseif (is_object($callable)) { + $this->_debugOutput['callback'] = get_class($callable); + } + } + } + } diff --git a/Civi/Api4/Generic/BasicBatchAction.php b/Civi/Api4/Generic/BasicBatchAction.php index 5a342dbab7..259b68cbe8 100644 --- a/Civi/Api4/Generic/BasicBatchAction.php +++ b/Civi/Api4/Generic/BasicBatchAction.php @@ -84,6 +84,7 @@ class BasicBatchAction extends AbstractBatchAction { */ protected function doTask($item) { if (is_callable($this->doer)) { + $this->addCallbackToDebugOutput($this->doer); return call_user_func($this->doer, $item, $this); } throw new NotImplementedException('Doer function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); diff --git a/Civi/Api4/Generic/BasicCreateAction.php b/Civi/Api4/Generic/BasicCreateAction.php index 9fa2d1673b..ff26bd315c 100644 --- a/Civi/Api4/Generic/BasicCreateAction.php +++ b/Civi/Api4/Generic/BasicCreateAction.php @@ -75,6 +75,7 @@ class BasicCreateAction extends AbstractCreateAction { */ protected function writeRecord($item) { if (is_callable($this->setter)) { + $this->addCallbackToDebugOutput($this->setter); return call_user_func($this->setter, $item, $this); } throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); diff --git a/Civi/Api4/Generic/BasicGetAction.php b/Civi/Api4/Generic/BasicGetAction.php index a9357bb21d..67f3b0a0ed 100644 --- a/Civi/Api4/Generic/BasicGetAction.php +++ b/Civi/Api4/Generic/BasicGetAction.php @@ -96,6 +96,7 @@ class BasicGetAction extends AbstractGetAction { */ protected function getRecords() { if (is_callable($this->getter)) { + $this->addCallbackToDebugOutput($this->getter); return call_user_func($this->getter, $this); } throw new NotImplementedException('Getter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); diff --git a/Civi/Api4/Generic/BasicSaveAction.php b/Civi/Api4/Generic/BasicSaveAction.php index 9eef7c4346..db08fb95a6 100644 --- a/Civi/Api4/Generic/BasicSaveAction.php +++ b/Civi/Api4/Generic/BasicSaveAction.php @@ -90,6 +90,7 @@ class BasicSaveAction extends AbstractSaveAction { */ protected function writeRecord($item) { if (is_callable($this->setter)) { + $this->addCallbackToDebugOutput($this->setter); return call_user_func($this->setter, $item, $this); } throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); diff --git a/Civi/Api4/Generic/BasicUpdateAction.php b/Civi/Api4/Generic/BasicUpdateAction.php index 8867523e48..db80d0423e 100644 --- a/Civi/Api4/Generic/BasicUpdateAction.php +++ b/Civi/Api4/Generic/BasicUpdateAction.php @@ -84,6 +84,7 @@ class BasicUpdateAction extends AbstractUpdateAction { */ protected function writeRecord($item) { if (is_callable($this->setter)) { + $this->addCallbackToDebugOutput($this->setter); return call_user_func($this->setter, $item, $this); } throw new NotImplementedException('Setter function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); -- 2.25.1