Commit | Line | Data |
---|---|---|
b55bc593 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
41498ac5 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
b55bc593 | 5 | | | |
41498ac5 TO |
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 | | |
b55bc593 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
b55bc593 TO |
11 | |
12 | namespace Civi\API\Subscriber; | |
8882ff5c | 13 | |
b55bc593 TO |
14 | use Civi\API\Events; |
15 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
16 | ||
6550386a EM |
17 | /** |
18 | * Class TransactionSubscriber | |
e29a1b03 TO |
19 | * |
20 | * Implement transaction management for API calls. Two API options are accepted: | |
8882ff5c TO |
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. | |
e29a1b03 | 27 | * |
6550386a EM |
28 | * @package Civi\API\Subscriber |
29 | */ | |
b55bc593 | 30 | class TransactionSubscriber implements EventSubscriberInterface { |
34f3bbd9 | 31 | |
6550386a EM |
32 | /** |
33 | * @return array | |
34 | */ | |
b55bc593 | 35 | public static function getSubscribedEvents() { |
c64f69d9 | 36 | return [ |
39b870b8 TO |
37 | 'civi.api.prepare' => ['onApiPrepare', Events::W_EARLY], |
38 | 'civi.api.respond' => ['onApiRespond', Events::W_MIDDLE], | |
39 | 'civi.api.exception' => ['onApiException', Events::W_EARLY], | |
c64f69d9 | 40 | ]; |
b55bc593 TO |
41 | } |
42 | ||
43 | /** | |
f33d2b8c TO |
44 | * List of active transaction objects. |
45 | * | |
46 | * array(scalar $apiRequestId => CRM_Core_Transaction $tx) | |
47 | * | |
48 | * @var array | |
b55bc593 | 49 | */ |
c64f69d9 | 50 | private $transactions = []; |
5558f278 | 51 | |
e29a1b03 | 52 | /** |
f33d2b8c | 53 | * (Unused?) A list of requests which should be forcibly rolled back to |
e29a1b03 | 54 | * their save points. |
f33d2b8c TO |
55 | * |
56 | * array (scalar $apiRequestId => bool) | |
57 | * | |
58 | * @var array | |
e29a1b03 | 59 | */ |
c64f69d9 | 60 | private $forceRollback = []; |
e29a1b03 | 61 | |
5558f278 | 62 | /** |
fe482240 | 63 | * Determine if an API request should be treated as transactional. |
5558f278 TO |
64 | * |
65 | * @param \Civi\API\Provider\ProviderInterface $apiProvider | |
8882ff5c | 66 | * The API provider responsible for this request. |
5558f278 | 67 | * @param array $apiRequest |
8882ff5c | 68 | * The full API request. |
5558f278 TO |
69 | * @return bool |
70 | */ | |
71 | public function isTransactional($apiProvider, $apiRequest) { | |
3635b823 CW |
72 | if ($apiRequest['version'] == 4) { |
73 | return FALSE; | |
74 | } | |
e29a1b03 | 75 | if ($this->isForceRollback($apiProvider, $apiRequest)) { |
8882ff5c | 76 | return TRUE; |
e29a1b03 | 77 | } |
289c8dd4 | 78 | if (isset($apiRequest['params']['is_transactional'])) { |
e29a1b03 | 79 | return \CRM_Utils_String::strtobool($apiRequest['params']['is_transactional']) || $apiRequest['params']['is_transactional'] == 'nest'; |
289c8dd4 | 80 | } |
5558f278 TO |
81 | return strtolower($apiRequest['action']) == 'create' || strtolower($apiRequest['action']) == 'delete' || strtolower($apiRequest['action']) == 'submit'; |
82 | } | |
b55bc593 | 83 | |
e29a1b03 TO |
84 | /** |
85 | * Determine if caller wants us to *always* rollback. | |
86 | * | |
87 | * @param \Civi\API\Provider\ProviderInterface $apiProvider | |
8882ff5c | 88 | * The API provider responsible for this request. |
e29a1b03 | 89 | * @param array $apiRequest |
8882ff5c | 90 | * The full API request. |
e29a1b03 TO |
91 | * @return bool |
92 | */ | |
93 | public function isForceRollback($apiProvider, $apiRequest) { | |
3635b823 CW |
94 | if ($apiRequest['version'] == 4) { |
95 | return FALSE; | |
96 | } | |
8882ff5c | 97 | // FIXME: When APIv3 uses better parsing, only one check will be needed. |
e29a1b03 TO |
98 | if (isset($apiRequest['params']['options']['force_rollback'])) { |
99 | return \CRM_Utils_String::strtobool($apiRequest['params']['options']['force_rollback']); | |
100 | } | |
101 | if (isset($apiRequest['options']['force_rollback'])) { | |
102 | return \CRM_Utils_String::strtobool($apiRequest['options']['force_rollback']); | |
103 | } | |
104 | return FALSE; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Determine if caller wants a nested transaction or a re-used transaction. | |
109 | * | |
110 | * @param \Civi\API\Provider\ProviderInterface $apiProvider | |
8882ff5c | 111 | * The API provider responsible for this request. |
e29a1b03 | 112 | * @param array $apiRequest |
8882ff5c | 113 | * The full API request. |
a6c01b45 CW |
114 | * @return bool |
115 | * True if a new nested transaction is required; false if active tx may be used | |
e29a1b03 TO |
116 | */ |
117 | public function isNested($apiProvider, $apiRequest) { | |
118 | if ($this->isForceRollback($apiProvider, $apiRequest)) { | |
119 | return TRUE; | |
120 | } | |
8882ff5c | 121 | if (isset($apiRequest['params']['is_transactional']) && $apiRequest['params']['is_transactional'] === 'nest') { |
e29a1b03 TO |
122 | return TRUE; |
123 | } | |
124 | return FALSE; | |
125 | } | |
126 | ||
b55bc593 TO |
127 | /** |
128 | * Open a new transaction instance (if appropriate in the current policy) | |
129 | * | |
5558f278 | 130 | * @param \Civi\API\Event\PrepareEvent $event |
8882ff5c | 131 | * API preparation event. |
b55bc593 | 132 | */ |
8882ff5c | 133 | public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { |
b55bc593 | 134 | $apiRequest = $event->getApiRequest(); |
5558f278 | 135 | if ($this->isTransactional($event->getApiProvider(), $apiRequest)) { |
e29a1b03 TO |
136 | $this->transactions[$apiRequest['id']] = new \CRM_Core_Transaction($this->isNested($event->getApiProvider(), $apiRequest)); |
137 | } | |
138 | if ($this->isForceRollback($event->getApiProvider(), $apiRequest)) { | |
139 | $this->transactions[$apiRequest['id']]->rollback(); | |
b55bc593 TO |
140 | } |
141 | } | |
142 | ||
143 | /** | |
fe482240 | 144 | * Close any pending transactions. |
8882ff5c TO |
145 | * |
146 | * @param \Civi\API\Event\RespondEvent $event | |
147 | * API response event. | |
b55bc593 | 148 | */ |
8882ff5c | 149 | public function onApiRespond(\Civi\API\Event\RespondEvent $event) { |
5558f278 | 150 | $apiRequest = $event->getApiRequest(); |
ea24cf7f TO |
151 | if (isset($this->transactions[$apiRequest['id']])) { |
152 | if (civicrm_error($event->getResponse())) { | |
153 | $this->transactions[$apiRequest['id']]->rollback(); | |
154 | } | |
155 | unset($this->transactions[$apiRequest['id']]); | |
156 | } | |
b55bc593 TO |
157 | } |
158 | ||
159 | /** | |
fe482240 | 160 | * Rollback the pending transaction. |
b55bc593 TO |
161 | * |
162 | * @param \Civi\API\Event\ExceptionEvent $event | |
8882ff5c | 163 | * API exception event. |
b55bc593 | 164 | */ |
8882ff5c | 165 | public function onApiException(\Civi\API\Event\ExceptionEvent $event) { |
5558f278 TO |
166 | $apiRequest = $event->getApiRequest(); |
167 | if (isset($this->transactions[$apiRequest['id']])) { | |
168 | $this->transactions[$apiRequest['id']]->rollback(); | |
169 | unset($this->transactions[$apiRequest['id']]); | |
b55bc593 TO |
170 | } |
171 | } | |
96025800 | 172 | |
6550386a | 173 | } |