forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormalExpression.php
More file actions
399 lines (368 loc) · 11.1 KB
/
FormalExpression.php
File metadata and controls
399 lines (368 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
namespace ProcessMaker\Models;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use ProcessMaker\Exception\ExpressionFailedException;
use ProcessMaker\Exception\ScriptLanguageNotSupported;
use ProcessMaker\Exception\SyntaxErrorException;
use ProcessMaker\Nayra\Bpmn\BaseTrait;
use ProcessMaker\Nayra\Contracts\Bpmn\FormalExpressionInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\SyntaxError;
use Throwable;
/**
* FormalExpression
*/
class FormalExpression implements FormalExpressionInterface
{
use BaseTrait;
const defaultLanguage = 'FEEL';
const templateEngine = 'Mustache';
/**
* Languages supported for expressions
*/
const languages = [
'FEEL' => ['feelExpression', 'feelEncode'],
];
private static $pmFunctions = [];
/**
* FEEL expression object to be used to evaluate
* @var \Symfony\Component\ExpressionLanguage\ExpressionLanguage
*/
private $feelExpression;
/**
* Initialize the expression language evaluator
*/
protected function initFormalExpression()
{
$this->feelExpression = new ExpressionLanguage();
$this->registerPMFunctions();
}
/**
* Register a custom PM function
*
* @param callable $callable
*/
public function registerPMFunction($name, callable $callable)
{
static::$pmFunctions[$name] = $callable;
}
/**
* Register system functions
*/
private function registerPMFunctions()
{
$this->feelExpression->register(
'getActiveTaskAt',
function () {
},
function ($arguments, $element_id, $request_id) {
$task = ProcessRequestToken::where('element_id', $element_id)
->where('process_request_id', $request_id)
->where('status', 'ACTIVE')
->first();
return $task;
}
);
// Escalate a task to the assigned user's manager
$this->feelExpression->register(
'escalateTask',
function () {
},
function ($arguments, $task_id) {
$task = ProcessRequestToken::find($task_id);
$task->reassignTo('#manager')->save();
}
);
// Escalate a task to the assigned user's delegation user
$this->feelExpression->register(
'delegateTask',
function () {
},
function ($arguments, $task_id) {
$task = ProcessRequestToken::find($task_id);
if ($task->user) {
$delegationUserId = $task->user->delegation_user_id;
$task->reassignTo($delegationUserId)->save();
}
}
);
$this->feelExpression->register(
'reassignTasks',
function () {
},
function ($arguments, $user_id, $target_user_id) {
$activeTasks = ProcessRequestToken::where('user_id', $user_id)
->where('element_type', 'task')
->where('status', 'ACTIVE')
->get();
foreach ($activeTasks as $task) {
$task->reassignTo($target_user_id)->save();
}
return $target_user_id;
}
);
$this->feelExpression->register(
'get',
function () {
},
function ($arguments, $o, $a) {
return ((array) $o)[$a];
}
);
// date($format, $timestamp)
$this->feelExpression->register(
'date',
function () {
},
function ($arguments, $a, $b = null, $timezone = null) {
if (empty($timezone)) {
$timezone = config('app.timezone');
}
if ($b) {
return Carbon::createFromTimestamp($b, $timezone)->format($a);
} else {
return Carbon::now($timezone)->format($a);
}
}
);
// empty($name)
$this->feelExpression->register(
'empty',
function () {
},
function ($data, $name) {
return empty($data[$name]);
}
);
// env($name)
$this->feelExpression->register(
'env',
function () {
},
function ($__data, $name) {
$env = EnvironmentVariable::where('name', $name)->first();
if ($env) {
return $env->value;
}
return env($name);
}
);
// process($id)
$this->feelExpression->register(
'process',
function () {
},
function ($__data, $id) {
return Process::find($id);
}
);
// request($id)
$this->feelExpression->register(
'request',
function () {
},
function ($__data, $id) {
return ProcessRequest::find($id);
}
);
// user($id)
$this->feelExpression->register(
'user',
function () {
},
function ($__data, $id) {
return User::find($id);
}
);
// lowercase($str)
$this->feelExpression->register(
'lowercase',
function () {
},
function ($__data, $str) {
return strtolower($str);
}
);
// uppercase($str)
$this->feelExpression->register(
'uppercase',
function () {
},
function ($__data, $str) {
return strtoupper($str);
}
);
// count($array)
$this->feelExpression->register(
'count',
function () {
},
function ($__data, $array) {
return count($array);
}
);
//group_manager($user_id, $assigned_groups)
$this->feelExpression->register(
'group_manager',
function () {
},
function ($__data, $user_id, $assigned_groups) {
if (!$user_id) {
return null;
}
$user = User::find($user_id);
if (!$user) {
return null;
}
$user_groups = $user->groups()->get();
foreach ($user_groups as $group) {
if (in_array($group->id, $assigned_groups) && $group->manager_id) {
return $group->manager_id;
}
}
if (!$user->manager_id) {
// If no manager is found, then assign the task to the Process Manager.
$request = ProcessRequest::find($__data['_request']['id']);
$process = $request->processVersion;
return $process->manager_id;
}
return $user->manager_id;
}
);
// arrayget($array, $key, $default)
// similar to laravel's Arr:get: arrayget($myarray, 'field1.subfield2', false)
$this->feelExpression->register(
'arrayget',
function () {
},
function ($arguments, $array, $key, $default) {
return Arr::get($array, $key, $default);
}
);
// Register global PM functions from packages
foreach (static::$pmFunctions as $name => $callable) {
$this->feelExpression->register(
$name,
function () {
},
$callable
);
}
}
/**
* Prepare the data for the FEEL evaluator
*
* @param array $data
*
* @return array
*/
private function feelEncode(array $data)
{
return (array) json_decode(json_encode($data));
}
private function getTemplateEngine()
{
if (self::templateEngine == 'Mustache') {
return new MustacheExpressionEvaluator();
} else {
throw new \Exception('Template engine not supported');
}
}
/**
* Get the body of the Expression.
*
* @return string
*/
public function getBody()
{
return $this->getProperty(FormalExpressionInterface::BPMN_PROPERTY_BODY);
}
/**
* Get the body of the Expression.
*
* @param string $body
*
* @return $this
*/
public function setBody($body)
{
$this->setProperty(FormalExpressionInterface::BPMN_PROPERTY_BODY, $body);
return $this;
}
/**
* Get the type that this Expression returns when evaluated.
*
* @return string
*/
public function getEvaluatesToType()
{
return $this->getProperty(FormalExpressionInterface::BPMN_PROPERTY_EVALUATES_TO_TYPE_REF);
}
/**
* Get the expression language.
*
* @return string
*/
public function getLanguage()
{
return $this->getProperty(FormalExpressionInterface::BPMN_PROPERTY_LANGUAGE);
}
/**
* Get the expression language.
*
* @param string $language
*
* @return $this
*/
public function setLanguage($language)
{
$this->setProperty(FormalExpressionInterface::BPMN_PROPERTY_LANGUAGE, $language);
return $this;
}
/**
* Invoke the format expression.
*
* @param mixed $data
*
* @return string
*/
public function __invoke($data)
{
return $this->evaluate($this->getTemplateEngine(), $this->getBody(), $data);
}
/**
* Evaluate an expression using an specific template engine
*
* @param \ProcessMaker\Contracts\TemplateExpressionInterface $templateEngine
* @param string $expression
* @param array $data
*
* @return bool
*
* @throws ExpressionFailedException
* @throws ScriptLanguageNotSupported
* @throws SyntaxErrorException
*/
private function evaluate($templateEngine, $expression, array $data)
{
$body = $templateEngine->render($expression, $data);
if (!trim($body)) {
return true;
}
$language = $this->getLanguage() ?: self::defaultLanguage;
if (!isset(self::languages[$language])) {
throw new ScriptLanguageNotSupported($language);
}
$evaluator = self::languages[$language][0];
$encoder = isset(self::languages[$language][1]) ? self::languages[$language][1] : null;
$values = $encoder ? $this->$encoder($data) : $data;
try {
return $this->$evaluator->evaluate($body, $values);
} catch (SyntaxError $syntaxError) {
throw new SyntaxErrorException($syntaxError, $body, $values);
} catch (Throwable $error) {
throw new ExpressionFailedException($error);
}
}
}