From: Tim Otten Date: Sat, 22 Mar 2014 01:37:59 +0000 (-0700) Subject: CRM-14370 - API Kernel - Extract WrapperAdapter X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6d3bdc983eb8ee40a6a598b03a31259a93b21e14;p=civicrm-core.git CRM-14370 - API Kernel - Extract WrapperAdapter --- diff --git a/Civi/API/Kernel.php b/Civi/API/Kernel.php index 89ca67707c..ed13ecb280 100644 --- a/Civi/API/Kernel.php +++ b/Civi/API/Kernel.php @@ -75,15 +75,6 @@ class Kernel { $apiRequest['extra'] = $extra; $apiRequest['fields'] = NULL; - /** @var $apiWrappers array<\API_Wrapper> */ - $apiWrappers = array( - \CRM_Utils_API_HTMLInputCoder::singleton(), - \CRM_Utils_API_NullOutputCoder::singleton(), - \CRM_Utils_API_ReloadOption::singleton(), - \CRM_Utils_API_MatchOption::singleton(), - ); - \CRM_Utils_Hook::apiWrappers($apiWrappers, $apiRequest); - try { if (!is_array($params)) { throw new \API_Exception('Input variable `params` is not an array', 2000); @@ -101,11 +92,6 @@ class Kernel { $apiRequest = $this->dispatcher->dispatch(Events::PREPARE, new PrepareEvent(NULL, $apiRequest))->getApiRequest(); - // For input filtering, process $apiWrappers in forward order - foreach ($apiWrappers as $apiWrapper) { - $apiRequest = $apiWrapper->fromApiInput($apiRequest); - } - $function = $apiRequest['function']; if ($apiRequest['function'] && $apiRequest['is_generic']) { // Unlike normal API implementations, generic implementations require explicit @@ -120,11 +106,6 @@ class Kernel { throw new \API_Exception("API (" . $apiRequest['entity'] . ", " . $apiRequest['action'] . ") does not exist (join the API team and implement it!)"); } - // For output filtering, process $apiWrappers in reverse order - foreach (array_reverse($apiWrappers) as $apiWrapper) { - $result = $apiWrapper->toApiOutput($apiRequest, $result); - } - if (\CRM_Utils_Array::value('is_error', $result, 0) == 0) { _civicrm_api_call_nested_api($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']); } diff --git a/Civi/API/Subscriber/WrapperAdapter.php b/Civi/API/Subscriber/WrapperAdapter.php new file mode 100644 index 0000000000..b94da6cfe4 --- /dev/null +++ b/Civi/API/Subscriber/WrapperAdapter.php @@ -0,0 +1,89 @@ + array('onApiPrepare', Events::W_MIDDLE), + Events::RESPOND => array('onApiRespond', Events::W_EARLY), + ); + } + + /** + * @var array(\API_Wrapper) + */ + protected $defaults; + + function __construct($defaults = array()) { + $this->defaults = $defaults; + } + + public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { + $apiRequest = $event->getApiRequest(); + + // For input filtering, process $apiWrappers in forward order + foreach ($this->getWrappers($apiRequest) as $apiWrapper) { + $apiRequest = $apiWrapper->fromApiInput($apiRequest); + } + + $event->setApiRequest($apiRequest); + } + + public function onApiRespond(\Civi\API\Event\RespondEvent $event) { + $apiRequest = $event->getApiRequest(); + $result = $event->getResponse(); + + // For output filtering, process $apiWrappers in reverse order + foreach (array_reverse($this->getWrappers($apiRequest)) as $apiWrapper) { + $result = $apiWrapper->toApiOutput($apiRequest, $result); + } + + $event->setResponse($result); + } + + /** + * @param array $apiRequest + * @return array<\API_Wrapper> + */ + public function getWrappers($apiRequest) { + if (!isset($apiRequest['wrappers'])) { + $apiRequest['wrappers'] = $this->defaults; + \CRM_Utils_Hook::apiWrappers($apiRequest['wrappers'], $apiRequest); + } + return $apiRequest['wrappers']; + } +} \ No newline at end of file diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 6c22738f2d..c6019c3a29 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -89,6 +89,12 @@ class Container { $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\WrapperAdapter(array( + \CRM_Utils_API_HTMLInputCoder::singleton(), + \CRM_Utils_API_NullOutputCoder::singleton(), + \CRM_Utils_API_ReloadOption::singleton(), + \CRM_Utils_API_MatchOption::singleton(), + ))); $dispatcher->addSubscriber(new \Civi\API\Subscriber\XDebugSubscriber()); $dispatcher->addListener(\Civi\API\Events::AUTHORIZE, function(\Civi\API\Event\AuthorizeEvent $event) { $apiRequest = $event->getApiRequest();