|
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 Coco\SourceWatcher\Utils\FileUtils; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Iterator; |
|
10
|
|
|
use Monolog\Handler\StreamHandler; |
|
11
|
|
|
use Monolog\Logger; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Pipeline |
|
15
|
|
|
* |
|
16
|
|
|
* @package Coco\SourceWatcher\Core |
|
17
|
|
|
*/ |
|
18
|
|
|
class Pipeline implements Iterator |
|
19
|
|
|
{ |
|
20
|
|
|
private array $steps = []; |
|
21
|
|
|
|
|
22
|
|
|
private array $results = []; |
|
23
|
|
|
|
|
24
|
|
|
private Logger $logger; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct () |
|
27
|
|
|
{ |
|
28
|
|
|
$this->logger = new Logger( "Connector" ); |
|
29
|
|
|
|
|
30
|
|
|
$streamPath = FileUtils::file_build_path( __DIR__, "..", "..", "..", "..", "logs", |
|
31
|
|
|
"Connector" . "-" . gmdate( "Y-m-d-H-i-s", time() ) . "-" . getmypid() . ".txt" ); |
|
32
|
|
|
|
|
33
|
|
|
$this->logger->pushHandler( new StreamHandler( $streamPath ), Logger::DEBUG ); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getSteps () : array |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->steps; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function setSteps ( array $steps ) : void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->steps = $steps; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function pipe ( Step $step ) : void |
|
47
|
|
|
{ |
|
48
|
|
|
if ( $step instanceof ExecutionExtractor ) { |
|
49
|
|
|
$step->setInput( new ExtractorResultInput( end( $this->steps ) ) ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->steps[] = $step; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function execute () : void |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ( $this->steps as $index => $currentStep ) { |
|
58
|
|
|
if ( $currentStep instanceof Extractor ) { |
|
59
|
|
|
$this->results = $currentStep->extract(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ( $currentStep instanceof Transformer ) { |
|
63
|
|
|
foreach ( $this->results as $currentIndex => $currentItem ) { |
|
64
|
|
|
$currentStep->transform( $this->results[$currentIndex] ); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ( $currentStep instanceof Loader ) { |
|
69
|
|
|
foreach ( $this->results as $currentIndex => $currentItem ) { |
|
70
|
|
|
try { |
|
71
|
|
|
$currentStep->load( $currentItem ); |
|
72
|
|
|
} catch ( Exception $exception ) { |
|
73
|
|
|
$this->logger->debug( $exception->getMessage() ); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getResults () : array |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->results; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private int $index = 0; |
|
86
|
|
|
|
|
87
|
|
|
public function current () |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->results[$this->index]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function next () |
|
93
|
|
|
{ |
|
94
|
|
|
$this->index++; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function key () |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->index; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function valid () |
|
103
|
|
|
{ |
|
104
|
|
|
return isset( $this->results[$this->key()] ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function rewind () |
|
108
|
|
|
{ |
|
109
|
|
|
$this->index = 0; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.