1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Pipeline |
9
|
|
|
* @package Coco\SourceWatcher\Core |
10
|
|
|
*/ |
11
|
|
|
class Pipeline implements Iterator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Extractor |
15
|
|
|
*/ |
16
|
|
|
private ?Extractor $extractor = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private array $steps = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private array $results = []; |
27
|
|
|
|
28
|
|
|
public function __construct () |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Extractor |
35
|
|
|
*/ |
36
|
|
|
public function getExtractor () : Extractor |
37
|
|
|
{ |
38
|
|
|
return $this->extractor; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Extractor $extractor |
43
|
|
|
*/ |
44
|
|
|
public function setExtractor ( Extractor $extractor ) : void |
45
|
|
|
{ |
46
|
|
|
$this->extractor = $extractor; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getSteps () : array |
53
|
|
|
{ |
54
|
|
|
return $this->steps; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $steps |
59
|
|
|
*/ |
60
|
|
|
public function setSteps ( array $steps ) : void |
61
|
|
|
{ |
62
|
|
|
$this->steps = $steps; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getResults () : array |
69
|
|
|
{ |
70
|
|
|
return $this->results; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $results |
75
|
|
|
*/ |
76
|
|
|
public function setResults ( array $results ) : void |
77
|
|
|
{ |
78
|
|
|
$this->results = $results; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private int $index = 0; |
82
|
|
|
|
83
|
|
|
public function current () |
84
|
|
|
{ |
85
|
|
|
return $this->results[$this->index]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function next () |
89
|
|
|
{ |
90
|
|
|
$this->index++; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function key () |
94
|
|
|
{ |
95
|
|
|
return $this->index; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function valid () |
99
|
|
|
{ |
100
|
|
|
return isset( $this->results[$this->key()] ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function rewind () |
104
|
|
|
{ |
105
|
|
|
$this->index = 0; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Step $step |
110
|
|
|
*/ |
111
|
|
|
public function pipe ( Step $step ) : void |
112
|
|
|
{ |
113
|
|
|
$this->steps[] = $step; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function execute () : void |
117
|
|
|
{ |
118
|
|
|
$this->results = $this->extractor->extract(); |
|
|
|
|
119
|
|
|
|
120
|
|
|
foreach ( $this->steps as $currentStep ) { |
121
|
|
|
if ( $currentStep instanceof Transformer ) { |
122
|
|
|
foreach ( $this->results as $currentIndex => $currentItem ) { |
123
|
|
|
$currentStep->transform( $this->results[$currentIndex] ); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ( $currentStep instanceof Loader ) { |
128
|
|
|
foreach ( $this->results as $currentIndex => $currentItem ) { |
129
|
|
|
$currentStep->load( $currentItem ); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|