|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\El; |
|
4
|
|
|
|
|
5
|
|
|
use Jabe\Engine\Delegate\VariableScopeInterface; |
|
6
|
|
|
use Jabe\Engine\Impl\Core\Variable\Scope\AbstractVariableScope; |
|
7
|
|
|
use Jabe\Engine\Impl\Util\El\{ |
|
8
|
|
|
ArrayELResolver, |
|
9
|
|
|
CompositeELResolver, |
|
10
|
|
|
ELContext, |
|
11
|
|
|
ELResolver, |
|
12
|
|
|
ExpressionFactory, |
|
13
|
|
|
FunctionMapper, |
|
14
|
|
|
ListELResolver, |
|
15
|
|
|
MapELResolver, |
|
16
|
|
|
ValueExpression |
|
17
|
|
|
}; |
|
18
|
|
|
use Jabe\Engine\Impl\Juel\ExpressionFactoryImpl; |
|
19
|
|
|
use Jabe\Engine\Impl\Util\EnsureUtil; |
|
20
|
|
|
use Jabe\Engine\Test\Mock\MockElResolver; |
|
21
|
|
|
use Jabe\Engine\Variable\Context\VariableContextInterface; |
|
22
|
|
|
|
|
23
|
|
|
class JuelExpressionManager implements ExpressionManagerInterface, ElProviderCompatibleInterface |
|
24
|
|
|
{ |
|
25
|
|
|
protected $functions = []; |
|
26
|
|
|
protected $expressionFactory; |
|
27
|
|
|
protected $beans = []; |
|
28
|
|
|
protected $initialized = false; |
|
29
|
|
|
protected $elResolver; |
|
30
|
|
|
protected $functionMapper; |
|
31
|
|
|
// Default implementation (does nothing) |
|
32
|
|
|
protected $parsingElContext; |
|
33
|
|
|
protected $elProvider; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(array $beans = []) |
|
36
|
|
|
{ |
|
37
|
|
|
// Use the ExpressionFactoryImpl built-in version of juel, with parametrised |
|
38
|
|
|
// method expressions enabled |
|
39
|
|
|
$this->expressionFactory = new ExpressionFactoryImpl(); |
|
40
|
|
|
$this->beans = $beans; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function createExpression(string $expression): ExpressionInterface |
|
44
|
|
|
{ |
|
45
|
|
|
$this->ensureInitialized(); |
|
46
|
|
|
$valueExpression = $this->createValueExpression($expression); |
|
47
|
|
|
return new JuelExpression($valueExpression, $this, $expression); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function addFunction(string $name, \ReflectionMethod $function): void |
|
51
|
|
|
{ |
|
52
|
|
|
EnsureUtil::ensureNotEmpty("name", "name", $name); |
|
|
|
|
|
|
53
|
|
|
EnsureUtil::ensureNotNull("function", "function", $function); |
|
54
|
|
|
$this->functions[$name] = $function; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function createValueExpression(string $expression): ValueExpression |
|
58
|
|
|
{ |
|
59
|
|
|
$this->ensureInitialized(); |
|
60
|
|
|
$instance = new \stdClass(); |
|
61
|
|
|
return $this->expressionFactory->createValueExpression($parsingElContext, $expression, $instance); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function setExpressionFactory(ExpressionFactory $expressionFactory): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->expressionFactory = $expressionFactory; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getElContext(VariableScopeInterface $variableScope): ELContext |
|
70
|
|
|
{ |
|
71
|
|
|
$this->ensureInitialized(); |
|
72
|
|
|
$elContext = null; |
|
73
|
|
|
if ($variableScope instanceof AbstractVariableScopeInterface) { |
|
74
|
|
|
$variableScopeImpl = $variableScope; |
|
75
|
|
|
$elContext = $variableScopeImpl->getCachedElContext(); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($elContext === null) { |
|
79
|
|
|
$elContext = $this->createElContext($variableScope); |
|
80
|
|
|
if ($variableScope instanceof AbstractVariableScopeInterface) { |
|
81
|
|
|
$variableScope->setCachedElContext($elContext); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $elContext; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function createElContext(/*VariableScopeInterface|VariableContextInterface*/$variableScopeOrContext): ProcessEngineElContext |
|
89
|
|
|
{ |
|
90
|
|
|
if ($variableScopeOrContext instanceof VariableScopeInterface) { |
|
91
|
|
|
$this->ensureInitialized(); |
|
92
|
|
|
$elContext = new ProcessEngineElContext($this->functionMapper, $this->elResolver); |
|
93
|
|
|
$elContext->putContext(ExpressionFactory::class, $this->expressionFactory); |
|
94
|
|
|
$elContext->putContext(VariableScopeInterface::class, $variableScopeOrContext); |
|
95
|
|
|
return $elContext; |
|
96
|
|
|
} elseif ($variableScopeOrContext instanceof VariableContextInterface) { |
|
97
|
|
|
$this->ensureInitialized(); |
|
98
|
|
|
$elContext = new ProcessEngineElContext($this->functionMapper, $this->elResolver); |
|
99
|
|
|
$elContext->putContext(ExpressionFactory::class, $this->expressionFactory); |
|
100
|
|
|
$elContext->putContext(VariableContextInterface::class, $variableScopeOrContext); |
|
101
|
|
|
return $elContext; |
|
102
|
|
|
} |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function ensureInitialized(): void |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->initialized) { |
|
108
|
|
|
$this->elResolver = $this->createElResolver(); |
|
109
|
|
|
$this->functionMapper = $this->createFunctionMapper(); |
|
110
|
|
|
$this->parsingElContext = new ProcessEngineElContext($this->functionMapper); |
|
111
|
|
|
$this->initialized = true; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function createElResolver(VariableScopeInterface $scope = null): ELResolver |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$elResolver = new CompositeELResolver(); |
|
118
|
|
|
$elResolver->add(new VariableScopeElResolver()); |
|
119
|
|
|
$elResolver->add(new VariableContextElResolver()); |
|
120
|
|
|
$elResolver->add(new MockElResolver()); |
|
121
|
|
|
|
|
122
|
|
|
/*if (!empty($this->beans)) { |
|
123
|
|
|
// ACT-1102: Also expose all beans in configuration when using standalone |
|
124
|
|
|
// engine, not |
|
125
|
|
|
// in spring-context |
|
126
|
|
|
elResolver.add(new ReadOnlyMapELResolver(beans)); |
|
127
|
|
|
}*/ |
|
128
|
|
|
|
|
129
|
|
|
//$elResolver->add(new ProcessApplicationElResolverDelegate()); |
|
130
|
|
|
$elResolver->add(new ArrayELResolver()); |
|
131
|
|
|
$elResolver->add(new ListELResolver()); |
|
132
|
|
|
$elResolver->add(new MapELResolver()); |
|
133
|
|
|
//$elResolver->add(new ProcessApplicationBeanElResolverDelegate()); |
|
134
|
|
|
|
|
135
|
|
|
return $elResolver; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected function createFunctionMapper(): FunctionMapper |
|
139
|
|
|
{ |
|
140
|
|
|
$function = $this->functions; |
|
|
|
|
|
|
141
|
|
|
$functionMapper = new class ($functions) extends FunctionMapper { |
|
|
|
|
|
|
142
|
|
|
private $functions; |
|
143
|
|
|
|
|
144
|
|
|
public function __construct(array $functions) |
|
145
|
|
|
{ |
|
146
|
|
|
$this->functions = $functions; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function resolveFunction(string $prefix, string $localName): \ReflectionMethod |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->functions[$localName]; |
|
152
|
|
|
} |
|
153
|
|
|
}; |
|
154
|
|
|
return $functionMapper; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function toElProvider() |
|
158
|
|
|
{ |
|
159
|
|
|
if ($this->elProvider == null) { |
|
160
|
|
|
$this->elProvider = $this->createElProvider(); |
|
161
|
|
|
} |
|
162
|
|
|
return $this->elProvider; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
protected function createElProvider() |
|
166
|
|
|
{ |
|
167
|
|
|
return new ProcessEngineJuelElProvider($this); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|