forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.zep
More file actions
363 lines (309 loc) · 8.65 KB
/
Manager.zep
File metadata and controls
363 lines (309 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Phalcon\Mvc\Model\Transaction;
use Phalcon\Di\Di;
use Phalcon\Di\DiInterface;
use Phalcon\Di\InjectionAwareInterface;
use Phalcon\Mvc\Model\Transaction;
use Phalcon\Mvc\Model\TransactionInterface;
/**
* Phalcon\Mvc\Model\Transaction\Manager
*
* A transaction acts on a single database connection. If you have multiple
* class-specific databases, the transaction will not protect interaction among
* them.
*
* This class manages the objects that compose a transaction.
* A transaction produces a unique connection that is passed to every object
* part of the transaction.
*
* ```php
* use Phalcon\Mvc\Model\Transaction\Failed;
* use Phalcon\Mvc\Model\Transaction\Manager;
*
* try {
* $transactionManager = new Manager();
*
* $transaction = $transactionManager->get();
*
* $robot = new Robots();
*
* $robot->setTransaction($transaction);
*
* $robot->name = "WALL·E";
* $robot->created_at = date("Y-m-d");
*
* if ($robot->save() === false) {
* $transaction->rollback("Can't save robot");
* }
*
* $robotPart = new RobotParts();
*
* $robotPart->setTransaction($transaction);
*
* $robotPart->type = "head";
*
* if ($robotPart->save() === false) {
* $transaction->rollback("Can't save robot part");
* }
*
* $transaction->commit();
* } catch (Failed $e) {
* echo "Failed, reason: ", $e->getMessage();
* }
*```
*/
class Manager implements ManagerInterface, InjectionAwareInterface
{
/**
* @var DiInterface|null
*/
protected container;
/**
* @var bool
*/
protected initialized = false;
/**
* @var int
*/
protected number = 0;
/**
* @var bool
*/
protected rollbackPendent = true;
/**
* @var string
*/
protected service = "db";
/**
* @var array
*/
protected transactions = [];
/**
* Phalcon\Mvc\Model\Transaction\Manager constructor
*
* @param DiInterface|null container
*/
public function __construct(<DiInterface> container = null)
{
if !container {
let container = Di::getDefault();
}
let this->container = container;
if unlikely typeof container != "object" {
throw new Exception(
"A dependency injection container is required to access the services related to the ORM"
);
}
}
/**
* Remove all the transactions from the manager
*/
public function collectTransactions() -> void
{
var transactions;
let transactions = this->transactions;
for _ in transactions {
let this->number--;
}
let this->transactions = [];
}
/**
* Commits active transactions within the manager
*/
public function commit()
{
var transactions, transaction, connection;
let transactions = this->transactions;
for transaction in transactions {
let connection = transaction->getConnection();
if connection->isUnderTransaction() {
connection->commit();
}
this->collectTransaction(transaction);
}
}
/**
* Returns a new \Phalcon\Mvc\Model\Transaction or an already created once
* This method registers a shutdown function to rollback active connections
*
* @param bool autoBegin
*
* @return TransactionInterface
*/
public function get(bool autoBegin = true) -> <TransactionInterface>
{
if !this->initialized {
if this->rollbackPendent {
register_shutdown_function(
[
this,
"rollbackPendent"
]
);
}
let this->initialized = true;
}
return this->getOrCreateTransaction(autoBegin);
}
/**
* Returns the database service used to isolate the transaction
*/
public function getDbService() -> string
{
return this->service;
}
/**
* Returns the dependency injection container
*/
public function getDI() -> <DiInterface>
{
return this->container;
}
/**
* Create/Returns a new transaction or an existing one
*
* @param bool autoBegin
*
* @return TransactionInterface
*/
public function getOrCreateTransaction(bool autoBegin = true) -> <TransactionInterface>
{
var container, transaction, transactions;
let container = <DiInterface> this->container;
if unlikely typeof container != "object" {
throw new Exception(
"A dependency injection container is required to access the services related to the ORM"
);
}
if this->number {
let transactions = this->transactions;
for transaction in reverse transactions {
if typeof transaction == "object" {
transaction->setIsNewTransaction(false);
return transaction;
}
}
}
let transaction = new Transaction(container, autoBegin, this->service);
transaction->setTransactionManager(this);
let this->transactions[] = transaction, this->number++;
return transaction;
}
/**
* Check if the transaction manager is registering a shutdown function to
* clean up pendent transactions
*/
public function getRollbackPendent() -> bool
{
return this->rollbackPendent;
}
/**
* Checks whether the manager has an active transaction
*/
public function has() -> bool
{
return this->number > 0;
}
/**
* Notifies the manager about a committed transaction
*
* @param TransactionInterface transaction
*/
public function notifyCommit(<TransactionInterface> transaction) -> void
{
this->collectTransaction(transaction);
}
/**
* Notifies the manager about a rollbacked transaction
*
* @param TransactionInterface transaction
*/
public function notifyRollback(<TransactionInterface> transaction) -> void
{
this->collectTransaction(transaction);
}
/**
* Rollbacks active transactions within the manager
* Collect will remove the transaction from the manager
*
* @param bool collect
*/
public function rollback(bool collect = true) -> void
{
var transactions, transaction, connection;
let transactions = this->transactions;
for transaction in transactions {
let connection = transaction->getConnection();
if connection->isUnderTransaction() {
connection->rollback();
connection->close();
}
if collect {
this->collectTransaction(transaction);
}
}
}
/**
* Rollbacks active transactions within the manager
*/
public function rollbackPendent() -> void
{
this->rollback();
}
/**
* Sets the database service used to run the isolated transactions
*
* @param string service
*/
public function setDbService(string! service) -> <ManagerInterface>
{
let this->service = service;
return this;
}
/**
* Sets the dependency injection container
*
* @param DiInterface container
*/
public function setDI(<DiInterface> container) -> void
{
let this->container = container;
}
/**
* Set if the transaction manager must register a shutdown function to clean
* up pendent transactions
*
* @param bool rollbackPendent
*/
public function setRollbackPendent(bool rollbackPendent) -> <ManagerInterface>
{
let this->rollbackPendent = rollbackPendent;
return this;
}
/**
* Removes transactions from the TransactionManager
*
* @param TransactionInterface transaction
*/
protected function collectTransaction(<TransactionInterface> transaction) -> void
{
var managedTransaction;
array newTransactions;
let newTransactions = [];
for managedTransaction in this->transactions {
if managedTransaction != transaction {
let newTransactions[] = managedTransaction;
} else {
let this->number--;
}
}
let this->transactions = newTransactions;
}
}