-
-
Notifications
You must be signed in to change notification settings - Fork 135
Add support for AsPipeline concern #304
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
Open
stevenmaguire
wants to merge
11
commits into
lorisleiva:main
Choose a base branch
from
stevenmaguire:sm-add-pipeline-decorator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ea22de3
Remove trailing whitespace on empty lines
stevenmaguire 15b77de
Add support for AsPipeline concern
stevenmaguire 5da4c16
Remove superflous else condition
stevenmaguire 044569e
Add backup handling logic in PipelineDecorator if asPipeline method i…
stevenmaguire 8db4150
Update code style in the name of elegance
stevenmaguire 6aa97e6
Remove possible dead code path
stevenmaguire af9f9c5
Move pipeline closure resolution logic to decorator and expand test c…
stevenmaguire 96a9a19
Remove superfluous code from tests
stevenmaguire 4d936c0
Remover superflous label from test conditions
stevenmaguire b5f72e9
Add tests and reorganize code based on further refined requirements a…
stevenmaguire f501610
Update solution to ensure compatiblity with PHP 8.4
stevenmaguire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ trait AsAction | |
use AsJob; | ||
use AsCommand; | ||
use AsFake; | ||
use AsPipeline; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Concerns; | ||
|
||
trait AsPipeline | ||
{ | ||
// | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Decorators; | ||
|
||
use Closure; | ||
use Exception; | ||
use Lorisleiva\Actions\Concerns\DecorateActions; | ||
|
||
class PipelineDecorator | ||
{ | ||
use DecorateActions; | ||
|
||
public function __construct($action) | ||
{ | ||
$this->setAction($action); | ||
} | ||
|
||
public function __invoke(mixed ...$arguments): mixed | ||
{ | ||
$passable = array_shift($arguments); | ||
$closure = array_pop($arguments); | ||
|
||
$method = $this->hasMethod('asPipeline') ? 'asPipeline' : 'handle'; | ||
|
||
return $closure($this->callMethod($method, [$passable]) ?? $passable) ?? $passable; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\DesignPatterns; | ||
|
||
use Illuminate\Pipeline\Pipeline; | ||
use Lorisleiva\Actions\BacktraceFrame; | ||
use Lorisleiva\Actions\Concerns\AsPipeline; | ||
use Lorisleiva\Actions\Decorators\PipelineDecorator; | ||
use Lorisleiva\Actions\DesignPatterns\DesignPattern; | ||
|
||
class PipelineDesignPattern extends DesignPattern | ||
{ | ||
public function getTrait(): string | ||
{ | ||
return AsPipeline::class; | ||
} | ||
|
||
public function recognizeFrame(BacktraceFrame $frame): bool | ||
{ | ||
return $frame->matches(Pipeline::class, 'Illuminate\Pipeline\{closure}') | ||
|| $frame->matches(Pipeline::class, '{closure:{closure:Illuminate\Pipeline\Pipeline::carry():184}:185}'); | ||
} | ||
|
||
public function decorate($instance, BacktraceFrame $frame) | ||
{ | ||
return app(PipelineDecorator::class, ['action' => $instance]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Tests; | ||
|
||
use Illuminate\Support\Facades\Pipeline; | ||
use Lorisleiva\Actions\Concerns\AsAction; | ||
use Lorisleiva\Actions\Tests\Stubs\PipelinePassable; | ||
|
||
class AsPipelineTest | ||
{ | ||
use AsAction; | ||
|
||
public function handle(PipelinePassable $passable): void | ||
{ | ||
$passable->increment(); | ||
} | ||
|
||
public function asPipeline(PipelinePassable $passable): void | ||
{ | ||
$this->handle($passable); | ||
} | ||
} | ||
|
||
it('can run as a pipe in a pipeline, with an explicit asPipeline method', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->through([ | ||
AsPipelineTest::class, | ||
AsPipelineTest::class, | ||
AsPipelineTest::class, | ||
AsPipelineTest::class, | ||
]) | ||
->thenReturn(); | ||
|
||
expect(is_a($passable, PipelinePassable::class))->toBe(true); | ||
expect($passable->count)->toBe(4); | ||
}); | ||
|
||
it('can run with an arbitrary via method configured on Pipeline', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->via('arbitraryMethodThatDoesNotExistOnTheAction') | ||
lorisleiva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->through([ | ||
AsPipelineTest::class, | ||
app()->make(AsPipelineTest::class), | ||
]) | ||
->thenReturn(); | ||
|
||
expect(is_a($passable, PipelinePassable::class))->toBe(true); | ||
expect($passable->count)->toBe(2); | ||
}); | ||
|
||
it('can run as a pipe in a pipeline with only one explicit container resolved instance at the bottom of the stack', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->through([ | ||
AsPipelineTest::class, // implicit container resolved instance | ||
app()->make(AsPipelineTest::class), // explicit container resolved instance | ||
]) | ||
->thenReturn(); | ||
|
||
expect(is_a($passable, PipelinePassable::class))->toBe(true); | ||
expect($passable->count)->toBe(2); | ||
}); | ||
|
||
it('cannot run as a pipe in a pipeline with an explicit container resolved instance in the middle of the stack', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->through([ | ||
AsPipelineTest::class, // implicit container resolved instance | ||
app()->make(AsPipelineTest::class), // explicit container resolved instance | ||
AsPipelineTest::class, // implicit container resolved instance | ||
AsPipelineTest::class, // implicit container resolved instance | ||
]) | ||
->thenReturn(); | ||
|
||
expect(is_a($passable, PipelinePassable::class))->toBe(true); | ||
expect($passable->count)->toBe(2); | ||
}); | ||
|
||
it('cannot run as a pipe in a pipeline as an standalone instance', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->through([ | ||
new AsPipelineTest, // standalone instance | ||
AsPipelineTest::class, // implicit container resolved instance | ||
app()->make(AsPipelineTest::class), // explicit container resolved instance | ||
]) | ||
->thenReturn(); | ||
|
||
expect(is_null($passable))->toBe(true); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Tests; | ||
|
||
use Illuminate\Support\Facades\Pipeline; | ||
use Lorisleiva\Actions\Concerns\AsPipeline; | ||
use Lorisleiva\Actions\Tests\Stubs\PipelinePassable; | ||
|
||
class AsPipelineWithExplicitTraitTest | ||
{ | ||
use AsPipeline; | ||
|
||
public function handle(PipelinePassable $passable): void | ||
{ | ||
$passable->increment(); | ||
} | ||
} | ||
|
||
it('can run as a pipe in a pipeline, with explicit trait, without asPipeline method', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->through([ | ||
AsPipelineWithExplicitTraitTest::class, | ||
app()->make(AsPipelineWithExplicitTraitTest::class), | ||
]) | ||
->thenReturn(); | ||
|
||
expect(is_a($passable, PipelinePassable::class))->toBe(true); | ||
expect($passable->count)->toBe(2); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Tests; | ||
|
||
use Illuminate\Support\Facades\Pipeline; | ||
use Lorisleiva\Actions\Concerns\AsAction; | ||
use Lorisleiva\Actions\Tests\Stubs\PipelinePassable; | ||
|
||
class AsPipelineWithImplicitTraitTest | ||
{ | ||
use AsAction; | ||
|
||
public function handle(PipelinePassable $passable): void | ||
{ | ||
$passable->increment(); | ||
} | ||
} | ||
|
||
it('can run as a pipe in a pipeline, with implicit trait, without asPipeline method', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->through([ | ||
AsPipelineWithImplicitTraitTest::class, | ||
app()->make(AsPipelineWithImplicitTraitTest::class), | ||
]) | ||
->thenReturn(); | ||
|
||
expect(is_a($passable, PipelinePassable::class))->toBe(true); | ||
expect($passable->count)->toBe(2); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Tests; | ||
|
||
use ArgumentCountError; | ||
use Illuminate\Support\Facades\Pipeline; | ||
use Lorisleiva\Actions\Concerns\AsAction; | ||
use Lorisleiva\Actions\Tests\Stubs\PipelinePassable; | ||
use TypeError; | ||
|
||
class AsPipelineWithMultipleNonOptionalParametersTest | ||
{ | ||
use AsAction; | ||
|
||
public function handle(PipelinePassable $passable, int $nonOptionalAdditionalParameter): void | ||
{ | ||
$passable->increment(); | ||
} | ||
|
||
public function asPipeline(PipelinePassable $passable): PipelinePassable | ||
{ | ||
$this->handle($passable); | ||
|
||
return $passable; | ||
} | ||
} | ||
|
||
it('cannot run as a pipe in a pipeline expecting multiple non-optional parameters', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->through([ | ||
AsPipelineWithMultipleNonOptionalParametersTest::class, | ||
app()->make(AsPipelineWithMultipleNonOptionalParametersTest::class), | ||
]) | ||
->thenReturn(); | ||
})->throws(ArgumentCountError::class); | ||
|
||
it('cannot run as a pipe in a pipeline as an explicit container resolved instance preceding an implicit container resolved instance', function () { | ||
$passable = Pipeline::send(new PipelinePassable) | ||
->through([ | ||
app()->make(AsPipelineWithMultipleNonOptionalParametersTest::class), | ||
AsPipelineWithMultipleNonOptionalParametersTest::class, | ||
]) | ||
->thenReturn(); | ||
})->throws(TypeError::class); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Tests\Stubs; | ||
|
||
/** | ||
* Test fixture used in all of the AsPipeline{*} tests. | ||
*/ | ||
class PipelinePassable | ||
{ | ||
public function __construct(public int $count = 0) | ||
{ | ||
// | ||
} | ||
|
||
public function increment() | ||
{ | ||
$this->count++; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.