3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
28 namespace Civi\API\Subscriber
;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
34 * Class TransactionSubscriber
36 * Implement transaction management for API calls. Two API options are accepted:
37 * - is_transactional: bool|'nest' - if true, then all work is done inside a
38 * transaction. By default, true for mutator actions (C-UD). 'nest' will
39 * force creation of a nested transaction; otherwise, the default is to
40 * re-use any existing transactions.
41 * - options.force_rollback: bool - if true, all work is done in a nested
42 * transaction which will be rolled back.
44 * @package Civi\API\Subscriber
46 class TransactionSubscriber
implements EventSubscriberInterface
{
51 public static function getSubscribedEvents() {
53 Events
::PREPARE
=> ['onApiPrepare', Events
::W_EARLY
],
54 Events
::RESPOND
=> ['onApiRespond', Events
::W_MIDDLE
],
55 Events
::EXCEPTION
=> ['onApiException', Events
::W_EARLY
],
60 * @var array (scalar $apiRequestId => CRM_Core_Transaction $tx)
62 private $transactions = [];
65 * @var array (scalar $apiRequestId => bool)
67 * A list of requests which should be forcibly rolled back to
70 private $forceRollback = [];
73 * Determine if an API request should be treated as transactional.
75 * @param \Civi\API\Provider\ProviderInterface $apiProvider
76 * The API provider responsible for this request.
77 * @param array $apiRequest
78 * The full API request.
81 public function isTransactional($apiProvider, $apiRequest) {
82 if ($apiRequest['version'] == 4) {
85 if ($this->isForceRollback($apiProvider, $apiRequest)) {
88 if (isset($apiRequest['params']['is_transactional'])) {
89 return \CRM_Utils_String
::strtobool($apiRequest['params']['is_transactional']) ||
$apiRequest['params']['is_transactional'] == 'nest';
91 return strtolower($apiRequest['action']) == 'create' ||
strtolower($apiRequest['action']) == 'delete' ||
strtolower($apiRequest['action']) == 'submit';
95 * Determine if caller wants us to *always* rollback.
97 * @param \Civi\API\Provider\ProviderInterface $apiProvider
98 * The API provider responsible for this request.
99 * @param array $apiRequest
100 * The full API request.
103 public function isForceRollback($apiProvider, $apiRequest) {
104 if ($apiRequest['version'] == 4) {
107 // FIXME: When APIv3 uses better parsing, only one check will be needed.
108 if (isset($apiRequest['params']['options']['force_rollback'])) {
109 return \CRM_Utils_String
::strtobool($apiRequest['params']['options']['force_rollback']);
111 if (isset($apiRequest['options']['force_rollback'])) {
112 return \CRM_Utils_String
::strtobool($apiRequest['options']['force_rollback']);
118 * Determine if caller wants a nested transaction or a re-used transaction.
120 * @param \Civi\API\Provider\ProviderInterface $apiProvider
121 * The API provider responsible for this request.
122 * @param array $apiRequest
123 * The full API request.
125 * True if a new nested transaction is required; false if active tx may be used
127 public function isNested($apiProvider, $apiRequest) {
128 if ($this->isForceRollback($apiProvider, $apiRequest)) {
131 if (isset($apiRequest['params']['is_transactional']) && $apiRequest['params']['is_transactional'] === 'nest') {
138 * Open a new transaction instance (if appropriate in the current policy)
140 * @param \Civi\API\Event\PrepareEvent $event
141 * API preparation event.
143 public function onApiPrepare(\Civi\API\Event\PrepareEvent
$event) {
144 $apiRequest = $event->getApiRequest();
145 if ($this->isTransactional($event->getApiProvider(), $apiRequest)) {
146 $this->transactions
[$apiRequest['id']] = new \
CRM_Core_Transaction($this->isNested($event->getApiProvider(), $apiRequest));
148 if ($this->isForceRollback($event->getApiProvider(), $apiRequest)) {
149 $this->transactions
[$apiRequest['id']]->rollback();
154 * Close any pending transactions.
156 * @param \Civi\API\Event\RespondEvent $event
157 * API response event.
159 public function onApiRespond(\Civi\API\Event\RespondEvent
$event) {
160 $apiRequest = $event->getApiRequest();
161 if (isset($this->transactions
[$apiRequest['id']])) {
162 if (civicrm_error($event->getResponse())) {
163 $this->transactions
[$apiRequest['id']]->rollback();
165 unset($this->transactions
[$apiRequest['id']]);
170 * Rollback the pending transaction.
172 * @param \Civi\API\Event\ExceptionEvent $event
173 * API exception event.
175 public function onApiException(\Civi\API\Event\ExceptionEvent
$event) {
176 $apiRequest = $event->getApiRequest();
177 if (isset($this->transactions
[$apiRequest['id']])) {
178 $this->transactions
[$apiRequest['id']]->rollback();
179 unset($this->transactions
[$apiRequest['id']]);