From dcef11bd7c0d5f3c304c2a741ffe14d08db3f7f6 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 21 Mar 2014 18:13:16 -0700 Subject: [PATCH] CRM-14370 - API Kernel - Extract APIv3SchemaAdapter --- Civi/API/Kernel.php | 14 +-- Civi/API/Subscriber/APIv3SchemaAdapter.php | 100 +++++++++++++++++++++ Civi/Core/Container.php | 1 + api/v3/utils.php | 26 ------ 4 files changed, 102 insertions(+), 39 deletions(-) create mode 100644 Civi/API/Subscriber/APIv3SchemaAdapter.php diff --git a/Civi/API/Kernel.php b/Civi/API/Kernel.php index f3010f0ab1..89ca67707c 100644 --- a/Civi/API/Kernel.php +++ b/Civi/API/Kernel.php @@ -73,6 +73,7 @@ class Kernel { $apiRequest['version'] = civicrm_get_api_version($params); $apiRequest['params'] = $params; $apiRequest['extra'] = $extra; + $apiRequest['fields'] = NULL; /** @var $apiWrappers array<\API_Wrapper> */ $apiWrappers = array( @@ -100,17 +101,6 @@ class Kernel { $apiRequest = $this->dispatcher->dispatch(Events::PREPARE, new PrepareEvent(NULL, $apiRequest))->getApiRequest(); - $fields = _civicrm_api3_api_getfields($apiRequest); - // we do this before we - _civicrm_api3_swap_out_aliases($apiRequest, $fields); - if (strtolower($action) != 'getfields') { - if (empty($apiRequest['params']['id'])) { - $apiRequest['params'] = array_merge(_civicrm_api3_getdefaults($apiRequest, $fields), $apiRequest['params']); - } - //if 'id' is set then only 'version' will be checked but should still be checked for consistency - civicrm_api3_verify_mandatory($apiRequest['params'], NULL, _civicrm_api3_getrequired($apiRequest, $fields)); - } - // For input filtering, process $apiWrappers in forward order foreach ($apiWrappers as $apiWrapper) { $apiRequest = $apiWrapper->fromApiInput($apiRequest); @@ -124,8 +114,6 @@ class Kernel { $result = $function($apiRequest); } elseif ($apiRequest['function'] && !$apiRequest['is_generic']) { - _civicrm_api3_validate_fields($apiRequest['entity'], $apiRequest['action'], $apiRequest['params'], $fields); - $result = isset($extra) ? $function($apiRequest['params'], $extra) : $function($apiRequest['params']); } else { diff --git a/Civi/API/Subscriber/APIv3SchemaAdapter.php b/Civi/API/Subscriber/APIv3SchemaAdapter.php new file mode 100644 index 0000000000..a534d20836 --- /dev/null +++ b/Civi/API/Subscriber/APIv3SchemaAdapter.php @@ -0,0 +1,100 @@ + array( + array('onApiPrepare', Events::W_MIDDLE), + array('onApiPrepare_validate', Events::W_LATE), + ), + ); + } + + public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { + $apiRequest = $event->getApiRequest(); + + $apiRequest['fields'] = _civicrm_api3_api_getfields($apiRequest); + + _civicrm_api3_swap_out_aliases($apiRequest, $apiRequest['fields']); + if (strtolower($apiRequest['action']) != 'getfields') { + if (empty($apiRequest['params']['id'])) { + $apiRequest['params'] = array_merge($this->getDefaults($apiRequest['fields']), $apiRequest['params']); + } + //if 'id' is set then only 'version' will be checked but should still be checked for consistency + civicrm_api3_verify_mandatory($apiRequest['params'], NULL, $this->getRequired($apiRequest['fields'])); + } + + $event->setApiRequest($apiRequest); + } + + public function onApiPrepare_validate(\Civi\API\Event\Event $event) { + $apiRequest = $event->getApiRequest(); + // Not sure why this is omitted for generic actions. It would make sense to omit 'getfields', but that's only one generic action. + + if (isset($apiRequest['function']) && !$apiRequest['is_generic'] && isset($apiRequest['fields'])) { + _civicrm_api3_validate_fields($apiRequest['entity'], $apiRequest['action'], $apiRequest['params'], $apiRequest['fields']); + $event->setApiRequest($apiRequest); + } + } + + /** + * Return array of defaults for the given API (function is a wrapper on getfields) + */ + public function getDefaults($fields) { + $defaults = array(); + + foreach ($fields as $field => $values) { + if (isset($values['api.default'])) { + $defaults[$field] = $values['api.default']; + } + } + return $defaults; + } + + /** + * Return array of required fields for the given API (function is a wrapper on getfields) + */ + public function getRequired($fields) { + $required = array('version'); + + foreach ($fields as $field => $values) { + if (!empty($values['api.required'])) { + $required[] = $field; + } + } + return $required; + } +} \ No newline at end of file diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 2eeec48fdd..6c22738f2d 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -88,6 +88,7 @@ class Container { public function createApiKernel($dispatcher) { $dispatcher->addSubscriber(new \Civi\API\Subscriber\TransactionSubscriber()); $dispatcher->addSubscriber(new \Civi\API\Subscriber\I18nSubscriber()); + $dispatcher->addSubscriber(new \Civi\API\Subscriber\APIv3SchemaAdapter()); $dispatcher->addSubscriber(new \Civi\API\Subscriber\XDebugSubscriber()); $dispatcher->addListener(\Civi\API\Events::AUTHORIZE, function(\Civi\API\Event\AuthorizeEvent $event) { $apiRequest = $event->getApiRequest(); diff --git a/api/v3/utils.php b/api/v3/utils.php index 250a4cb300..860668a1b6 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1543,33 +1543,7 @@ function _getStandardTypeFromCustomDataType($dataType) { ); return $mapping[$dataType]; } -/** - * Return array of defaults for the given API (function is a wrapper on getfields) - */ -function _civicrm_api3_getdefaults($apiRequest, $fields) { - $defaults = array(); - - foreach ($fields as $field => $values) { - if (isset($values['api.default'])) { - $defaults[$field] = $values['api.default']; - } - } - return $defaults; -} -/** - * Return array of defaults for the given API (function is a wrapper on getfields) - */ -function _civicrm_api3_getrequired($apiRequest, $fields) { - $required = array('version'); - - foreach ($fields as $field => $values) { - if (!empty($values['api.required'])) { - $required[] = $field; - } - } - return $required; -} /** * Fill params array with alternate (alias) values where a field has an alias and that is filled & the main field isn't -- 2.25.1