|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrainExe\Expression\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Inject; |
|
6
|
|
|
use BrainExe\Core\Annotations\Controller as ControllerAnnotation; |
|
7
|
|
|
use BrainExe\Core\Annotations\Route; |
|
8
|
|
|
use BrainExe\Core\Application\UserException; |
|
9
|
|
|
use BrainExe\Expression\Entity; |
|
10
|
|
|
use BrainExe\Expression\Gateway; |
|
11
|
|
|
use BrainExe\Expression\Language; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @ControllerAnnotation("Expression.Controller") |
|
16
|
|
|
*/ |
|
17
|
|
|
class Controller |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Gateway |
|
21
|
|
|
*/ |
|
22
|
|
|
private $gateway; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Language |
|
26
|
|
|
*/ |
|
27
|
|
|
private $language; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @Inject({ |
|
31
|
|
|
* "@Expression.Gateway", |
|
32
|
|
|
* "@Expression.Language" |
|
33
|
|
|
* }) |
|
34
|
|
|
* @param Gateway $gateway |
|
35
|
|
|
* @param Language $language |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( |
|
38
|
|
|
Gateway $gateway, |
|
39
|
|
|
Language $language |
|
40
|
|
|
) { |
|
41
|
|
|
$this->gateway = $gateway; |
|
42
|
|
|
$this->language = $language; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return Entity[] |
|
47
|
|
|
* @Route("/expressions/", name="expressions.load", methods="GET") |
|
48
|
|
|
*/ |
|
49
|
|
|
public function load() |
|
50
|
|
|
{ |
|
51
|
|
|
return [ |
|
52
|
|
|
'events' => include ROOT . '/cache/events.php', |
|
53
|
|
|
'expressions' => $this->gateway->getAll(), |
|
54
|
|
|
'functions' => $this->language->getFunctionNames() |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @Route("/expressions/", name="expressions.save", methods="PUT") |
|
60
|
|
|
* @param Request $request |
|
61
|
|
|
* @return Entity |
|
62
|
|
|
* @throws UserException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function save(Request $request) |
|
65
|
|
|
{ |
|
66
|
|
|
$expressionId = $request->request->get('expressionId'); |
|
67
|
|
|
|
|
68
|
|
|
$expressions = $this->gateway->getAll(); |
|
69
|
|
|
if (isset($expressions[$expressionId])) { |
|
70
|
|
|
$entity = $expressions[$expressionId]; |
|
71
|
|
|
} else { |
|
72
|
|
|
$entity = new Entity(); |
|
73
|
|
|
$entity->expressionId = $expressionId; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$entity->conditions = (array)$request->request->get('conditions'); |
|
77
|
|
|
$entity->actions = (array)$request->request->get('actions'); |
|
78
|
|
|
$entity->enabled = (bool)$request->request->get('enabled'); |
|
79
|
|
|
|
|
80
|
|
|
if (empty($entity->actions)) { |
|
81
|
|
|
throw new UserException(_('No actions defined')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
foreach ($entity->actions as $action) { |
|
85
|
|
|
$this->validate($action); |
|
86
|
|
|
} |
|
87
|
|
|
foreach ($entity->conditions as $condition) { |
|
88
|
|
|
$this->validate($condition); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->gateway->save($entity); |
|
92
|
|
|
|
|
93
|
|
|
return $entity; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param Request $request |
|
98
|
|
|
* @param $expressionId |
|
99
|
|
|
* @return bool |
|
100
|
|
|
* @Route("/expressions/{expressionId}/", name="expressions.delete", methods="DELETE") |
|
101
|
|
|
*/ |
|
102
|
|
|
public function delete(Request $request, $expressionId) |
|
103
|
|
|
{ |
|
104
|
|
|
unset($request); |
|
105
|
|
|
|
|
106
|
|
|
return $this->gateway->delete($expressionId); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param Request $request |
|
111
|
|
|
* @Route("/expressions/evaluate/", name="expressions.evaluate", methods="GET") |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function evaluate(Request $request) |
|
115
|
|
|
{ |
|
116
|
|
|
$expression = $request->query->get('expression'); |
|
117
|
|
|
|
|
118
|
|
|
return $this->language->evaluate($expression, array()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $expression |
|
124
|
|
|
*/ |
|
125
|
|
|
private function validate($expression) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->language->parse($expression, $this->language->getParameterNames()); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|