Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database;
4
 
5
class DatabaseTransactionsManager
6
{
7
    /**
8
     * All of the recorded transactions.
9
     *
10
     * @var \Illuminate\Support\Collection
11
     */
12
    protected $transactions;
13
 
14
    /**
15
     * The database transaction that should be ignored by callbacks.
16
     *
17
     * @var \Illuminate\Database\DatabaseTransactionRecord
18
     */
19
    protected $callbacksShouldIgnore;
20
 
21
    /**
22
     * Create a new database transactions manager instance.
23
     *
24
     * @return void
25
     */
26
    public function __construct()
27
    {
28
        $this->transactions = collect();
29
    }
30
 
31
    /**
32
     * Start a new database transaction.
33
     *
34
     * @param  string  $connection
35
     * @param  int  $level
36
     * @return void
37
     */
38
    public function begin($connection, $level)
39
    {
40
        $this->transactions->push(
41
            new DatabaseTransactionRecord($connection, $level)
42
        );
43
    }
44
 
45
    /**
46
     * Rollback the active database transaction.
47
     *
48
     * @param  string  $connection
49
     * @param  int  $level
50
     * @return void
51
     */
52
    public function rollback($connection, $level)
53
    {
54
        $this->transactions = $this->transactions->reject(
55
            fn ($transaction) => $transaction->connection == $connection && $transaction->level > $level
56
        )->values();
57
 
58
        if ($this->transactions->isEmpty()) {
59
            $this->callbacksShouldIgnore = null;
60
        }
61
    }
62
 
63
    /**
64
     * Commit the active database transaction.
65
     *
66
     * @param  string  $connection
67
     * @return void
68
     */
69
    public function commit($connection)
70
    {
71
        [$forThisConnection, $forOtherConnections] = $this->transactions->partition(
72
            fn ($transaction) => $transaction->connection == $connection
73
        );
74
 
75
        $this->transactions = $forOtherConnections->values();
76
 
77
        $forThisConnection->map->executeCallbacks();
78
 
79
        if ($this->transactions->isEmpty()) {
80
            $this->callbacksShouldIgnore = null;
81
        }
82
    }
83
 
84
    /**
85
     * Register a transaction callback.
86
     *
87
     * @param  callable  $callback
88
     * @return void
89
     */
90
    public function addCallback($callback)
91
    {
92
        if ($current = $this->callbackApplicableTransactions()->last()) {
93
            return $current->addCallback($callback);
94
        }
95
 
96
        $callback();
97
    }
98
 
99
    /**
100
     * Specify that callbacks should ignore the given transaction when determining if they should be executed.
101
     *
102
     * @param  \Illuminate\Database\DatabaseTransactionRecord  $transaction
103
     * @return $this
104
     */
105
    public function callbacksShouldIgnore(DatabaseTransactionRecord $transaction)
106
    {
107
        $this->callbacksShouldIgnore = $transaction;
108
 
109
        return $this;
110
    }
111
 
112
    /**
113
     * Get the transactions that are applicable to callbacks.
114
     *
115
     * @return \Illuminate\Support\Collection
116
     */
117
    public function callbackApplicableTransactions()
118
    {
119
        return $this->transactions->reject(function ($transaction) {
120
            return $transaction === $this->callbacksShouldIgnore;
121
        })->values();
122
    }
123
 
124
    /**
125
     * Get all the transactions.
126
     *
127
     * @return \Illuminate\Support\Collection
128
     */
129
    public function getTransactions()
130
    {
131
        return $this->transactions;
132
    }
133
}