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