|
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
|
|
|
use Coco\SourceWatcher\Utils\Internationalization; |
|
8
|
|
|
use Ramsey\Uuid\Uuid; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class SourceWatcher |
|
13
|
|
|
* |
|
14
|
|
|
* @package Coco\SourceWatcher\Core |
|
15
|
|
|
*/ |
|
16
|
|
|
class SourceWatcher |
|
17
|
|
|
{ |
|
18
|
|
|
private StepLoader $stepLoader; |
|
19
|
|
|
|
|
20
|
|
|
private Pipeline $pipeline; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct () |
|
23
|
|
|
{ |
|
24
|
|
|
$this->stepLoader = new StepLoader(); |
|
25
|
|
|
$this->pipeline = new Pipeline(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getStepLoader () : StepLoader |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->stepLoader; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getPipeline () : Pipeline |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->pipeline; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $extractorName |
|
40
|
|
|
* @param Input $input |
|
41
|
|
|
* @param array $options |
|
42
|
|
|
* @return $this |
|
43
|
|
|
* @throws SourceWatcherException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function extract ( string $extractorName, Input $input, array $options = [] ) : SourceWatcher |
|
46
|
|
|
{ |
|
47
|
|
|
$extractor = $this->stepLoader->getStep( Extractor::class, $extractorName ); |
|
48
|
|
|
|
|
49
|
|
|
if ( $extractor == null ) { |
|
50
|
|
|
throw new SourceWatcherException( sprintf( Internationalization::getInstance()->getText( SourceWatcher::class, |
|
51
|
|
|
"Extractor_Not_Found" ), $extractorName ) ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$extractor->setInput( $input ); |
|
|
|
|
|
|
55
|
|
|
$extractor->options( $options ); |
|
56
|
|
|
|
|
57
|
|
|
$this->pipeline->setExtractor( $extractor ); |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $transformerName |
|
64
|
|
|
* @param array $options |
|
65
|
|
|
* @return $this |
|
66
|
|
|
* @throws SourceWatcherException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function transform ( string $transformerName, array $options = [] ) : SourceWatcher |
|
69
|
|
|
{ |
|
70
|
|
|
$transformer = $this->stepLoader->getStep( Transformer::class, $transformerName ); |
|
71
|
|
|
|
|
72
|
|
|
if ( $transformer == null ) { |
|
73
|
|
|
throw new SourceWatcherException( sprintf( Internationalization::getInstance()->getText( SourceWatcher::class, |
|
74
|
|
|
"Transformer_Not_Found" ), $transformerName ) ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$transformer->options( $options ); |
|
78
|
|
|
|
|
79
|
|
|
$this->pipeline->pipe( $transformer ); |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $loaderName |
|
86
|
|
|
* @param Output $output |
|
87
|
|
|
* @param array $options |
|
88
|
|
|
* @return $this |
|
89
|
|
|
* @throws SourceWatcherException |
|
90
|
|
|
*/ |
|
91
|
|
|
public function load ( string $loaderName, Output $output, array $options = [] ) : SourceWatcher |
|
92
|
|
|
{ |
|
93
|
|
|
$loader = $this->stepLoader->getStep( Loader::class, $loaderName ); |
|
94
|
|
|
|
|
95
|
|
|
if ( $loader == null ) { |
|
96
|
|
|
throw new SourceWatcherException( sprintf( Internationalization::getInstance()->getText( SourceWatcher::class, |
|
97
|
|
|
"Loader_Not_Found" ), $loaderName ) ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$loader->setOutput( $output ); |
|
|
|
|
|
|
101
|
|
|
$loader->options( $options ); |
|
102
|
|
|
|
|
103
|
|
|
$this->pipeline->pipe( $loader ); |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function run () : void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->pipeline->execute(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function flush () : void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->pipeline = new Pipeline(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function save ( string $name = null ) : string |
|
119
|
|
|
{ |
|
120
|
|
|
$arrayRepresentation = []; |
|
121
|
|
|
|
|
122
|
|
|
$steps = $this->pipeline->getSteps(); |
|
123
|
|
|
|
|
124
|
|
|
// Add the extractor to be on top of the array which by default is not returned in the list of steps |
|
125
|
|
|
array_unshift( $steps, $this->pipeline->getExtractor() ); |
|
126
|
|
|
|
|
127
|
|
|
foreach ( $steps as $currentStep ) { |
|
128
|
|
|
$arrayRepresentation[] = $currentStep->getArrayRepresentation(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$jsonRepresentation = json_encode( $arrayRepresentation, JSON_PRETTY_PRINT ); |
|
132
|
|
|
|
|
133
|
|
|
$mainDirectory = $_SERVER["HOME"] . DIRECTORY_SEPARATOR . ".source-watcher"; |
|
134
|
|
|
|
|
135
|
|
|
if ( !file_exists( $mainDirectory ) ) { |
|
136
|
|
|
mkdir( $mainDirectory, 0777, true ); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$transformationsDirectory = $mainDirectory . DIRECTORY_SEPARATOR . "transformations"; |
|
140
|
|
|
|
|
141
|
|
|
if ( !file_exists( $transformationsDirectory ) ) { |
|
142
|
|
|
mkdir( $transformationsDirectory, 0777, true ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if ( empty( $name ) ) { |
|
146
|
|
|
$uuid = Uuid::uuid4(); |
|
147
|
|
|
|
|
148
|
|
|
$name = $uuid->toString(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$transformationFile = $transformationsDirectory . DIRECTORY_SEPARATOR . $name . ".swt"; |
|
152
|
|
|
|
|
153
|
|
|
file_put_contents( $transformationFile, $jsonRepresentation ); |
|
154
|
|
|
|
|
155
|
|
|
return $transformationFile; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|