/**
* @param \Civi\Core\CiviEventDispatcherInterface $dispatcher
- * The event dispatcher which receives kernel events.
- * @param array $apiProviders
- * Array of ProviderInterface.
+ * @param \Civi\API\Provider\ProviderInterface[] $apiProviders
*/
public function __construct($dispatcher, $apiProviders = []) {
$this->apiProviders = $apiProviders;
/**
* @param int $version
* API version.
- * @return array
- * Array<string>.
+ * @return string[]
*/
public function getEntityNames($version) {
// Question: Would it better to eliminate $this->apiProviders and just use $this->dispatcher?
$entityNames = [];
foreach ($this->getApiProviders() as $provider) {
- /** @var \Civi\API\Provider\ProviderInterface $provider */
$entityNames = array_merge($entityNames, $provider->getEntityNames($version));
}
$entityNames = array_unique($entityNames);
* API version.
* @param string $entity
* API entity.
- * @return array
- * Array<string>
+ * @return string[]
*/
public function getActionNames($version, $entity) {
// Question: Would it better to eliminate $this->apiProviders and just use $this->dispatcher?
$actionNames = [];
foreach ($this->getApiProviders() as $provider) {
- /** @var \Civi\API\Provider\ProviderInterface $provider */
$actionNames = array_merge($actionNames, $provider->getActionNames($version, $entity));
}
$actionNames = array_unique($actionNames);
if (!empty($apiRequest['params']['debug'])) {
$data['trace'] = $e->getTraceAsString();
}
- return $this->createError($e->getMessage(), $data, $apiRequest, $e->getCode());
+ return $this->createError($e->getMessage(), $data, $apiRequest);
}
/**
*/
public function formatApiException($e, $apiRequest) {
$data = $e->getExtraParams();
- $errorCode = $e->getCode();
if (($data['exception'] ?? NULL) instanceof \DB_Error) {
- $errorCode = $e->getDBErrorMessage();
$data['sql'] = $e->getSQL();
$data['debug_info'] = $e->getUserInfo();
}
$data['entity'] = $apiRequest['entity'] ?? NULL;
$data['action'] = $apiRequest['action'] ?? NULL;
- if (\CRM_Utils_Array::value('debug', \CRM_Utils_Array::value('params', $apiRequest))
+ if (!empty($apiRequest['params']['debug'])
// prevent recursion
&& empty($data['trace'])
) {
$data['trace'] = $e->getTraceAsString();
}
- return $this->createError($e->getMessage(), $data, $apiRequest, $errorCode);
+ return $this->createError($e->getMessage(), $data, $apiRequest);
}
/**
* Error data.
* @param array $apiRequest
* The full description of the API request.
- * @param mixed $code
- * Doesn't appear to be used.
*
* @throws \CRM_Core_Exception
* @return array
- * Array<type>.
*/
- public function createError($msg, $data, $apiRequest, $code = NULL) {
- // FIXME what to do with $code?
+ public function createError($msg, $data, $apiRequest) {
if ($msg === 'DB Error: constraint violation' || substr($msg, 0, 9) == 'DB Error:' || $msg == 'DB Error: already exists') {
try {
$fields = _civicrm_api3_api_getfields($apiRequest);
}
/**
- * @return array<ProviderInterface>
+ * @return \Civi\API\Provider\ProviderInterface[]
*/
public function getApiProviders() {
return $this->apiProviders;
}
/**
- * @param array $apiProviders
- * Array<ProviderInterface>.
+ * @param \Civi\API\Provider\ProviderInterface[] $apiProviders
* @return Kernel
*/
public function setApiProviders($apiProviders) {
*/
/**
- * CiviCRM API wrapper function.
+ * The original API wrapper.
+ *
+ * @deprecated
+ * Not recommended for new code but ok for existing code to continue using.
+ *
+ * Calling `civicrm_api()` is functionally identical to `civicrm_api3()` or `civicrm_api4()` except:
+ * 1. It requires `$params['version']`.
+ * 2. It catches exceptions and returns an array like `['is_error' => 1, 'error_message' => ...]`.
+ * This is disfavored for typical business-logic/hooks/forms/etc.
+ * However, if an existing caller handles `civicrm_api()`-style errors, then there is no functional benefit to reworking it.
*
* @param string $entity
- * type of entities to deal with
* @param string $action
- * create, get, delete or some special action name.
* @param array $params
- * array to be passed to function
*
* @return array|int|Civi\Api4\Generic\Result
*/
* Throws exception.
*
* @param string $entity
- * Type of entities to deal with.
* @param string $action
- * Create, get, delete or some special action name.
* @param array $params
- * Array to be passed to function.
*
* @throws CRM_Core_Exception
*
* @return array|int
- * Dependant on the $action
+ * Dependent on the $action
*/
function civicrm_api3(string $entity, string $action, array $params = []) {
$params['version'] = 3;
if (strtolower($apiRequest['action'] == 'getfields')) {
// the main param getfields takes is 'action' - however this param is not compatible with REST
// so we accept 'api_action' as an alias of action on getfields
- if (!empty($apiRequest['params']['api_action'])) {
- // $apiRequest['params']['action'] = $apiRequest['params']['api_action'];
- // unset($apiRequest['params']['api_action']);
- }
return ['action' => ['api.aliases' => ['api_action']]];
}
$getFieldsParams = ['action' => $apiRequest['action']];