Skip to content

Commit 354c264

Browse files
committed
Added $tries to Database::transaction
1 parent b2f653c commit 354c264

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/Database.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ public function transactionCommit();
9999
public function transactionRollback();
100100

101101
/**
102-
* @param callable $callback
102+
* @param int|callable $tries
103+
* @param callable|null $callback
104+
* @return $this
103105
* @throws \Exception
104-
* @return mixed
105106
*/
106-
public function transaction($callback);
107+
public function transaction($tries = 1, $callback = null);
107108
}

src/Databases/MySQL.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,29 @@ public function transactionRollback() {
236236
}
237237

238238
/**
239-
* @param callable $callback
240-
* @throws \Exception
239+
* @param int|callable $tries
240+
* @param callable|null $callback
241241
* @return $this
242+
* @throws \Exception
242243
*/
243-
public function transaction($callback) {
244-
try {
245-
$this->transactionStart();
246-
$result = call_user_func($callback, $this);
247-
$this->transactionCommit();
248-
return $result;
249-
} catch (\Exception $e) {
250-
$this->transactionRollback();
251-
throw $e;
244+
public function transaction($tries = 1, $callback = null) {
245+
if(is_callable($tries)) {
246+
$callback = $tries;
247+
$tries = 1;
248+
} elseif(!is_callable($callback)) {
249+
throw new \Exception("Callable must be a callable");
250+
}
251+
$e = null;
252+
for(; $tries--;) {
253+
try {
254+
$this->transactionStart();
255+
$result = call_user_func($callback, $this);
256+
$this->transactionCommit();
257+
return $result;
258+
} catch (\Exception $e) {
259+
$this->transactionRollback();
260+
}
252261
}
262+
throw $e;
253263
}
254264
}

0 commit comments

Comments
 (0)