3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 namespace Civi\API\Subscriber
;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
18 * Class TransactionSubscriber
20 * Implement transaction management for API calls. Two API options are accepted:
21 * - is_transactional: bool|'nest' - if true, then all work is done inside a
22 * transaction. By default, true for mutator actions (C-UD). 'nest' will
23 * force creation of a nested transaction; otherwise, the default is to
24 * re-use any existing transactions.
25 * - options.force_rollback: bool - if true, all work is done in a nested
26 * transaction which will be rolled back.
28 * @package Civi\API\Subscriber
30 class TransactionSubscriber
implements EventSubscriberInterface
{
35 public static function getSubscribedEvents() {
37 Events
::PREPARE
=> ['onApiPrepare', Events
::W_EARLY
],
38 Events
::RESPOND
=> ['onApiRespond', Events
::W_MIDDLE
],
39 Events
::EXCEPTION
=> ['onApiException', Events
::W_EARLY
],
44 * List of active transaction objects.
46 * array(scalar $apiRequestId => CRM_Core_Transaction $tx)
50 private $transactions = [];
53 * (Unused?) A list of requests which should be forcibly rolled back to
56 * array (scalar $apiRequestId => bool)
60 private $forceRollback = [];
63 * Determine if an API request should be treated as transactional.
65 * @param \Civi\API\Provider\ProviderInterface $apiProvider
66 * The API provider responsible for this request.
67 * @param array $apiRequest
68 * The full API request.
71 public function isTransactional($apiProvider, $apiRequest) {
72 if ($apiRequest['version'] == 4) {
75 if ($this->isForceRollback($apiProvider, $apiRequest)) {
78 if (isset($apiRequest['params']['is_transactional'])) {
79 return \CRM_Utils_String
::strtobool($apiRequest['params']['is_transactional']) ||
$apiRequest['params']['is_transactional'] == 'nest';
81 return strtolower($apiRequest['action']) == 'create' ||
strtolower($apiRequest['action']) == 'delete' ||
strtolower($apiRequest['action']) == 'submit';
85 * Determine if caller wants us to *always* rollback.
87 * @param \Civi\API\Provider\ProviderInterface $apiProvider
88 * The API provider responsible for this request.
89 * @param array $apiRequest
90 * The full API request.
93 public function isForceRollback($apiProvider, $apiRequest) {
94 if ($apiRequest['version'] == 4) {
97 // FIXME: When APIv3 uses better parsing, only one check will be needed.
98 if (isset($apiRequest['params']['options']['force_rollback'])) {
99 return \CRM_Utils_String
::strtobool($apiRequest['params']['options']['force_rollback']);
101 if (isset($apiRequest['options']['force_rollback'])) {
102 return \CRM_Utils_String
::strtobool($apiRequest['options']['force_rollback']);
108 * Determine if caller wants a nested transaction or a re-used transaction.
110 * @param \Civi\API\Provider\ProviderInterface $apiProvider
111 * The API provider responsible for this request.
112 * @param array $apiRequest
113 * The full API request.
115 * True if a new nested transaction is required; false if active tx may be used
117 public function isNested($apiProvider, $apiRequest) {
118 if ($this->isForceRollback($apiProvider, $apiRequest)) {
121 if (isset($apiRequest['params']['is_transactional']) && $apiRequest['params']['is_transactional'] === 'nest') {
128 * Open a new transaction instance (if appropriate in the current policy)
130 * @param \Civi\API\Event\PrepareEvent $event
131 * API preparation event.
133 public function onApiPrepare(\Civi\API\Event\PrepareEvent
$event) {
134 $apiRequest = $event->getApiRequest();
135 if ($this->isTransactional($event->getApiProvider(), $apiRequest)) {
136 $this->transactions
[$apiRequest['id']] = new \
CRM_Core_Transaction($this->isNested($event->getApiProvider(), $apiRequest));
138 if ($this->isForceRollback($event->getApiProvider(), $apiRequest)) {
139 $this->transactions
[$apiRequest['id']]->rollback();
144 * Close any pending transactions.
146 * @param \Civi\API\Event\RespondEvent $event
147 * API response event.
149 public function onApiRespond(\Civi\API\Event\RespondEvent
$event) {
150 $apiRequest = $event->getApiRequest();
151 if (isset($this->transactions
[$apiRequest['id']])) {
152 if (civicrm_error($event->getResponse())) {
153 $this->transactions
[$apiRequest['id']]->rollback();
155 unset($this->transactions
[$apiRequest['id']]);
160 * Rollback the pending transaction.
162 * @param \Civi\API\Event\ExceptionEvent $event
163 * API exception event.
165 public function onApiException(\Civi\API\Event\ExceptionEvent
$event) {
166 $apiRequest = $event->getApiRequest();
167 if (isset($this->transactions
[$apiRequest['id']])) {
168 $this->transactions
[$apiRequest['id']]->rollback();
169 unset($this->transactions
[$apiRequest['id']]);