|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Edyan\Neuralyzer\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
|
6
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
7
|
|
|
|
|
8
|
|
|
class Expression |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Container injected by autowiring |
|
12
|
|
|
* @var ContainerInterface |
|
13
|
|
|
*/ |
|
14
|
|
|
private $container; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* List of "things" to inject in expressions evaluation |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $services = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Used for autowiring |
|
24
|
|
|
* @param ContainerInterface $container |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(ContainerInterface $container) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->container = $container; |
|
29
|
|
|
$this->configure(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Return all registered services |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getServices(): array |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->services; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Evaluate an expression that would be in the most general case |
|
43
|
|
|
* an action coming from an Anonymization config |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $expression |
|
46
|
|
|
*/ |
|
47
|
|
|
public function evaluateExpression(string $expression) |
|
48
|
|
|
{ |
|
49
|
|
|
$expressionLanguage = new ExpressionLanguage(); |
|
50
|
|
|
|
|
51
|
|
|
return $expressionLanguage->evaluate($expression, $this->services); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Evaluate a list of expression |
|
57
|
|
|
* @param array $expressions |
|
58
|
|
|
*/ |
|
59
|
|
|
public function evaluateExpressions(array $expressions) |
|
60
|
|
|
{ |
|
61
|
|
|
$res = []; |
|
62
|
|
|
foreach ($expressions as $expression) { |
|
63
|
|
|
$res[] = $this->evaluateExpression($expression); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $res; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Configure that service by registering all services in an array |
|
71
|
|
|
*/ |
|
72
|
|
|
private function configure() |
|
73
|
|
|
{ |
|
74
|
|
|
$services = array_keys($this->container->findTaggedServiceIds('app.service')); |
|
|
|
|
|
|
75
|
|
|
foreach ($services as $service) { |
|
76
|
|
|
$service = $this->container->get($service); |
|
77
|
|
|
$this->services[$service->getName()] = $service; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|