|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrainExe\Expression; |
|
4
|
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Service; |
|
6
|
|
|
use BrainExe\Core\EventDispatcher\AbstractEvent; |
|
7
|
|
|
use BrainExe\Core\EventDispatcher\Events\TimingEvent; |
|
8
|
|
|
use BrainExe\Core\Traits\EventDispatcherTrait; |
|
9
|
|
|
use BrainExe\Core\Traits\LoggerTrait; |
|
10
|
|
|
use BrainExe\InputControl\InputControlEvent; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
use ReflectionClass; |
|
13
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
|
14
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
15
|
|
|
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @Service("Expression.Language", public=false, lazy=true) |
|
19
|
|
|
*/ |
|
20
|
|
|
class Language extends ExpressionLanguage |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
use EventDispatcherTrait; |
|
24
|
|
|
use LoggerTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function __construct(ParserCacheInterface $cache = null, array $providers = []) |
|
30
|
|
|
{ |
|
31
|
4 |
|
parent::__construct($cache, $providers); |
|
32
|
|
|
|
|
33
|
|
|
$functions = [ |
|
34
|
4 |
|
'sprintf', |
|
35
|
|
|
'date', |
|
36
|
|
|
'time', |
|
37
|
|
|
'microtime', |
|
38
|
|
|
'rand' |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
4 |
|
foreach ($functions as $function) { |
|
42
|
|
|
$this->register($function, function () use ($function) { |
|
43
|
1 |
|
$parameters = func_get_args(); |
|
44
|
|
|
|
|
45
|
1 |
|
return sprintf('%s(%s)', $function, implode(', ', $parameters)); |
|
46
|
|
|
}, function () use ($function) { |
|
47
|
1 |
|
$parameters = array_slice(func_get_args(), 1); |
|
48
|
1 |
|
return call_user_func_array($function, $parameters); |
|
49
|
4 |
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->register('setProperty', function ($property, $value) { |
|
53
|
|
|
return sprintf('($entity->payload[%s] = %s)', $property, $value); |
|
54
|
|
|
}, function ($parameters, $property, $value) { |
|
55
|
|
|
/** @var Entity $entity */ |
|
56
|
|
|
$entity = $parameters['entity']; |
|
57
|
|
|
$entity->payload[$property] = $value; |
|
58
|
4 |
|
}); |
|
59
|
|
|
|
|
60
|
|
|
$this->register('getProperty', function ($property) { |
|
61
|
|
|
return sprintf('$entity->payload[%s]', $property); |
|
62
|
|
|
}, function ($parameters, $property) { |
|
63
|
|
|
/** @var Entity $entity */ |
|
64
|
|
|
$entity = $parameters['entity']; |
|
65
|
|
|
|
|
66
|
|
|
return $entity->payload[$property]; |
|
67
|
4 |
|
}); |
|
68
|
|
|
|
|
69
|
|
|
$this->register('isTiming', function ($timingId) { |
|
70
|
|
|
return sprintf('($eventName == \'%s\' && $event->timingId === %s)', TimingEvent::TIMING_EVENT, $timingId); |
|
71
|
|
|
}, function ($parameters, $eventId) { |
|
72
|
|
|
if ($parameters['eventName'] !== TimingEvent::TIMING_EVENT) { |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $parameters['event']->timingId === $eventId; |
|
77
|
4 |
|
}); |
|
78
|
|
|
|
|
79
|
|
|
$this->register('isEvent', function ($eventId) { |
|
80
|
|
|
return sprintf('($eventName == \'%s\')', $eventId); |
|
81
|
|
|
}, function ($parameters, $eventId) { |
|
82
|
|
|
return $parameters['eventName'] === $eventId; |
|
83
|
4 |
|
}); |
|
84
|
|
|
|
|
85
|
|
|
$this->register('pushViaWebsocket', function () { |
|
86
|
|
|
throw new Exception('pushViaWebsocket() not implemented'); |
|
87
|
|
|
}, function ($parameters) { |
|
88
|
|
|
$this->getDispatcher()->dispatchAsWebsocketEvent($parameters['event']); |
|
89
|
4 |
|
}); |
|
90
|
|
|
|
|
91
|
|
|
$this->register('exec', function () { |
|
92
|
|
|
throw new Exception('exec() not implemented'); |
|
93
|
|
|
}, function ($parameters, $string) { |
|
94
|
|
|
unset($parameters); |
|
95
|
|
|
$inputEvent = new InputControlEvent($string); |
|
96
|
|
|
|
|
97
|
|
|
$this->getDispatcher()->dispatchInBackground($inputEvent); |
|
98
|
4 |
|
}); |
|
99
|
|
|
|
|
100
|
|
|
$this->register('event', function () { |
|
101
|
|
|
throw new Exception('event() not implemented'); |
|
102
|
|
|
}, function (array $parameters, $type, ...$eventArguments) { |
|
103
|
|
|
unset($parameters); |
|
104
|
|
|
$events = (include ROOT.'/cache/events.php'); // TODO extract |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$reflection = new ReflectionClass($events[$type]['class']); |
|
107
|
|
|
/** @var AbstractEvent $event */ |
|
108
|
|
|
$event = $reflection->newInstanceArgs($eventArguments); |
|
109
|
|
|
|
|
110
|
|
|
$this->dispatchEvent($event); |
|
111
|
4 |
|
}); |
|
112
|
|
|
|
|
113
|
|
|
$this->register('log', function () { |
|
114
|
|
|
throw new Exception('log() not implemented'); |
|
115
|
|
|
}, function (array $parameters, $level, $message, $context = null) { |
|
116
|
|
|
unset($parameters); |
|
117
|
|
|
$this->log($level, $message, ['channel' => $context]); |
|
118
|
4 |
|
}); |
|
119
|
|
|
|
|
120
|
|
|
$this->register('increaseCounter', function () { |
|
121
|
|
|
throw new Exception('log() not implemented'); |
|
122
|
|
|
}, function (array $parameters) { |
|
123
|
|
|
/** @var Entity $entity */ |
|
124
|
|
|
$entity = $parameters['entity']; |
|
125
|
|
|
if (empty($entity->payload['counter'])) { |
|
126
|
|
|
$entity->payload['counter'] = 1; |
|
127
|
|
|
} else { |
|
128
|
|
|
$entity->payload['counter']++; |
|
129
|
|
|
} |
|
130
|
4 |
|
}); |
|
131
|
|
|
|
|
132
|
|
|
$this->register('executeExpression', function () { |
|
133
|
|
|
throw new Exception('executeExpression() not implemented'); |
|
134
|
4 |
|
}, function (array $parameters, $expressionId) { |
|
|
|
|
|
|
135
|
|
|
// TODO throw some event |
|
|
|
|
|
|
136
|
4 |
|
}); |
|
137
|
4 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string|Expression $expression |
|
141
|
|
|
* @param array $values |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function evaluate($expression, $values = array()) |
|
145
|
|
|
{ |
|
146
|
1 |
|
if (!$expression) { |
|
147
|
|
|
return ''; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return parent::evaluate($expression, $values); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string[] |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getFunctionNames() |
|
157
|
|
|
{ |
|
158
|
|
|
return array_keys($this->functions); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return string[] |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getParameterNames() |
|
165
|
|
|
{ |
|
166
|
|
|
return [ |
|
167
|
|
|
'event', |
|
168
|
|
|
'eventName', |
|
169
|
|
|
'entity' |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|