|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/pipeline/blob/master/LICENSE.md |
|
6
|
|
|
* @link https://github.com/flipbox/pipeline |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Flipbox\Pipeline\Builders; |
|
10
|
|
|
|
|
11
|
|
|
use Flipbox\Pipeline\Pipelines\Pipeline; |
|
12
|
|
|
use Flipbox\Skeleton\Logger\AutoLoggerTrait; |
|
13
|
|
|
use League\Pipeline\PipelineInterface; |
|
14
|
|
|
use League\Pipeline\ProcessorInterface; |
|
15
|
|
|
use Psr\Log\InvalidArgumentException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Build a config based Pipeline |
|
19
|
|
|
* |
|
20
|
|
|
* @author Flipbox Digital <[email protected]> |
|
21
|
|
|
* @since 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
trait BuilderTrait |
|
24
|
|
|
{ |
|
25
|
|
|
use AutoLoggerTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var callable[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $stages = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Add an stage. |
|
34
|
|
|
* |
|
35
|
|
|
* @param callable $stage |
|
36
|
|
|
* |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function add(callable $stage) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->stages[] = $stage; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Build a new Pipeline object |
|
48
|
|
|
* |
|
49
|
|
|
* @param ProcessorInterface|null $processor |
|
50
|
|
|
* |
|
51
|
|
|
* @return PipelineInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
public function build(ProcessorInterface $processor = null): PipelineInterface |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
|
|
$config = [ |
|
57
|
|
|
'stages' => $this->stages |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
if ($processor !== null) { |
|
61
|
|
|
$config['processor'] = $processor; |
|
62
|
|
|
} |
|
63
|
|
|
return $this->createPipeline( |
|
64
|
|
|
$this->prepareConfig($config) |
|
65
|
|
|
); |
|
66
|
|
|
} catch (\Exception $e) { |
|
67
|
|
|
throw new InvalidArgumentException($e->getMessage()); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array $config |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function prepareConfig(array $config = []): array |
|
76
|
|
|
{ |
|
77
|
|
|
return $config; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array $config |
|
82
|
|
|
* @return PipelineInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function createPipeline(array $config = []): PipelineInterface |
|
85
|
|
|
{ |
|
86
|
|
|
return new Pipeline($config); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|