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
|
5 |
|
public function __construct( |
38
|
|
|
Gateway $gateway, |
39
|
|
|
Language $language |
40
|
|
|
) { |
41
|
5 |
|
$this->gateway = $gateway; |
42
|
5 |
|
$this->language = $language; |
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Entity[] |
47
|
|
|
* @Route("/expressions/", name="expressions.load", methods="GET") |
48
|
|
|
*/ |
49
|
1 |
|
public function load() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
1 |
|
'events' => include ROOT . '/cache/events.php', |
53
|
1 |
|
'expressions' => $this->gateway->getAll(), |
54
|
1 |
|
'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
|
2 |
|
public function save(Request $request) |
65
|
|
|
{ |
66
|
2 |
|
$expressionId = $request->request->get('expressionId'); |
67
|
|
|
|
68
|
2 |
|
$expressions = $this->gateway->getAll(); |
69
|
2 |
|
if (isset($expressions[$expressionId])) { |
70
|
1 |
|
$entity = $expressions[$expressionId]; |
71
|
|
|
} else { |
72
|
1 |
|
$entity = new Entity(); |
73
|
1 |
|
$entity->expressionId = $expressionId; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
$entity->conditions = (array)$request->request->get('conditions'); |
77
|
2 |
|
$entity->actions = (array)$request->request->get('actions'); |
78
|
2 |
|
$entity->enabled = (bool)$request->request->get('enabled'); |
79
|
|
|
|
80
|
2 |
|
if (empty($entity->actions)) { |
81
|
1 |
|
throw new UserException(_('No actions defined')); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
foreach ($entity->actions as $action) { |
85
|
1 |
|
$this->validate($action); |
86
|
|
|
} |
87
|
1 |
|
foreach ($entity->conditions as $condition) { |
88
|
1 |
|
$this->validate($condition); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$this->gateway->save($entity); |
92
|
|
|
|
93
|
1 |
|
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
|
1 |
|
public function delete(Request $request, $expressionId) |
103
|
|
|
{ |
104
|
1 |
|
unset($request); |
105
|
|
|
|
106
|
1 |
|
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
|
1 |
|
public function evaluate(Request $request) |
115
|
|
|
{ |
116
|
1 |
|
$expression = $request->query->get('expression'); |
117
|
|
|
|
118
|
1 |
|
return $this->language->evaluate($expression, array()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $expression |
124
|
|
|
*/ |
125
|
1 |
|
private function validate($expression) |
126
|
|
|
{ |
127
|
1 |
|
$this->language->parse($expression, $this->language->getParameterNames()); |
128
|
1 |
|
} |
129
|
|
|
} |
130
|
|
|
|