Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] feat: Adding catch callback to Pipeline #54237

Draft
wants to merge 2 commits into
base: 11.x
Choose a base branch
from
Draft
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
31 changes: 25 additions & 6 deletions src/Illuminate/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class Pipeline implements PipelineContract
*/
protected $method = 'handle';

/**
* The catch callback to be executed when an exception is thrown.
*
* @var \Closure|null
*/
protected $catch;

/**
* The final callback to be executed after the pipeline ends regardless of the outcome.
*
Expand All @@ -51,7 +58,6 @@ class Pipeline implements PipelineContract
/**
* Create a new class instance.
*
* @param \Illuminate\Contracts\Container\Container|null $container
* @return void
*/
public function __construct(?Container $container = null)
Expand Down Expand Up @@ -114,7 +120,6 @@ public function via($method)
/**
* Run the pipeline with a final destination callback.
*
* @param \Closure $destination
* @return mixed
*/
public function then(Closure $destination)
Expand All @@ -125,6 +130,12 @@ public function then(Closure $destination)

try {
return $pipeline($this->passable);
} catch (Throwable $e) {
if ($this->catch) {
return ($this->catch)($this->passable, $e);
}

throw $e;
} finally {
if ($this->finally) {
($this->finally)($this->passable);
Expand All @@ -147,7 +158,6 @@ public function thenReturn()
/**
* Set a final callback to be executed after the pipeline ends regardless of the outcome.
*
* @param \Closure $callback
* @return $this
*/
public function finally(Closure $callback)
Expand All @@ -157,10 +167,21 @@ public function finally(Closure $callback)
return $this;
}

/**
* Set a catch callback to be executed when an exception is thrown.
*
* @return $this
*/
public function catch(Closure $callback)
{
$this->catch = $callback;

return $this;
}

/**
* Get the final piece of the Closure onion.
*
* @param \Closure $destination
* @return \Closure
*/
protected function prepareDestination(Closure $destination)
Expand Down Expand Up @@ -263,7 +284,6 @@ protected function getContainer()
/**
* Set the container instance.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return $this
*/
public function setContainer(Container $container)
Expand All @@ -288,7 +308,6 @@ protected function handleCarry($carry)
* Handle the given exception.
*
* @param mixed $passable
* @param \Throwable $e
* @return mixed
*
* @throws \Throwable
Expand Down
209 changes: 209 additions & 0 deletions tests/Pipeline/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
use Throwable;

class PipelineTest extends TestCase
{
Expand Down Expand Up @@ -356,6 +357,34 @@ function ($std, $next) {
$this->assertSame(4, $result->value);
}

public function testPipelineFinallyOutOfOrder()
{
$std = new stdClass;

($pipeline = new Pipeline(new Container))
->send($std)
->through([
function ($std, $next) {
$std->value = 100;

return $next($std);
},
function ($std, $next) {
$std->value = 200;

return $next($std);
},
])->then(function ($std) use (&$pipeline) {
$std->value = 300;

return $pipeline;
})->finally(function () {
$this->fail('Finally should not be called');
});

$this->assertSame(300, $std->value);
}

public function testPipelineFinallyWhenExceptionOccurs()
{
$std = new stdClass();
Expand Down Expand Up @@ -391,6 +420,186 @@ function ($std) {
throw $e;
}
}

public function testPipelineCatch()
{
$std = new stdClass;

(new Pipeline(new Container))
->send($std)
->through([
function ($std, $next) {
$std->value = 1;

return $next($std);
},
function ($std) {
throw new Exception('My Exception: '.$std->value);
},
function ($std, $next) {
$std->value++;

return $next($std);
},
])->catch(function ($std, Throwable $e) {
$std->value = 100;
$this->assertSame('My Exception: 1', $e->getMessage());
})
->then(function ($std) {
$this->assertSame(1, $std->value);

return $std;
});

$this->assertSame(100, $std->value);
}

public function testPipelineCatchWhenThereIsNoException()
{
$std = new stdClass;

(new Pipeline(new Container))
->send($std)
->through([
function ($std, $next) {
$std->value = 1;

return $next($std);
},
function ($std, $next) {
$std->value++;

return $next($std);
},
])->catch(function () {
$this->fail('Catch should not be called');
})
->then(function ($std) {
$this->assertSame(2, $std->value);

return $std;
});
}

public function testPipelineCatchAndFinallyWhenThereIsException()
{
$std = new stdClass;

(new Pipeline(new Container))
->send($std)
->through([
function ($std, $next) {
$std->value = 1;

return $next($std);
},
function ($std) {
$std->value++;
throw new Exception('My Exception: '.$std->value);
},
])->finally(function ($std) {
$this->assertSame(100, $std->value);

$std->value = 200;
})->catch(function ($std, Throwable $e) {
$std->value = 100;

$this->assertSame('My Exception: 2', $e->getMessage());
})->then(function ($std) {
$this->assertSame(2, $std->value);

return $std;
});

$this->assertSame(200, $std->value);
}

public function testPipelineCatchAndFinallyWhenThereIsNoException()
{
$std = new stdClass;

(new Pipeline(new Container))
->send($std)
->through([
function ($std, $next) {
$std->value = 1;

return $next($std);
},
])->catch(function () {
$this->fail('Catch should not be called');
})
->finally(function ($std) {
$this->assertSame(200, $std->value);
$std->value = 300;
})
->then(function ($std) {
$this->assertSame(1, $std->value);
$std->value = 200;

return $std;
});

$this->assertSame(300, $std->value);
}

public function testPipelineCatchOutOfOrder()
{
$std = new stdClass;

try {
(new Pipeline(new Container))
->send($std)
->through([
function ($std, $next) {
$std->value = 1;

return $next($std);
},
function ($std) {
$std->value++;
throw new Exception('My Exception: '.$std->value);
},
])->then(function () {
$this->fail('Then should not be called');
})->catch(function () {
$this->fail('Catch should not be called');
});
} catch (Throwable $e) {
$this->assertSame('My Exception: 2', $e->getMessage());
}

$this->assertSame(2, $std->value);
}

public function testPipelineCatchWithReturn()
{
$std = new stdClass;

$result = (new Pipeline(new Container))
->send($std)
->through([
function ($std) {
$std->value = 1;
throw new Exception('My Exception: '.$std->value);
},
])->catch(function ($std, Throwable $e) {
$this->assertSame(1, $std->value);
$std->value = 100;
$this->assertSame('My Exception: 1', $e->getMessage());

return 1000;
})
->then(function ($std) {
$this->assertSame(1, $std->value);
$std->value = 200;

return $std;
});

$this->assertSame(100, $std->value);
$this->assertSame(1000, $result);
}
}

class PipelineTestPipeOne
Expand Down
Loading