|
6 | 6 | use Illuminate\Contracts\Container\Container; |
7 | 7 | use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract; |
8 | 8 | use Illuminate\Support\Traits\Conditionable; |
| 9 | +use Illuminate\Support\Traits\Macroable; |
9 | 10 | use RuntimeException; |
10 | 11 | use Throwable; |
11 | 12 |
|
12 | 13 | class Pipeline implements PipelineContract |
13 | 14 | { |
14 | 15 | use Conditionable; |
| 16 | + use Macroable; |
15 | 17 |
|
16 | 18 | /** |
17 | 19 | * The container implementation. |
@@ -48,6 +50,13 @@ class Pipeline implements PipelineContract |
48 | 50 | */ |
49 | 51 | protected $finally; |
50 | 52 |
|
| 53 | + /** |
| 54 | + * Indicates whether to wrap the pipeline in a database transaction. |
| 55 | + * |
| 56 | + * @var string|null|\UnitEnum|false |
| 57 | + */ |
| 58 | + protected $withinTransaction = false; |
| 59 | + |
51 | 60 | /** |
52 | 61 | * Create a new class instance. |
53 | 62 | * |
@@ -123,7 +132,9 @@ public function then(Closure $destination) |
123 | 132 | ); |
124 | 133 |
|
125 | 134 | try { |
126 | | - return $pipeline($this->passable); |
| 135 | + return $this->withinTransaction !== false |
| 136 | + ? $this->getContainer()->make('db')->connection($this->withinTransaction)->transaction(fn () => $pipeline($this->passable)) |
| 137 | + : $pipeline($this->passable); |
127 | 138 | } finally { |
128 | 139 | if ($this->finally) { |
129 | 140 | ($this->finally)($this->passable); |
@@ -245,6 +256,19 @@ protected function pipes() |
245 | 256 | return $this->pipes; |
246 | 257 | } |
247 | 258 |
|
| 259 | + /** |
| 260 | + * Execute each pipeline step within a database transaction. |
| 261 | + * |
| 262 | + * @param string|null|\UnitEnum|false $withinTransaction |
| 263 | + * @return $this |
| 264 | + */ |
| 265 | + public function withinTransaction($withinTransaction = null) |
| 266 | + { |
| 267 | + $this->withinTransaction = $withinTransaction; |
| 268 | + |
| 269 | + return $this; |
| 270 | + } |
| 271 | + |
248 | 272 | /** |
249 | 273 | * Get the container instance. |
250 | 274 | * |
|
0 commit comments