|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Oliverde8\Component\PhpEtl; |
|
6
|
|
|
|
|
7
|
|
|
use Oliverde8\Component\PhpEtl\Builder\Factories\AbstractFactory; |
|
8
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\ChainOperationInterface; |
|
9
|
|
|
use Oliverde8\Component\PhpEtl\Exception\UnknownOperationException; |
|
10
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ChainBuilder |
|
14
|
|
|
* |
|
15
|
|
|
* @author de Cramer Oliver<[email protected]> |
|
16
|
|
|
* @copyright 2018 Oliverde8 |
|
17
|
|
|
* @package Oliverde8\Component\PhpEtl |
|
18
|
|
|
*/ |
|
19
|
|
|
class ChainBuilder |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var AbstractFactory[] */ |
|
22
|
|
|
protected array $operationFactories; |
|
23
|
|
|
|
|
24
|
|
|
protected ExecutionContextFactoryInterface $contextFactory; |
|
25
|
|
|
|
|
26
|
|
|
protected ExpressionLanguage $expressionLanguage; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param ExecutionContextFactoryInterface $contextFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(ExecutionContextFactoryInterface $contextFactory) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->contextFactory = $contextFactory; |
|
34
|
|
|
|
|
35
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Register an operation factory. |
|
41
|
|
|
* |
|
42
|
|
|
* @param AbstractFactory $factory |
|
43
|
|
|
*/ |
|
44
|
|
|
public function registerFactory(AbstractFactory $factory) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->operationFactories[] = $factory; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get chain processor from configs. |
|
51
|
|
|
* |
|
52
|
|
|
* @throws Exception\ChainBuilderValidationException |
|
53
|
|
|
* @throws UnknownOperationException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function buildChainProcessor(array $configs, array $inputOptions = [], int $maxAsynchronousItems = 10): ChainProcessorInterface |
|
56
|
|
|
{ |
|
57
|
|
|
$chainOperations = []; |
|
58
|
|
|
foreach ($configs as $id => $operation) { |
|
59
|
|
|
$chainOperations[$id] = $this->getOperationFromConfig($operation, $inputOptions); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return new ChainProcessor($chainOperations, $this->contextFactory, $maxAsynchronousItems); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get chain operation instance from config. |
|
67
|
|
|
* |
|
68
|
|
|
* @throws Exception\ChainBuilderValidationException |
|
69
|
|
|
* @throws UnknownOperationException |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getOperationFromConfig(array $config, array $inputOptions): ChainOperationInterface |
|
72
|
|
|
{ |
|
73
|
|
|
foreach ($config['options'] as &$option) { |
|
74
|
|
|
if (is_string($option) && strpos($option, "!") === 0) { |
|
75
|
|
|
$option = ltrim($option, '!'); |
|
76
|
|
|
$option = $this->expressionLanguage->evaluate($option, $inputOptions); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
foreach ($this->operationFactories as $factory) { |
|
81
|
|
|
if ($factory->supports($config['operation'], $config['options'])) { |
|
82
|
|
|
return $factory->getOperation($config['operation'], $config['options']); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
throw new UnknownOperationException("No compatible factories were found for operation '{$config['operation']}'"); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|