1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Khusseini\PimcoreRadBrickBundle; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use stdClass; |
7
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
8
|
|
|
|
9
|
|
|
class DatasourceRegistry |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ExpressionLanguage |
13
|
|
|
*/ |
14
|
|
|
private $expressionLangauge; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var object |
18
|
|
|
*/ |
19
|
|
|
private $datasources = null; |
20
|
|
|
|
21
|
18 |
|
private $executedData = []; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
18 |
|
ExpressionLanguage $expressionLangauge = null |
25
|
18 |
|
) { |
26
|
|
|
if (!$expressionLangauge) { |
27
|
|
|
$expressionLangauge = new ExpressionLanguage(); |
28
|
18 |
|
} |
29
|
18 |
|
|
30
|
18 |
|
$this->expressionLangauge = $expressionLangauge; |
31
|
|
|
$this->datasources = new stdClass(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array<string> $args |
36
|
|
|
* |
37
|
6 |
|
* @return mixed |
38
|
|
|
*/ |
39
|
6 |
|
public function execute(string $name, array $args = []) |
40
|
1 |
|
{ |
41
|
|
|
if (!$this->has($name)) { |
42
|
|
|
throw new InvalidArgumentException(sprintf('Datasource \'%s\' not found.', $name)); |
43
|
5 |
|
} |
44
|
|
|
|
45
|
5 |
|
$ds = $this->datasources->{$name}; |
46
|
|
|
$this->executedData[$name] = $ds(...$args); |
47
|
|
|
|
48
|
6 |
|
return $this->executedData[$name]; |
49
|
|
|
} |
50
|
6 |
|
|
51
|
|
|
public function hasData(string $name): bool |
52
|
|
|
{ |
53
|
6 |
|
return isset($this->executedData[$name]) && (bool) $this->executedData[$name]; |
54
|
|
|
} |
55
|
6 |
|
|
56
|
6 |
|
public function getDataContainer(): array |
57
|
6 |
|
{ |
58
|
|
|
return $this->executedData; |
59
|
6 |
|
} |
60
|
|
|
|
61
|
12 |
|
public function has(string $name): bool |
62
|
|
|
{ |
63
|
12 |
|
return property_exists($this->datasources, $name); |
64
|
12 |
|
} |
65
|
|
|
|
66
|
|
|
public function getData(string $name, bool $execute = false, array $execArgs = []) |
67
|
|
|
{ |
68
|
|
|
if ($execute) { |
69
|
10 |
|
$this->execute($name, $execArgs); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!$this->hasData($name)) { |
73
|
|
|
return []; |
74
|
|
|
} |
75
|
9 |
|
|
76
|
9 |
|
return $this->executedData[$name]; |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
public function executeAll(): \Generator |
80
|
10 |
|
{ |
81
|
|
|
$ds = (array) $this->datasources; |
82
|
|
|
foreach ($ds as $name => $callback) { |
83
|
9 |
|
$this->executedData[$name] = $callback(); |
84
|
|
|
$data = $this->executedData[$name]; |
85
|
9 |
|
yield $name => $data; |
86
|
9 |
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
public function add(string $name, callable $callable): void |
90
|
4 |
|
{ |
91
|
4 |
|
$this->datasources->{$name} = $callable; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
4 |
|
* @param array<string> $args |
96
|
|
|
*/ |
97
|
|
|
public function createMethodCall( |
98
|
|
|
object $service, |
99
|
|
|
string $method, |
100
|
|
|
array $args |
101
|
|
|
): callable { |
102
|
|
|
return function (array $input) use ($service, $method, $args) { |
103
|
9 |
|
foreach ($args as $index => $expression) { |
104
|
|
|
$args[$index] = $this->getValueRecursive($input, $expression); |
105
|
|
|
} |
106
|
9 |
|
|
107
|
1 |
|
return \call_user_func_array([$service, $method], $args); |
108
|
1 |
|
}; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function getValueRecursive(array $input, $expression) |
112
|
|
|
{ |
113
|
|
|
if (\is_string($expression)) { |
114
|
|
|
return $this->getValue($input, $expression); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (\is_array($expression)) { |
118
|
|
|
foreach ($expression as $key => $value) { |
119
|
|
|
$expression[$key] = $this->getValueRecursive($input, $value); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $expression; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array<array> $context |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
public function getValue(array $context, string $expression) |
132
|
|
|
{ |
133
|
|
|
try { |
134
|
|
|
return $this->expressionLangauge->evaluate($expression, $context); |
135
|
|
|
} catch (\Exception $ex) { |
136
|
|
|
return $expression; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|