1 | <?php |
||
20 | class Language extends ExpressionLanguage |
||
21 | { |
||
22 | |||
23 | use EventDispatcherTrait; |
||
24 | use LoggerTrait; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 9 | public function __construct(ParserCacheInterface $cache = null, array $providers = []) |
|
30 | { |
||
31 | 9 | parent::__construct($cache, $providers); |
|
32 | |||
33 | $functions = [ |
||
34 | 9 | 'sprintf', |
|
35 | 9 | 'date', |
|
36 | 9 | 'time', |
|
37 | 9 | 'microtime', |
|
38 | 'rand' |
||
39 | 9 | ]; |
|
40 | |||
41 | 9 | 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 | 9 | }); |
|
50 | 9 | } |
|
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 | 9 | }); |
|
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 | 9 | }); |
|
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 | 9 | }); |
|
78 | |||
79 | $this->register('isEvent', function ($eventId) { |
||
80 | return sprintf('($eventName == \'%s\')', $eventId); |
||
81 | }, function ($parameters, $eventId) { |
||
82 | return $parameters['eventName'] === $eventId; |
||
83 | 9 | }); |
|
84 | |||
85 | $this->register('pushViaWebsocket', function () { |
||
86 | throw new Exception('pushViaWebsocket() not implemented'); |
||
87 | }, function ($parameters) { |
||
88 | $this->getDispatcher()->dispatchAsWebsocketEvent($parameters['event']); |
||
89 | 9 | }); |
|
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 | 9 | }); |
|
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 | 9 | }); |
|
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 | 9 | }); |
|
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 | 9 | }); |
|
131 | |||
132 | $this->register('executeExpression', function () { |
||
133 | throw new Exception('executeExpression() not implemented'); |
||
134 | 9 | }, function (array $parameters, $expressionId) { |
|
135 | // TODO throw some event |
||
136 | 9 | }); |
|
137 | 9 | } |
|
138 | |||
139 | /** |
||
140 | * @param string|Expression $expression |
||
141 | * @param array $values |
||
142 | * @return string |
||
143 | */ |
||
144 | 1 | public function evaluate($expression, $values = array()) |
|
152 | |||
153 | /** |
||
154 | * @return string[] |
||
155 | */ |
||
156 | public function getFunctionNames() |
||
160 | |||
161 | /** |
||
162 | * @return string[] |
||
163 | */ |
||
164 | public function getParameterNames() |
||
172 | } |
||
173 |