Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Illuminate/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class Pipeline implements PipelineContract
*/
protected $finally;

/**
* Indicates whether to wrap the pipeline in a database transaction.
*
* @var bool
*/
protected $withinTransactions = false;

/**
* Create a new class instance.
*
Expand Down Expand Up @@ -123,7 +130,9 @@ public function then(Closure $destination)
);

try {
return $pipeline($this->passable);
return $this->withinTransactions
? $this->container->make('db')->transaction(fn () => $pipeline($this->passable))
: $pipeline($this->passable);
} finally {
if ($this->finally) {
($this->finally)($this->passable);
Expand Down Expand Up @@ -245,6 +254,19 @@ protected function pipes()
return $this->pipes;
}

/**
* Execute each pipeline step within a database transaction.
*
* @param bool $withinTransactions
* @return $this
*/
public function withinTransactions(bool $withinTransactions = true)
{
$this->withinTransactions = $withinTransactions;

return $this;
}

/**
* Get the container instance.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Pipeline/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"Illuminate\\Pipeline\\": ""
}
},
"suggest": {
"illuminate/database": "Required to use database transactions (^12.0)."
},
"extra": {
"branch-alias": {
"dev-master": "12.x-dev"
Expand Down
56 changes: 56 additions & 0 deletions tests/Pipeline/PipelineTransactionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Illuminate\Tests\Pipeline;

use Exception;
use Illuminate\Database\Events\TransactionBeginning;
use Illuminate\Database\Events\TransactionCommitted;
use Illuminate\Database\Events\TransactionRolledBack;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Pipeline;
use Orchestra\Testbench\TestCase;

class PipelineTransactionTest extends TestCase
{
public function testPipelineTransaction()
{
Event::fake();

$result = Pipeline::withinTransactions()
->send('some string')
->through([function ($value, $next) {
return $next($value);
}])
->thenReturn();

$this->assertEquals('some string', $result);
Event::assertDispatched(TransactionBeginning::class);
Event::assertDispatched(TransactionCommitted::class);
}

public function testExceptionThrownRollsBackTransaction()
{
Event::fake();

$finallyRan = false;
try {
Pipeline::withinTransactions()
->send('some string')
->through([
function ($value, $next) {
throw new Exception('I was thrown');
},
])
->finally(function () use (&$finallyRan) {
$finallyRan = true;
})
->thenReturn();
$this->fail('No exception was thrown');
} catch (Exception) {
}

$this->assertTrue($finallyRan);
Event::assertDispatched(TransactionBeginning::class);
Event::assertDispatched(TransactionRolledBack::class);
}
}