1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Extractors\ExecutionExtractor; |
6
|
|
|
use Coco\SourceWatcher\Core\IO\Inputs\ExtractorResultInput; |
7
|
|
|
use Iterator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Pipeline |
11
|
|
|
* |
12
|
|
|
* @package Coco\SourceWatcher\Core |
13
|
|
|
*/ |
14
|
|
|
class Pipeline implements Iterator |
15
|
|
|
{ |
16
|
|
|
private array $steps = []; |
17
|
|
|
|
18
|
|
|
private array $results = []; |
19
|
|
|
|
20
|
|
|
public function getSteps () : array |
21
|
|
|
{ |
22
|
|
|
return $this->steps; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function setSteps ( array $steps ) : void |
26
|
|
|
{ |
27
|
|
|
$this->steps = $steps; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function pipe ( Step $step ) : void |
31
|
|
|
{ |
32
|
|
|
if ( $step instanceof ExecutionExtractor ) { |
33
|
|
|
$step->setInput( new ExtractorResultInput( end( $this->steps ) ) ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->steps[] = $step; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function execute () : void |
40
|
|
|
{ |
41
|
|
|
foreach ( $this->steps as $index => $currentStep ) { |
42
|
|
|
if ( $currentStep instanceof Extractor ) { |
43
|
|
|
$this->results = $currentStep->extract(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ( $currentStep instanceof Transformer ) { |
47
|
|
|
foreach ( $this->results as $currentIndex => $currentItem ) { |
48
|
|
|
$currentStep->transform( $this->results[$currentIndex] ); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ( $currentStep instanceof Loader ) { |
53
|
|
|
foreach ( $this->results as $currentIndex => $currentItem ) { |
54
|
|
|
$currentStep->load( $currentItem ); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getResults () : array |
61
|
|
|
{ |
62
|
|
|
return $this->results; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private int $index = 0; |
66
|
|
|
|
67
|
|
|
public function current () |
68
|
|
|
{ |
69
|
|
|
return $this->results[$this->index]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function next () |
73
|
|
|
{ |
74
|
|
|
$this->index++; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function key () |
78
|
|
|
{ |
79
|
|
|
return $this->index; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function valid () |
83
|
|
|
{ |
84
|
|
|
return isset( $this->results[$this->key()] ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function rewind () |
88
|
|
|
{ |
89
|
|
|
$this->index = 0; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|