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