123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace Illuminate\Database;
- class DatabaseTransactionsManager
- {
- /**
- * All of the recorded transactions.
- *
- * @var \Illuminate\Support\Collection
- */
- protected $transactions;
- /**
- * Create a new database transactions manager instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->transactions = collect();
- }
- /**
- * Start a new database transaction.
- *
- * @param string $connection
- * @param int $level
- * @return void
- */
- public function begin($connection, $level)
- {
- $this->transactions->push(
- new DatabaseTransactionRecord($connection, $level)
- );
- }
- /**
- * Rollback the active database transaction.
- *
- * @param string $connection
- * @param int $level
- * @return void
- */
- public function rollback($connection, $level)
- {
- $this->transactions = $this->transactions->reject(function ($transaction) use ($connection, $level) {
- return $transaction->connection == $connection &&
- $transaction->level > $level;
- })->values();
- }
- /**
- * Commit the active database transaction.
- *
- * @param string $connection
- * @return void
- */
- public function commit($connection)
- {
- [$forThisConnection, $forOtherConnections] = $this->transactions->partition(
- function ($transaction) use ($connection) {
- return $transaction->connection == $connection;
- }
- );
- $this->transactions = $forOtherConnections->values();
- $forThisConnection->map->executeCallbacks();
- }
- /**
- * Register a transaction callback.
- *
- * @param callable $callback
- * @return void
- */
- public function addCallback($callback)
- {
- if ($current = $this->transactions->last()) {
- return $current->addCallback($callback);
- }
- call_user_func($callback);
- }
- /**
- * Get all the transactions.
- *
- * @return \Illuminate\Support\Collection
- */
- public function getTransactions()
- {
- return $this->transactions;
- }
- }
|