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 CW |
36 | return [ |
37 | Events::PREPARE => ['onApiPrepare', Events::W_EARLY], | |
38 | Events::RESPOND => ['onApiRespond', Events::W_MIDDLE], | |
39 | Events::EXCEPTION => ['onApiException', Events::W_EARLY], | |
40 | ]; | |
b55bc593 TO |
41 | } |
42 | ||
43 | /** | |
5558f278 | 44 | * @var array (scalar $apiRequestId => CRM_Core_Transaction $tx) |
b55bc593 | 45 | */ |
c64f69d9 | 46 | private $transactions = []; |
5558f278 | 47 | |
e29a1b03 TO |
48 | /** |
49 | * @var array (scalar $apiRequestId => bool) | |
50 | * | |
51 | * A list of requests which should be forcibly rolled back to | |
52 | * their save points. | |
53 | */ | |
c64f69d9 | 54 | private $forceRollback = []; |
e29a1b03 | 55 | |
5558f278 | 56 | /** |
fe482240 | 57 | * Determine if an API request should be treated as transactional. |
5558f278 TO |
58 | * |
59 | * @param \Civi\API\Provider\ProviderInterface $apiProvider | |
8882ff5c | 60 | * The API provider responsible for this request. |
5558f278 | 61 | * @param array $apiRequest |
8882ff5c | 62 | * The full API request. |
5558f278 TO |
63 | * @return bool |
64 | */ | |
65 | public function isTransactional($apiProvider, $apiRequest) { | |
3635b823 CW |
66 | if ($apiRequest['version'] == 4) { |
67 | return FALSE; | |
68 | } | |
e29a1b03 | 69 | if ($this->isForceRollback($apiProvider, $apiRequest)) { |
8882ff5c | 70 | return TRUE; |
e29a1b03 | 71 | } |
289c8dd4 | 72 | if (isset($apiRequest['params']['is_transactional'])) { |
e29a1b03 | 73 | return \CRM_Utils_String::strtobool($apiRequest['params']['is_transactional']) || $apiRequest['params']['is_transactional'] == 'nest'; |
289c8dd4 | 74 | } |
5558f278 TO |
75 | return strtolower($apiRequest['action']) == 'create' || strtolower($apiRequest['action']) == 'delete' || strtolower($apiRequest['action']) == 'submit'; |
76 | } | |
b55bc593 | 77 | |
e29a1b03 TO |
78 | /** |
79 | * Determine if caller wants us to *always* rollback. | |
80 | * | |
81 | * @param \Civi\API\Provider\ProviderInterface $apiProvider | |
8882ff5c | 82 | * The API provider responsible for this request. |
e29a1b03 | 83 | * @param array $apiRequest |
8882ff5c | 84 | * The full API request. |
e29a1b03 TO |
85 | * @return bool |
86 | */ | |
87 | public function isForceRollback($apiProvider, $apiRequest) { | |
3635b823 CW |
88 | if ($apiRequest['version'] == 4) { |
89 | return FALSE; | |
90 | } | |
8882ff5c | 91 | // FIXME: When APIv3 uses better parsing, only one check will be needed. |
e29a1b03 TO |
92 | if (isset($apiRequest['params']['options']['force_rollback'])) { |
93 | return \CRM_Utils_String::strtobool($apiRequest['params']['options']['force_rollback']); | |
94 | } | |
95 | if (isset($apiRequest['options']['force_rollback'])) { | |
96 | return \CRM_Utils_String::strtobool($apiRequest['options']['force_rollback']); | |
97 | } | |
98 | return FALSE; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Determine if caller wants a nested transaction or a re-used transaction. | |
103 | * | |
104 | * @param \Civi\API\Provider\ProviderInterface $apiProvider | |
8882ff5c | 105 | * The API provider responsible for this request. |
e29a1b03 | 106 | * @param array $apiRequest |
8882ff5c | 107 | * The full API request. |
a6c01b45 CW |
108 | * @return bool |
109 | * True if a new nested transaction is required; false if active tx may be used | |
e29a1b03 TO |
110 | */ |
111 | public function isNested($apiProvider, $apiRequest) { | |
112 | if ($this->isForceRollback($apiProvider, $apiRequest)) { | |
113 | return TRUE; | |
114 | } | |
8882ff5c | 115 | if (isset($apiRequest['params']['is_transactional']) && $apiRequest['params']['is_transactional'] === 'nest') { |
e29a1b03 TO |
116 | return TRUE; |
117 | } | |
118 | return FALSE; | |
119 | } | |
120 | ||
b55bc593 TO |
121 | /** |
122 | * Open a new transaction instance (if appropriate in the current policy) | |
123 | * | |
5558f278 | 124 | * @param \Civi\API\Event\PrepareEvent $event |
8882ff5c | 125 | * API preparation event. |
b55bc593 | 126 | */ |
8882ff5c | 127 | public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) { |
b55bc593 | 128 | $apiRequest = $event->getApiRequest(); |
5558f278 | 129 | if ($this->isTransactional($event->getApiProvider(), $apiRequest)) { |
e29a1b03 TO |
130 | $this->transactions[$apiRequest['id']] = new \CRM_Core_Transaction($this->isNested($event->getApiProvider(), $apiRequest)); |
131 | } | |
132 | if ($this->isForceRollback($event->getApiProvider(), $apiRequest)) { | |
133 | $this->transactions[$apiRequest['id']]->rollback(); | |
b55bc593 TO |
134 | } |
135 | } | |
136 | ||
137 | /** | |
fe482240 | 138 | * Close any pending transactions. |
8882ff5c TO |
139 | * |
140 | * @param \Civi\API\Event\RespondEvent $event | |
141 | * API response event. | |
b55bc593 | 142 | */ |
8882ff5c | 143 | public function onApiRespond(\Civi\API\Event\RespondEvent $event) { |
5558f278 | 144 | $apiRequest = $event->getApiRequest(); |
ea24cf7f TO |
145 | if (isset($this->transactions[$apiRequest['id']])) { |
146 | if (civicrm_error($event->getResponse())) { | |
147 | $this->transactions[$apiRequest['id']]->rollback(); | |
148 | } | |
149 | unset($this->transactions[$apiRequest['id']]); | |
150 | } | |
b55bc593 TO |
151 | } |
152 | ||
153 | /** | |
fe482240 | 154 | * Rollback the pending transaction. |
b55bc593 TO |
155 | * |
156 | * @param \Civi\API\Event\ExceptionEvent $event | |
8882ff5c | 157 | * API exception event. |
b55bc593 | 158 | */ |
8882ff5c | 159 | public function onApiException(\Civi\API\Event\ExceptionEvent $event) { |
5558f278 TO |
160 | $apiRequest = $event->getApiRequest(); |
161 | if (isset($this->transactions[$apiRequest['id']])) { | |
162 | $this->transactions[$apiRequest['id']]->rollback(); | |
163 | unset($this->transactions[$apiRequest['id']]); | |
b55bc593 TO |
164 | } |
165 | } | |
96025800 | 166 | |
6550386a | 167 | } |