From 3a8dc2286dda0c8adcf3f73ad5de12aabe4bd2ee Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 12 Feb 2020 11:24:13 -0500 Subject: [PATCH] APIv4 - merge ActionUtil with Request::create The Request::create function was broken for v4, and ActionUtil had been serving the same purpose but in a non-broken way. --- Civi/API/Request.php | 60 ++++++++++------------ Civi/Api4/Action/GetActions.php | 3 +- Civi/Api4/Generic/AbstractAction.php | 16 +++--- Civi/Api4/Generic/BasicGetFieldsAction.php | 15 +++++- Civi/Api4/Generic/BasicReplaceAction.php | 4 +- Civi/Api4/Generic/BasicSaveAction.php | 3 +- Civi/Api4/Generic/DAOGetFieldsAction.php | 1 - Civi/Api4/Utils/ActionUtil.php | 46 ----------------- api/api.php | 6 +-- 9 files changed, 52 insertions(+), 102 deletions(-) delete mode 100644 Civi/Api4/Utils/ActionUtil.php diff --git a/Civi/API/Request.php b/Civi/API/Request.php index 11c32ffc5e..ccc970c117 100644 --- a/Civi/API/Request.php +++ b/Civi/API/Request.php @@ -27,46 +27,42 @@ class Request { * @param array $params * API parameters. * - * @throws \API_Exception - * @return array - * the request descriptor; keys: - * - version: int - * - entity: string - * - action: string - * - params: array (string $key => mixed $value) [deprecated in v4] - * - fields: NULL|array (string $key => array $fieldSpec) - * - options: \CRM_Utils_OptionBag derived from params [v4-only] - * - data: \CRM_Utils_OptionBag derived from params [v4-only] - * - chains: unspecified derived from params [v4-only] + * @throws \Civi\API\Exception\NotImplementedException + * @return \Civi\Api4\Generic\AbstractAction|array */ - public static function create($entity, $action, $params) { - $version = \CRM_Utils_Array::value('version', $params); - switch ($version) { - default: - $apiRequest = []; - $apiRequest['id'] = self::$nextId++; - $apiRequest['version'] = (int) $version; - $apiRequest['params'] = $params; - $apiRequest['fields'] = NULL; - $apiRequest['entity'] = self::normalizeEntityName($entity); - $apiRequest['action'] = self::normalizeActionName($action); - return $apiRequest; + public static function create(string $entity, string $action, array $params) { + switch ($params['version']) { + case 3: + return [ + 'id' => self::$nextId++, + 'version' => 3, + 'params' => $params, + 'fields' => NULL, + 'entity' => self::normalizeEntityName($entity), + 'action' => self::normalizeActionName($action), + ]; case 4: - $callable = ["Civi\\Api4\\$entity", $action]; - if (!is_callable($callable)) { - throw new Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)"); + // For custom pseudo-entities + if (strpos($entity, 'Custom_') === 0) { + $apiRequest = \Civi\Api4\CustomValue::$action(substr($entity, 7)); + } + else { + $callable = ["\\Civi\\Api4\\$entity", $action]; + if (!is_callable($callable)) { + throw new \Civi\API\Exception\NotImplementedException("API ($entity, $action) does not exist (join the API team and implement it!)"); + } + $apiRequest = call_user_func($callable); } - $apiCall = call_user_func($callable); - $apiRequest['id'] = self::$nextId++; - unset($params['version']); foreach ($params as $name => $param) { $setter = 'set' . ucfirst($name); - $apiCall->$setter($param); + $apiRequest->$setter($param); } - return $apiCall; - } + return $apiRequest; + default: + throw new \Civi\API\Exception\NotImplementedException("CiviCRM API version {$params['version']} not found."); + } } /** diff --git a/Civi/Api4/Action/GetActions.php b/Civi/Api4/Action/GetActions.php index b16fb0a285..a1d2b94132 100644 --- a/Civi/Api4/Action/GetActions.php +++ b/Civi/Api4/Action/GetActions.php @@ -23,7 +23,6 @@ namespace Civi\Api4\Action; use Civi\API\Exception\NotImplementedException; use Civi\Api4\Generic\BasicGetAction; -use Civi\Api4\Utils\ActionUtil; use Civi\Api4\Utils\ReflectionUtils; /** @@ -84,7 +83,7 @@ class GetActions extends BasicGetAction { private function loadAction($actionName, $method = NULL) { try { if (!isset($this->_actions[$actionName]) && (!$this->_actionsToGet || in_array($actionName, $this->_actionsToGet))) { - $action = ActionUtil::getAction($this->getEntityName(), $actionName); + $action = \Civi\API\Request::create($this->getEntityName(), $actionName, ['version' => 4]); if (is_object($action)) { $this->_actions[$actionName] = ['name' => $actionName]; if ($this->_isFieldSelected('description', 'comment', 'see')) { diff --git a/Civi/Api4/Generic/AbstractAction.php b/Civi/Api4/Generic/AbstractAction.php index 531f11aace..0de9856f63 100644 --- a/Civi/Api4/Generic/AbstractAction.php +++ b/Civi/Api4/Generic/AbstractAction.php @@ -21,7 +21,6 @@ namespace Civi\Api4\Generic; use Civi\Api4\Utils\ReflectionUtils; -use Civi\Api4\Utils\ActionUtil; /** * Base class for all api actions. @@ -424,15 +423,14 @@ abstract class AbstractAction implements \ArrayAccess { */ public function entityFields() { if (!$this->_entityFields) { - $getFields = ActionUtil::getAction($this->getEntityName(), 'getFields'); + $getFields = \Civi\API\Request::create($this->getEntityName(), 'getFields', [ + 'version' => 4, + 'checkPermissions' => $this->checkPermissions, + 'action' => $this->getActionName(), + 'includeCustom' => FALSE, + ]); $result = new Result(); - if (method_exists($this, 'getBaoName')) { - $getFields->setIncludeCustom(FALSE); - } - $getFields - ->setCheckPermissions($this->checkPermissions) - ->setAction($this->getActionName()) - ->_run($result); + $getFields->_run($result); $this->_entityFields = (array) $result->indexBy('name'); } return $this->_entityFields; diff --git a/Civi/Api4/Generic/BasicGetFieldsAction.php b/Civi/Api4/Generic/BasicGetFieldsAction.php index ae11f65f48..553d5bed22 100644 --- a/Civi/Api4/Generic/BasicGetFieldsAction.php +++ b/Civi/Api4/Generic/BasicGetFieldsAction.php @@ -22,7 +22,6 @@ namespace Civi\Api4\Generic; use Civi\API\Exception\NotImplementedException; -use Civi\Api4\Utils\ActionUtil; /** * Lists information about fields for the $ENTITY entity. @@ -78,7 +77,7 @@ class BasicGetFieldsAction extends BasicGetAction { */ public function _run(Result $result) { try { - $actionClass = ActionUtil::getAction($this->getEntityName(), $this->getAction()); + $actionClass = \Civi\API\Request::create($this->getEntityName(), $this->getAction(), ['version' => 4]); } catch (NotImplementedException $e) { } @@ -143,6 +142,18 @@ class BasicGetFieldsAction extends BasicGetAction { return $this; } + /** + * @param bool $includeCustom + * @return $this + */ + public function setIncludeCustom(bool $includeCustom) { + // Be forgiving if the param doesn't exist and don't throw an exception + if (property_exists($this, 'includeCustom')) { + $this->includeCustom = $includeCustom; + } + return $this; + } + public function fields() { return [ [ diff --git a/Civi/Api4/Generic/BasicReplaceAction.php b/Civi/Api4/Generic/BasicReplaceAction.php index b8e20285cd..cb06ec096e 100644 --- a/Civi/Api4/Generic/BasicReplaceAction.php +++ b/Civi/Api4/Generic/BasicReplaceAction.php @@ -21,8 +21,6 @@ namespace Civi\Api4\Generic; -use Civi\Api4\Utils\ActionUtil; - /** * Replaces an existing set of $ENTITIES with a new one. * @@ -100,7 +98,7 @@ class BasicReplaceAction extends AbstractBatchAction { $idField = $this->getSelect()[0]; $toDelete = array_diff_key(array_column($items, NULL, $idField), array_flip(array_filter(\CRM_Utils_Array::collect($idField, $this->records)))); - $saveAction = ActionUtil::getAction($this->getEntityName(), 'save'); + $saveAction = \Civi\API\Request::create($this->getEntityName(), 'save', ['version' => 4]); $saveAction ->setCheckPermissions($this->getCheckPermissions()) ->setReload($this->reload) diff --git a/Civi/Api4/Generic/BasicSaveAction.php b/Civi/Api4/Generic/BasicSaveAction.php index b5c998af56..a080fa6d82 100644 --- a/Civi/Api4/Generic/BasicSaveAction.php +++ b/Civi/Api4/Generic/BasicSaveAction.php @@ -22,7 +22,6 @@ namespace Civi\Api4\Generic; use Civi\API\Exception\NotImplementedException; -use Civi\Api4\Utils\ActionUtil; /** * $ACTION one or more $ENTITIES. @@ -68,7 +67,7 @@ class BasicSaveAction extends AbstractSaveAction { } if ($this->reload) { /** @var BasicGetAction $get */ - $get = ActionUtil::getAction($this->getEntityName(), 'get'); + $get = \Civi\API\Request::create($this->getEntityName(), 'get', ['version' => 4]); $get ->setCheckPermissions($this->getCheckPermissions()) ->addWhere($this->getIdField(), 'IN', (array) $result->column($this->getIdField())); diff --git a/Civi/Api4/Generic/DAOGetFieldsAction.php b/Civi/Api4/Generic/DAOGetFieldsAction.php index 992eca6707..7d7ac1849d 100644 --- a/Civi/Api4/Generic/DAOGetFieldsAction.php +++ b/Civi/Api4/Generic/DAOGetFieldsAction.php @@ -25,7 +25,6 @@ use Civi\Api4\Service\Spec\SpecFormatter; /** * @inheritDoc - * @method $this setIncludeCustom(bool $value) * @method bool getIncludeCustom() */ class DAOGetFieldsAction extends BasicGetFieldsAction { diff --git a/Civi/Api4/Utils/ActionUtil.php b/Civi/Api4/Utils/ActionUtil.php deleted file mode 100644 index 614f4de961..0000000000 --- a/Civi/Api4/Utils/ActionUtil.php +++ /dev/null @@ -1,46 +0,0 @@ - $param) { - $setter = 'set' . ucfirst($name); - $apiCall->$setter($param); - } + $apiCall = \Civi\API\Request::create($entity, $action, ['version' => 4] + $params); if ($index && is_array($index)) { $indexCol = reset($index); -- 2.25.1