From 0a946de26c2e00af77e3593954f9edf248dfed14 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 6 Apr 2014 02:24:08 -0700 Subject: [PATCH] CRM-14370 - Extract ChainSubscriber; move helper function This allows us to change the timing of chained invocation, positioning the it after WrapperAdapter but before TransactionSubscriber (like it was originally). This fixes a regression in api_v3_OptionGroupTest::testGetOptionCreateSuccess --- Civi/API/Kernel.php | 4 - Civi/API/Subscriber/ChainSubscriber.php | 169 ++++++++++++++++++++++++ Civi/API/Subscriber/WrapperAdapter.php | 2 +- Civi/Core/Container.php | 1 + api/api.php | 102 -------------- 5 files changed, 171 insertions(+), 107 deletions(-) create mode 100644 Civi/API/Subscriber/ChainSubscriber.php diff --git a/Civi/API/Kernel.php b/Civi/API/Kernel.php index 09076e554d..c8dacea8fc 100644 --- a/Civi/API/Kernel.php +++ b/Civi/API/Kernel.php @@ -90,10 +90,6 @@ class Kernel { $apiRequest = $this->prepare($apiProvider, $apiRequest); $result = $apiProvider->invoke($apiRequest); - if (\CRM_Utils_Array::value('is_error', $result, 0) == 0) { - _civicrm_api_call_nested_api($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']); - } - $apiResponse = $this->respond($apiProvider, $apiRequest, $result); return $this->formatResult($apiRequest, $apiResponse); } diff --git a/Civi/API/Subscriber/ChainSubscriber.php b/Civi/API/Subscriber/ChainSubscriber.php new file mode 100644 index 0000000000..5c4549feff --- /dev/null +++ b/Civi/API/Subscriber/ChainSubscriber.php @@ -0,0 +1,169 @@ + 3, + * 'first_name' => 'Amy', + * 'api.Email.create' => array( + * 'email' => 'amy@example.com', + * 'location_type_id' => 123, + * ), + * )); + * @endcode + * + * The ChainSubscriber looks for any parameters of the form "api.Email.create"; if found, it issues the nested + * API call (and passes some extra context -- eg Amy's contact_id). + */ +class ChainSubscriber implements EventSubscriberInterface { + public static function getSubscribedEvents() { + return array( + Events::RESPOND => array('onApiRespond', Events::W_EARLY), + ); + } + + public function onApiRespond(\Civi\API\Event\RespondEvent $event) { + $apiRequest = $event->getApiRequest(); + $result = $event->getResponse(); + if (\CRM_Utils_Array::value('is_error', $result, 0) == 0) { + $this->_civicrm_api_call_nested_api($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']); + $event->setResponse($result); + } + } + + /** + * Call any nested api calls + * + * TODO: We don't really need this to be a separate function. + */ + protected function _civicrm_api_call_nested_api(&$params, &$result, $action, $entity, $version) { + $entity = _civicrm_api_get_entity_name_from_camel($entity); + + //we don't need to worry about nested api in the getfields/getoptions actions, so just return immediately + if (in_array(strtolower($action), array('getfields', 'getoptions'))) { + return; + } + + if (strtolower($action) == 'getsingle') { + // I don't understand the protocol here, but we don't want + // $result to be a recursive array + // $result['values'][0] = $result; + $oldResult = $result; + $result = array('values' => array(0 => $oldResult)); + } + foreach ($params as $field => $newparams) { + if ((is_array($newparams) || $newparams === 1) && $field <> 'api.has_parent' && substr($field, 0, 3) == 'api') { + + // 'api.participant.delete' => 1 is a valid options - handle 1 instead of an array + if ($newparams === 1) { + $newparams = array('version' => $version); + } + // can be api_ or api. + $separator = $field[3]; + if (!($separator == '.' || $separator == '_')) { + continue; + } + $subAPI = explode($separator, $field); + + $subaction = empty($subAPI[2]) ? $action : $subAPI[2]; + $subParams = array( + 'debug' => \CRM_Utils_Array::value('debug', $params), + ); + $subEntity = $subAPI[1]; + + foreach ($result['values'] as $idIndex => $parentAPIValues) { + + if (strtolower($subEntity) != 'contact') { + //contact spits the dummy at activity_id so what else won't it like? + //set entity_id & entity table based on the parent's id & entity. e.g for something like + //note if the parent call is contact 'entity_table' will be set to 'contact' & 'id' to the contact id from + //the parent call. + //in this case 'contact_id' will also be set to the parent's id + $subParams["entity_id"] = $parentAPIValues['id']; + $subParams['entity_table'] = 'civicrm_' . _civicrm_api_get_entity_name_from_camel($entity); + $subParams[strtolower($entity) . "_id"] = $parentAPIValues['id']; + } + if (strtolower($entity) != 'contact' && \CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) { + //e.g. if event_id is in the values returned & subentity is event then pass in event_id as 'id' + //don't do this for contact as it does some wierd things like returning primary email & + //thus limiting the ability to chain email + //TODO - this might need the camel treatment + $subParams['id'] = $parentAPIValues[$subEntity . "_id"]; + } + + if (\CRM_Utils_Array::value('entity_table', $result['values'][$idIndex]) == $subEntity) { + $subParams['id'] = $result['values'][$idIndex]['entity_id']; + } + // if we are dealing with the same entity pass 'id' through (useful for get + delete for example) + if (strtolower($entity) == strtolower($subEntity)) { + $subParams['id'] = $result['values'][$idIndex]['id']; + } + + + $subParams['version'] = $version; + if (!empty($params['check_permissions'])) { + $subParams['check_permissions'] = $params['check_permissions']; + } + $subParams['sequential'] = 1; + $subParams['api.has_parent'] = 1; + if (array_key_exists(0, $newparams)) { + $genericParams = $subParams; + // it is a numerically indexed array - ie. multiple creates + foreach ($newparams as $entityparams) { + $subParams = array_merge($genericParams, $entityparams); + _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); + $result['values'][$result['id']][$field][] = civicrm_api($subEntity, $subaction, $subParams); + if ($result['is_error'] === 1) { + throw new \Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); + } + } + } + else { + + $subParams = array_merge($subParams, $newparams); + _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); + $result['values'][$idIndex][$field] = civicrm_api($subEntity, $subaction, $subParams); + if (!empty($result['is_error'])) { + throw new \Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); + } + } + } + } + } + if (strtolower($action) == 'getsingle') { + $result = $result['values'][0]; + } + } + +} \ No newline at end of file diff --git a/Civi/API/Subscriber/WrapperAdapter.php b/Civi/API/Subscriber/WrapperAdapter.php index b94da6cfe4..9ea309dee0 100644 --- a/Civi/API/Subscriber/WrapperAdapter.php +++ b/Civi/API/Subscriber/WrapperAdapter.php @@ -39,7 +39,7 @@ class WrapperAdapter implements EventSubscriberInterface { public static function getSubscribedEvents() { return array( Events::PREPARE => array('onApiPrepare', Events::W_MIDDLE), - Events::RESPOND => array('onApiRespond', Events::W_EARLY), + Events::RESPOND => array('onApiRespond', Events::W_EARLY * 2), ); } diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index a9923a8fa3..e251f2adf9 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -91,6 +91,7 @@ class Container { * @return \Civi\API\Kernel */ public function createApiKernel($dispatcher, $magicFunctionProvider) { + $dispatcher->addSubscriber(new \Civi\API\Subscriber\ChainSubscriber()); $dispatcher->addSubscriber(new \Civi\API\Subscriber\TransactionSubscriber()); $dispatcher->addSubscriber(new \Civi\API\Subscriber\I18nSubscriber()); $dispatcher->addSubscriber($magicFunctionProvider); diff --git a/api/api.php b/api/api.php index 390dda1f63..893805895f 100644 --- a/api/api.php +++ b/api/api.php @@ -110,108 +110,6 @@ function _civicrm_api_get_camel_name($entity, $version = NULL) { return implode('', $fragments); } -/** - * Call any nested api calls - */ -function _civicrm_api_call_nested_api(&$params, &$result, $action, $entity, $version) { - $entity = _civicrm_api_get_entity_name_from_camel($entity); - - //we don't need to worry about nested api in the getfields/getoptions actions, so just return immediately - if (in_array(strtolower($action), array('getfields', 'getoptions'))) { - return; - } - - if(strtolower($action) == 'getsingle'){ - // I don't understand the protocol here, but we don't want - // $result to be a recursive array - // $result['values'][0] = $result; - $oldResult = $result; - $result = array('values' => array(0 => $oldResult)); - } - foreach ($params as $field => $newparams) { - if ((is_array($newparams) || $newparams === 1) && $field <> 'api.has_parent' && substr($field, 0, 3) == 'api') { - - // 'api.participant.delete' => 1 is a valid options - handle 1 instead of an array - if ($newparams === 1) { - $newparams = array('version' => $version); - } - // can be api_ or api. - $separator = $field[3]; - if (!($separator == '.' || $separator == '_')) { - continue; - } - $subAPI = explode($separator, $field); - - $subaction = empty($subAPI[2]) ? $action : $subAPI[2]; - $subParams = array( - 'debug' => CRM_Utils_Array::value('debug', $params), - ); - $subEntity = $subAPI[1]; - - foreach ($result['values'] as $idIndex => $parentAPIValues) { - - if (strtolower($subEntity) != 'contact') { - //contact spits the dummy at activity_id so what else won't it like? - //set entity_id & entity table based on the parent's id & entity. e.g for something like - //note if the parent call is contact 'entity_table' will be set to 'contact' & 'id' to the contact id from - //the parent call. - //in this case 'contact_id' will also be set to the parent's id - $subParams["entity_id"] = $parentAPIValues['id']; - $subParams['entity_table'] = 'civicrm_' . _civicrm_api_get_entity_name_from_camel($entity); - $subParams[strtolower($entity) . "_id"] = $parentAPIValues['id']; - } - if (strtolower($entity) != 'contact' && CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) { - //e.g. if event_id is in the values returned & subentity is event then pass in event_id as 'id' - //don't do this for contact as it does some wierd things like returning primary email & - //thus limiting the ability to chain email - //TODO - this might need the camel treatment - $subParams['id'] = $parentAPIValues[$subEntity . "_id"]; - } - - if (CRM_Utils_Array::value('entity_table', $result['values'][$idIndex]) == $subEntity) { - $subParams['id'] = $result['values'][$idIndex]['entity_id']; - } - // if we are dealing with the same entity pass 'id' through (useful for get + delete for example) - if (strtolower($entity) == strtolower($subEntity)) { - $subParams['id'] = $result['values'][$idIndex]['id']; - } - - - $subParams['version'] = $version; - if(!empty($params['check_permissions'])){ - $subParams['check_permissions'] = $params['check_permissions']; - } - $subParams['sequential'] = 1; - $subParams['api.has_parent'] = 1; - if (array_key_exists(0, $newparams)) { - $genericParams = $subParams; - // it is a numerically indexed array - ie. multiple creates - foreach ($newparams as $entityparams) { - $subParams = array_merge($genericParams, $entityparams); - _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); - $result['values'][$result['id']][$field][] = civicrm_api($subEntity, $subaction, $subParams); - if ($result['is_error'] === 1) { - throw new Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); - } - } - } - else { - - $subParams = array_merge($subParams, $newparams); - _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); - $result['values'][$idIndex][$field] = civicrm_api($subEntity, $subaction, $subParams); - if (!empty($result['is_error'])) { - throw new Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); - } - } - } - } - } - if(strtolower($action) == 'getsingle'){ - $result = $result['values'][0]; - } -} - /** * Swap out any $values vars - ie. the value after $value is swapped for the parent $result * 'activity_type_id' => '$value.testfield', -- 2.25.1