1 | <?php |
||
16 | trait ManagesTransactions |
||
17 | { |
||
18 | /** |
||
19 | * Execute a Closure within a transaction. |
||
20 | * |
||
21 | * @param \Closure $callback |
||
22 | * @param int $attempts |
||
23 | * @return mixed |
||
24 | * |
||
25 | * @throws \Exception|\Throwable |
||
26 | */ |
||
27 | public function transaction(Closure $callback, $attempts = 1) |
||
55 | |||
56 | /** |
||
57 | * Handle an exception encountered when running a transacted statement. |
||
58 | * |
||
59 | * @param \Exception $e |
||
60 | * @param int $currentAttempt |
||
61 | * @param int $maxAttempts |
||
62 | * @return void |
||
63 | * |
||
64 | * @throws \Exception |
||
65 | */ |
||
66 | protected function handleTransactionException($e, $currentAttempt, $maxAttempts) |
||
90 | |||
91 | /** |
||
92 | * Start a new database transaction. |
||
93 | * |
||
94 | * @return void |
||
95 | * @throws \Exception |
||
96 | */ |
||
97 | public function beginTransaction() |
||
102 | |||
103 | /** |
||
104 | * Create a transaction within the database. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | protected function createTransaction() |
||
120 | |||
121 | /** |
||
122 | * Create a save point within the database. |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | protected function createSavepoint() |
||
132 | |||
133 | /** |
||
134 | * Handle an exception from a transaction beginning. |
||
135 | * |
||
136 | * @param \Exception $e |
||
137 | * @return void |
||
138 | * |
||
139 | * @throws \Exception |
||
140 | */ |
||
141 | protected function handleBeginTransactionException($e) |
||
151 | |||
152 | /** |
||
153 | * Commit the active database transaction. |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public function commit() |
||
165 | |||
166 | /** |
||
167 | * Rollback the active database transaction. |
||
168 | * |
||
169 | * @param int|null $toLevel |
||
170 | * @return void |
||
171 | */ |
||
172 | public function rollBack($toLevel = null) |
||
192 | |||
193 | /** |
||
194 | * Perform a rollback within the database. |
||
195 | * |
||
196 | * @param int $toLevel |
||
197 | * @return void |
||
198 | */ |
||
199 | protected function performRollBack($toLevel) |
||
209 | |||
210 | /** |
||
211 | * Get the number of active transactions. |
||
212 | * |
||
213 | * @return int |
||
214 | */ |
||
215 | public function transactionLevel() |
||
219 | |||
220 | /** |
||
221 | * Determine if the given exception was caused by a deadlock. |
||
222 | * |
||
223 | * @param \Exception $e |
||
224 | * @return bool |
||
225 | */ |
||
226 | protected function causedByDeadlock(Exception $e) |
||
240 | } |
||
241 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.