Skip to content

Commit 42ad951

Browse files
Update pipeline (#17)
## About - Adds `Conditionable` trait to the pipeline - Removes helpers, moves the `run` method to the class - Renames events to have consistent readable names
1 parent 400a4be commit 42ad951

File tree

11 files changed

+84
-104
lines changed

11 files changed

+84
-104
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ root = true
22

33
[*]
44
charset = utf-8
5-
indent_size = 2
5+
indent_size = 4
66
indent_style = space
77
end_of_line = lf
88
trim_trailing_whitespace = true

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
Laravel Pipeline with DB transaction support, events and additional methods.
1212

13-
The package requires `PHP 8` or higher and `Laravel 9` or higher.
13+
The package requires `PHP 8.1` or higher and `Laravel 10` or higher.
1414

1515
---
1616

@@ -49,14 +49,7 @@ Pipeline::make()
4949
});
5050
```
5151

52-
You can use the `pipeline` helper:
53-
```php
54-
pipeline($data, [
55-
// pipes
56-
])->thenReturn();
57-
```
58-
59-
You can as well instantiate the pipeline using IoC or manually:
52+
You can as well instantiate the pipeline using the service container or manually:
6053
```php
6154
app(Pipeline::class)
6255
...
@@ -69,12 +62,14 @@ app(Pipeline::class)
6962
...
7063
```
7164

72-
You can use the `run` helper to execute a single pipeline-compatible action:
65+
You can use the `run` method to execute a single pipe:
7366
```php
74-
run(MyAction::class, $data);
67+
$pipeline = Pipeline::make();
68+
69+
$pipeline->run(Pipe::class, $data);
7570
```
7671

77-
By default, `run` uses the `handle` method in your action classes, but if you use a different method name in your pipelines, you can fix that by adding code to your ServiceProvider:
72+
By default, `run` uses the `handle` method in your class as an entry point, but if you use a different method name in your pipelines, you can fix that by adding code to your ServiceProvider:
7873
```php
7974
$this->app->resolving(Pipeline::class, function ($pipeline) {
8075
return $pipeline->via('execute');
@@ -93,8 +88,8 @@ Usage of `withTransaction` method will enable a [`manual DB transaction`](https:
9388
Usage of `withEvents` method will enable [`Laravel Events`](https://laravel.com/docs/9.x/events#introduction) throughout the pipeline execution.
9489

9590
#### Available events
96-
- [`PipeStarted`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipeStarted.php) - fired **before** execution of pipe;
97-
- [`PipePassed`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipePassed.php) - fired **after** execution of pipe.
91+
- [`PipeExecutionStarted`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipeExecutionStarted.php) - fired **before** execution of the pipe;
92+
- [`PipeExecutionFinished`](https://github.com/michael-rubel/laravel-enhanced-pipeline/blob/main/src/Events/PipeExecutionFinished.php) - fired **after** execution of the pipe.
9893

9994
## Testing
10095
```bash

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
"autoload": {
3131
"psr-4": {
3232
"MichaelRubel\\EnhancedPipeline\\": "src"
33-
},
34-
"files": [
35-
"src/Helpers/helpers.php"
36-
]
33+
}
3734
},
3835
"autoload-dev": {
3936
"psr-4": {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MichaelRubel\EnhancedPipeline\Events;
66

7-
class PipeStarted
7+
class PipeExecutionFinished
88
{
99
/**
1010
* @param mixed $pipe
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MichaelRubel\EnhancedPipeline\Events;
66

7-
class PipePassed
7+
class PipeExecutionStarted
88
{
99
/**
1010
* @param mixed $pipe

src/Helpers/helpers.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/Pipeline.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
use Illuminate\Container\Container as ContainerConcrete;
99
use Illuminate\Contracts\Container\Container;
1010
use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;
11-
use MichaelRubel\EnhancedPipeline\Events\PipePassed;
12-
use MichaelRubel\EnhancedPipeline\Events\PipeStarted;
11+
use Illuminate\Support\Traits\Conditionable;
12+
use MichaelRubel\EnhancedPipeline\Events\PipeExecutionFinished;
13+
use MichaelRubel\EnhancedPipeline\Events\PipeExecutionStarted;
1314
use MichaelRubel\EnhancedPipeline\Traits\HasDatabaseTransactions;
1415
use MichaelRubel\EnhancedPipeline\Traits\HasEvents;
1516
use RuntimeException;
1617
use Throwable;
1718

1819
class Pipeline implements PipelineContract
1920
{
20-
use HasDatabaseTransactions, HasEvents;
21+
use Conditionable, HasDatabaseTransactions, HasEvents;
2122

2223
/**
2324
* The container implementation.
@@ -73,7 +74,7 @@ public static function make(?Container $container = null): Pipeline
7374
$container = ContainerConcrete::getInstance();
7475
}
7576

76-
return new Pipeline($container);
77+
return $container->make(static::class);
7778
}
7879

7980
/**
@@ -189,15 +190,15 @@ protected function carry()
189190
{
190191
return function ($stack, $pipe) {
191192
return function ($passable) use ($stack, $pipe) {
192-
$this->fireEvent(PipeStarted::class, $pipe, $passable);
193+
$this->fireEvent(PipeExecutionStarted::class, $pipe, $passable);
193194

194195
if (is_callable($pipe)) {
195196
// If the pipe is a callable, then we will call it directly, but otherwise we
196197
// will resolve the pipes out of the dependency container and call it with
197198
// the appropriate method and arguments, returning the results back out.
198199
$result = $pipe($passable, $stack);
199200

200-
$this->fireEvent(PipePassed::class, $pipe, $passable);
201+
$this->fireEvent(PipeExecutionFinished::class, $pipe, $passable);
201202

202203
return $result;
203204
} elseif (! is_object($pipe)) {
@@ -220,7 +221,7 @@ protected function carry()
220221
? $pipe->{$this->method}(...$parameters)
221222
: $pipe(...$parameters);
222223

223-
$this->fireEvent(PipePassed::class, $pipe, $passable);
224+
$this->fireEvent(PipeExecutionFinished::class, $pipe, $passable);
224225

225226
return $this->handleCarry($carry);
226227
};
@@ -302,6 +303,17 @@ public function onFailure(Closure $callback)
302303
return $this;
303304
}
304305

306+
/**
307+
* Run a single pipe.
308+
*/
309+
public function run(string $pipe, mixed $data = true): mixed
310+
{
311+
return $this
312+
->send($data)
313+
->through([$pipe])
314+
->thenReturn();
315+
}
316+
305317
/**
306318
* Handle the value returned from each pipe before passing it to the next.
307319
*

tests/OriginalPipelineTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,36 @@ public function testPipelineThenReturnMethodRunsPipelineThenReturnsPassable()
245245

246246
unset($_SERVER['__test.pipe.one']);
247247
}
248+
249+
public function testPipelineConditionable()
250+
{
251+
$result = (new Pipeline(new Container))
252+
->send('foo')
253+
->when(true, function (Pipeline $pipeline) {
254+
$pipeline->pipe([PipelineTestPipeOne::class]);
255+
})
256+
->then(function ($piped) {
257+
return $piped;
258+
});
259+
260+
$this->assertSame('foo', $result);
261+
$this->assertSame('foo', $_SERVER['__test.pipe.one']);
262+
unset($_SERVER['__test.pipe.one']);
263+
264+
$_SERVER['__test.pipe.one'] = null;
265+
$result = (new Pipeline(new Container))
266+
->send('foo')
267+
->when(false, function (Pipeline $pipeline) {
268+
$pipeline->pipe([PipelineTestPipeOne::class]);
269+
})
270+
->then(function ($piped) {
271+
return $piped;
272+
});
273+
274+
$this->assertSame('foo', $result);
275+
$this->assertNull($_SERVER['__test.pipe.one']);
276+
unset($_SERVER['__test.pipe.one']);
277+
}
248278
}
249279

250280
class PipelineTestPipeOne

tests/PipelineEventsTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use Illuminate\Support\Facades\Event;
88
use MichaelRubel\EnhancedPipeline\EnhancedPipelineServiceProvider;
9-
use MichaelRubel\EnhancedPipeline\Events\PipePassed;
10-
use MichaelRubel\EnhancedPipeline\Events\PipeStarted;
9+
use MichaelRubel\EnhancedPipeline\Events\PipeExecutionFinished;
10+
use MichaelRubel\EnhancedPipeline\Events\PipeExecutionStarted;
1111
use MichaelRubel\EnhancedPipeline\Pipeline;
1212

1313
class PipelineEventsTest extends TestCase
@@ -41,7 +41,7 @@ public function testFiresPipeStartedEvents()
4141
])
4242
->thenReturn();
4343

44-
Event::assertDispatched(function (PipeStarted $event) {
44+
Event::assertDispatched(function (PipeExecutionStarted $event) {
4545
$this->assertInstanceOf(TestPipe::class, app($event->pipe));
4646
$this->assertSame('data', $event->passable);
4747

@@ -59,14 +59,14 @@ public function testFiresPipeStartedEventsButFailsToPass()
5959
->onFailure(fn () => true)
6060
->thenReturn();
6161

62-
Event::assertDispatched(function (PipeStarted $event) {
62+
Event::assertDispatched(function (PipeExecutionStarted $event) {
6363
$this->assertInstanceOf(PipelineWithException::class, app($event->pipe));
6464
$this->assertSame('data', $event->passable);
6565

6666
return true;
6767
});
6868

69-
Event::assertNotDispatched(PipePassed::class);
69+
Event::assertNotDispatched(PipeExecutionFinished::class);
7070
}
7171

7272
/** @test */
@@ -81,7 +81,7 @@ public function testFiresPipePassedEvents()
8181
])
8282
->thenReturn();
8383

84-
Event::assertDispatched(function (PipePassed $event) {
84+
Event::assertDispatched(function (PipeExecutionFinished $event) {
8585
$this->assertInstanceOf(TestPipe::class, app($event->pipe));
8686
$this->assertSame('data', $event->passable);
8787

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,40 @@
66

77
use MichaelRubel\EnhancedPipeline\Pipeline;
88

9-
class PipelineRunHelperTest extends TestCase
9+
class PipelineRunTest extends TestCase
1010
{
11-
public function testRunHelperWithoutParams()
11+
public function testRunWithoutParams()
1212
{
13-
$executed = run(Action::class);
13+
$executed = Pipeline::make()->run(Action::class);
1414

1515
$this->assertTrue($executed);
1616
}
1717

18-
public function testRunHelperActionReturnsPassedData()
18+
public function testRunReturnsPassedData()
1919
{
2020
$data = ['test' => 'yeah'];
2121

22-
$executed = run(Action::class, with($data));
22+
$executed = Pipeline::make()->run(Action::class, with($data));
2323

2424
$this->assertSame('yeah', $executed['test']);
2525
}
2626

27-
public function testRunHelperHasCustomizableMethod()
27+
public function testRunHasCustomizableMethod()
28+
{
29+
$executed = Pipeline::make()
30+
->via('execute')
31+
->run(ActionExecute::class);
32+
33+
$this->assertTrue($executed);
34+
}
35+
36+
public function testRunHasCustomizableMethodViaContainer()
2837
{
2938
$this->app->resolving(Pipeline::class, function ($pipeline) {
3039
return $pipeline->via('execute');
3140
});
3241

33-
$executed = run(ActionExecute::class);
42+
$executed = Pipeline::make()->run(ActionExecute::class);
3443

3544
$this->assertTrue($executed);
3645
}

0 commit comments

Comments
 (0)