$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);
$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
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']);
}
--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.4 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2013 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+*/
+
+namespace Civi\API\Subscriber;
+use Civi\API\Events;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * This is a wrapper for the legacy "API Wrapper" interface which allows
+ * wrappers to run through the new kernel. It translates from dispatcher events
+ * ('api.prepare', 'api.respond') to wrapper calls ('fromApiInput', 'toApiOutput').
+ */
+class WrapperAdapter implements EventSubscriberInterface {
+
+ public static function getSubscribedEvents() {
+ return array(
+ Events::PREPARE => 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
$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();