Capsule
2 years ago
Concerns
2 weeks ago
Connectors
2 weeks ago
Console
2 weeks ago
DBAL
2 weeks ago
Eloquent
2 weeks ago
Events
2 weeks ago
Migrations
2 weeks ago
PDO
2 weeks ago
Query
2 weeks ago
Schema
2 weeks ago
ClassMorphViolationException.php
2 years ago
ConfigurationUrlParser.php
2 years ago
Connection.php
2 weeks ago
ConnectionInterface.php
2 years ago
ConnectionResolver.php
2 weeks ago
ConnectionResolverInterface.php
2 years ago
DatabaseManager.php
2 weeks ago
DatabaseServiceProvider.php
2 weeks ago
DatabaseTransactionRecord.php
2 weeks ago
DatabaseTransactionsManager.php
2 weeks ago
DeadlockException.php
2 weeks ago
DetectsConcurrencyErrors.php
2 years ago
DetectsLostConnections.php
2 weeks ago
Grammar.php
2 weeks ago
LazyLoadingViolationException.php
2 years ago
LostConnectionException.php
2 weeks ago
MigrationServiceProvider.php
2 weeks ago
MultipleColumnsSelectedException.php
2 weeks ago
MultipleRecordsFoundException.php
2 weeks ago
MySqlConnection.php
2 weeks ago
PostgresConnection.php
2 weeks ago
QueryException.php
2 years ago
RecordsNotFoundException.php
2 years ago
SQLiteConnection.php
2 weeks ago
SQLiteDatabaseDoesNotExistException.php
2 weeks ago
Seeder.php
2 weeks ago
SqlServerConnection.php
2 weeks ago
DatabaseTransactionsManager.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database; |
| 4 | |
| 5 | /** @internal */ |
| 6 | class DatabaseTransactionsManager |
| 7 | { |
| 8 | /** |
| 9 | * All of the recorded transactions. |
| 10 | * |
| 11 | * @var \Illuminate\Support\Collection |
| 12 | */ |
| 13 | protected $transactions; |
| 14 | /** |
| 15 | * The database transaction that should be ignored by callbacks. |
| 16 | * |
| 17 | * @var \Illuminate\Database\DatabaseTransactionRecord |
| 18 | */ |
| 19 | protected $callbacksShouldIgnore; |
| 20 | /** |
| 21 | * Create a new database transactions manager instance. |
| 22 | * |
| 23 | * @return void |
| 24 | */ |
| 25 | public function __construct() |
| 26 | { |
| 27 | $this->transactions = \IAWPSCOPED\collect(); |
| 28 | } |
| 29 | /** |
| 30 | * Start a new database transaction. |
| 31 | * |
| 32 | * @param string $connection |
| 33 | * @param int $level |
| 34 | * @return void |
| 35 | */ |
| 36 | public function begin($connection, $level) |
| 37 | { |
| 38 | $this->transactions->push(new DatabaseTransactionRecord($connection, $level)); |
| 39 | } |
| 40 | /** |
| 41 | * Rollback the active database transaction. |
| 42 | * |
| 43 | * @param string $connection |
| 44 | * @param int $level |
| 45 | * @return void |
| 46 | */ |
| 47 | public function rollback($connection, $level) |
| 48 | { |
| 49 | $this->transactions = $this->transactions->reject(fn($transaction) => $transaction->connection == $connection && $transaction->level > $level)->values(); |
| 50 | if ($this->transactions->isEmpty()) { |
| 51 | $this->callbacksShouldIgnore = null; |
| 52 | } |
| 53 | } |
| 54 | /** |
| 55 | * Commit the active database transaction. |
| 56 | * |
| 57 | * @param string $connection |
| 58 | * @return void |
| 59 | */ |
| 60 | public function commit($connection) |
| 61 | { |
| 62 | [$forThisConnection, $forOtherConnections] = $this->transactions->partition(fn($transaction) => $transaction->connection == $connection); |
| 63 | $this->transactions = $forOtherConnections->values(); |
| 64 | $forThisConnection->map->executeCallbacks(); |
| 65 | if ($this->transactions->isEmpty()) { |
| 66 | $this->callbacksShouldIgnore = null; |
| 67 | } |
| 68 | } |
| 69 | /** |
| 70 | * Register a transaction callback. |
| 71 | * |
| 72 | * @param callable $callback |
| 73 | * @return void |
| 74 | */ |
| 75 | public function addCallback($callback) |
| 76 | { |
| 77 | if ($current = $this->callbackApplicableTransactions()->last()) { |
| 78 | return $current->addCallback($callback); |
| 79 | } |
| 80 | $callback(); |
| 81 | } |
| 82 | /** |
| 83 | * Specify that callbacks should ignore the given transaction when determining if they should be executed. |
| 84 | * |
| 85 | * @param \Illuminate\Database\DatabaseTransactionRecord $transaction |
| 86 | * @return $this |
| 87 | */ |
| 88 | public function callbacksShouldIgnore(DatabaseTransactionRecord $transaction) |
| 89 | { |
| 90 | $this->callbacksShouldIgnore = $transaction; |
| 91 | return $this; |
| 92 | } |
| 93 | /** |
| 94 | * Get the transactions that are applicable to callbacks. |
| 95 | * |
| 96 | * @return \Illuminate\Support\Collection |
| 97 | */ |
| 98 | public function callbackApplicableTransactions() |
| 99 | { |
| 100 | return $this->transactions->reject(function ($transaction) { |
| 101 | return $transaction === $this->callbacksShouldIgnore; |
| 102 | })->values(); |
| 103 | } |
| 104 | /** |
| 105 | * Get all the transactions. |
| 106 | * |
| 107 | * @return \Illuminate\Support\Collection |
| 108 | */ |
| 109 | public function getTransactions() |
| 110 | { |
| 111 | return $this->transactions; |
| 112 | } |
| 113 | } |
| 114 |