1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\IO\Inputs\Input; |
6
|
|
|
use Coco\SourceWatcher\Core\IO\Outputs\Output; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SourceWatcher |
10
|
|
|
* @package Coco\SourceWatcher\Core |
11
|
|
|
*/ |
12
|
|
|
class SourceWatcher |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var StepLoader |
16
|
|
|
*/ |
17
|
|
|
private StepLoader $stepLoader; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Pipeline |
21
|
|
|
*/ |
22
|
|
|
private Pipeline $pipeline; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* SourceWatcher constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct () |
28
|
|
|
{ |
29
|
|
|
$this->stepLoader = new StepLoader(); |
30
|
|
|
$this->pipeline = new Pipeline(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return StepLoader |
35
|
|
|
*/ |
36
|
|
|
public function getStepLoader () : StepLoader |
37
|
|
|
{ |
38
|
|
|
return $this->stepLoader; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return Pipeline |
43
|
|
|
*/ |
44
|
|
|
public function getPipeline () : Pipeline |
45
|
|
|
{ |
46
|
|
|
return $this->pipeline; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $extractorName |
51
|
|
|
* @param Input $input |
52
|
|
|
* @param array $options |
53
|
|
|
* @return $this |
54
|
|
|
* @throws SourceWatcherException |
55
|
|
|
*/ |
56
|
|
|
public function extract ( string $extractorName, Input $input, array $options = [] ) : SourceWatcher |
57
|
|
|
{ |
58
|
|
|
$extractor = $this->stepLoader->getStep( Extractor::class, $extractorName ); |
59
|
|
|
|
60
|
|
|
if ( $extractor == null ) { |
61
|
|
|
throw new SourceWatcherException( sprintf( "The extractor %s can't be found.", $extractorName ) ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$extractor->setInput( $input ); |
|
|
|
|
65
|
|
|
$extractor->options( $options ); |
66
|
|
|
|
67
|
|
|
$this->pipeline->setExtractor( $extractor ); |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $transformerName |
74
|
|
|
* @param array $options |
75
|
|
|
* @return $this |
76
|
|
|
* @throws SourceWatcherException |
77
|
|
|
*/ |
78
|
|
|
public function transform ( string $transformerName, array $options = [] ) : SourceWatcher |
79
|
|
|
{ |
80
|
|
|
$transformer = $this->stepLoader->getStep( Transformer::class, $transformerName ); |
81
|
|
|
|
82
|
|
|
if ( $transformer == null ) { |
83
|
|
|
throw new SourceWatcherException( sprintf( "The transformer %s can't be found.", $transformerName ) ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$transformer->options( $options ); |
87
|
|
|
|
88
|
|
|
$this->pipeline->pipe( $transformer ); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $loaderName |
95
|
|
|
* @param Output $output |
96
|
|
|
* @param array $options |
97
|
|
|
* @return $this |
98
|
|
|
* @throws SourceWatcherException |
99
|
|
|
*/ |
100
|
|
|
public function load ( string $loaderName, Output $output, array $options = [] ) : SourceWatcher |
101
|
|
|
{ |
102
|
|
|
$loader = $this->stepLoader->getStep( Loader::class, $loaderName ); |
103
|
|
|
|
104
|
|
|
if ( $loader == null ) { |
105
|
|
|
throw new SourceWatcherException( sprintf( "The loader %s can't be found.", $loaderName ) ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$loader->setOutput( $output ); |
|
|
|
|
109
|
|
|
$loader->options( $options ); |
110
|
|
|
|
111
|
|
|
$this->pipeline->pipe( $loader ); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function run () : void |
117
|
|
|
{ |
118
|
|
|
$this->pipeline->execute(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|