3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 use Civi\API\Event\AuthorizeEvent
;
30 use Civi\API\Event\PrepareEvent
;
31 use Civi\API\Event\ExceptionEvent
;
32 use Civi\API\Event\ResolveEvent
;
33 use Civi\API\Event\RespondEvent
;
38 * @copyright CiviCRM LLC (c) 2004-2013
44 * @var \Symfony\Component\EventDispatcher\EventDispatcher
46 protected $dispatcher;
49 * @var array<APIProviderInterface>
51 protected $apiProviders;
53 function __construct($dispatcher, $apiProviders = array()) {
54 $this->apiProviders
= $apiProviders;
55 $this->dispatcher
= $dispatcher;
59 * @param string $entity
60 * type of entities to deal with
61 * @param string $action
62 * create, get, delete or some special action name.
63 * @param array $params
64 * array to be passed to function
69 public function run($entity, $action, $params, $extra) {
71 * @var $apiProvider \Civi\API\Provider\ProviderInterface|NULL
75 $apiRequest = $this->createRequest($entity, $action, $params, $extra);
78 if (!is_array($params)) {
79 throw new \
API_Exception('Input variable `params` is not an array', 2000);
83 $errorScope = \CRM_Core_TemporaryErrorScope
::useException();
85 $resolveEvent = $this->dispatcher
->dispatch(Events
::RESOLVE
, new ResolveEvent($apiRequest));
86 $apiRequest = $resolveEvent->getApiRequest();
87 $apiProvider = $resolveEvent->getApiProvider();
89 throw new \
API_Exception("API (" . $apiRequest['entity'] . ", " . $apiRequest['action'] . ") does not exist (join the API team and implement it!)");
92 if (! $this->dispatcher
->dispatch(Events
::AUTHORIZE
, new AuthorizeEvent($apiProvider, $apiRequest))->isAuthorized()) {
93 throw new \
API_Exception("Authorization failed", 'unauthorized');
96 $apiRequest = $this->dispatcher
->dispatch(Events
::PREPARE
, new PrepareEvent($apiProvider, $apiRequest))->getApiRequest();
97 $result = $apiProvider->invoke($apiRequest);
99 if (\CRM_Utils_Array
::value('is_error', $result, 0) == 0) {
100 _civicrm_api_call_nested_api($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']);
103 $responseEvent = $this->dispatcher
->dispatch(Events
::RESPOND
, new RespondEvent($apiProvider, $apiRequest, $result));
104 return $this->formatResult($apiRequest, $responseEvent->getResponse());
106 catch (\Exception
$e) {
107 $this->dispatcher
->dispatch(Events
::EXCEPTION
, new ExceptionEvent($e, $apiProvider, $apiRequest));
109 if ($e instanceof \PEAR_Exception
) {
110 $err = $this->formatPearException($e, $apiRequest);
111 } elseif ($e instanceof \API_Exception
) {
112 $err = $this->formatApiException($e, $apiRequest);
114 $err = $this->formatException($e, $apiRequest);
117 return $this->formatResult($apiRequest, $err);
123 * Create a formatted/normalized request object.
125 * @param string $entity
126 * @param string $action
127 * @param array $params
128 * @param mixed $extra
129 * @return array the request descriptor
131 public function createRequest($entity, $action, $params, $extra) {
132 $apiRequest = array();
133 $apiRequest['entity'] = \CRM_Utils_String
::munge($entity);
134 $apiRequest['action'] = \CRM_Utils_String
::munge($action);
135 $apiRequest['version'] = civicrm_get_api_version($params);
136 $apiRequest['params'] = $params;
137 $apiRequest['extra'] = $extra;
138 $apiRequest['fields'] = NULL;
142 public function boot() {
143 require_once ('api/v3/utils.php');
144 require_once 'api/Exception.php';
145 _civicrm_api3_initialize();
149 * @param \Exception $e
150 * @param array $apiRequest
151 * @return array (API response)
153 public function formatException($e, $apiRequest) {
155 if (!empty($apiRequest['params']['debug'])) {
156 $data['trace'] = $e->getTraceAsString();
158 return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode());
162 * @param \API_Exception $e
163 * @param array $apiRequest
164 * @return array (API response)
166 public function formatApiException($e, $apiRequest) {
167 $data = $e->getExtraParams();
168 $data['entity'] = \CRM_Utils_Array
::value('entity', $apiRequest);
169 $data['action'] = \CRM_Utils_Array
::value('action', $apiRequest);
171 if (\CRM_Utils_Array
::value('debug', \CRM_Utils_Array
::value('params', $apiRequest))
172 && empty($data['trace']) // prevent recursion
174 $data['trace'] = $e->getTraceAsString();
177 return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode());
181 * @param \PEAR_Exception $e
182 * @param array $apiRequest
183 * @return array (API response)
185 public function formatPearException($e, $apiRequest) {
187 $error = $e->getCause();
188 if ($error instanceof \DB_Error
) {
189 $data["error_code"] = \DB
::errorMessage($error->getCode());
190 $data["sql"] = $error->getDebugInfo();
192 if (!empty($apiRequest['params']['debug'])) {
193 if (method_exists($e, 'getUserInfo')) {
194 $data['debug_info'] = $error->getUserInfo();
196 if (method_exists($e, 'getExtraData')) {
197 $data['debug_info'] = $data +
$error->getExtraData();
199 $data['trace'] = $e->getTraceAsString();
202 $data['tip'] = "add debug=1 to your API call to have more info about the error";
205 return civicrm_api3_create_error($e->getMessage(), $data, $apiRequest);
211 public function formatResult($apiRequest, $result) {
212 if (isset($apiRequest, $apiRequest['params'])) {
213 if (isset($apiRequest['params']['format.is_success']) && $apiRequest['params']['format.is_success'] == 1) {
214 return (empty($result['is_error'])) ?
1 : 0;
217 if (!empty($apiRequest['params']['format.only_id']) && isset($result['id'])) {
219 return $result['id'];