-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvent.php
executable file
·169 lines (159 loc) · 5.06 KB
/
Event.php
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
<?php
/*
* This file is part of Numbers Framework.
*
* (c) Volodymyr Volynets <volodymyr.volynets@gmail.com>
*
* This source file is subject to the Apache 2.0 license that is bundled
* with this source code in the file LICENSE.
*/
use Object\Enum\EventTypes;
use Object\Event\EventBase;
class Event
{
/**
* Events
*
* @var array
*/
protected static array $events = [];
/**
* Dispatch
*
* @param string|EventBase $code
* @param string $queue
* @param mixed $data
* @param array $options
* @return array
*/
public static function dispatch(string|EventBase $code, string $queue, mixed $data, array $options = []): array
{
// if we pass event object
if ($code instanceof EventBase) {
$result = $code->validate($data, $options);
if (!$result['success']) {
return $result;
}
$code = $code->code;
}
$options['type'] = $options['type'] ?? EventTypes::RequestEnd->value;
$options['queue'] = $options['queue'] ?? $queue;
// if submodule is not active we simply skip processing and return true
if (!Can::submoduleExists('Numbers.Backend.System.Events')) {
Log::warning('Submitted an event but Numbers.Backend.System.Events is not active.');
return ['success' => true, 'error' => []];
}
$model = Factory::model('\Numbers\Backend\System\Events\Base', true);
return $model->registerAnEvent($code, $data, $options);
}
/**
* Schedule as cron
*
* @param string $code
* @param string $queue
* @param mixed $data
* @param string $cron
* @param array $options
* @return array
*/
public static function cron(string $code, string $queue, mixed $data, string $cron, array $options = []): array
{
$options['type'] = EventTypes::Cron->value;
$options['cron'] = $cron;
$options['queue'] = $queue;
return self::dispatch($code, $data, $options);
}
/**
* Schedule for later (at datetime)
*
* @param string $code
* @param string $queue
* @param mixed $data
* @param Datetime2 $datetime
* @param array $options
* @return array
*/
public static function later(string $code, string $queue, mixed $data, Datetime2 $datetime, array $options = []): array
{
$options['type'] = EventTypes::AtDatetime->value;
$options['datetime'] = $datetime;
$options['queue'] = $queue;
return self::dispatch($code, $data, $options);
}
/**
* Realtime (thought daemon)
*
* @param string $code
* @param string $queue
* @param mixed $data
* @param array $options
* @return array
*/
public static function realtime(string $code, string $queue, mixed $data, array $options = []): array
{
$options['type'] = EventTypes::Realtime->value;
$options['queue'] = $queue;
return self::dispatch($code, $data, $options);
}
/**
* End of request
*
* @param string $code
* @param string $queue
* @param mixed $data
* @param array $options
* @return array
*/
public static function requestEnd(string $code, string $queue, mixed $data, array $options = []): array
{
$options['type'] = EventTypes::RequestEnd->value;
$options['queue'] = $queue;
return self::dispatch($code, $data, $options);
}
/**
* Set event
*
* @param array $options
* @return void
*/
public static function setEvent(array $options = []): void
{
// preset types as array
if (!isset(self::$events[$options['type']])) {
self::$events[$options['type']] = [];
}
// for errors we append to execute later
if ($options['type'] == 'SM::ERRORS') {
self::$events[$options['type']][] = $options;
} else {
self::$events[$options['type']][$options['request_id']] = $options;
}
}
/**
* Process events
*
* @param string $type
* @return array
*/
public static function processEvents(string $type): array
{
// if submodule is not active we simply skip processing and return true
if (!Can::submoduleExists('Numbers.Backend.System.Events')) {
Log::warning('Submitted an event but Numbers.Backend.System.Events is not active.');
return ['success' => true, 'error' => []];
}
// we success if there's no events
if (empty(self::$events[$type])) {
return ['success' => true, 'error' => []];
}
print_r2(self::$events[$type]);
$model = Factory::model('\Numbers\Backend\System\Events\Base', true);
foreach (self::$events[$type] as $k => $v) {
$result = $model->processOneEvent($k);
if (!$result['succcess']) {
Log::warning('Event ' . $k . ' returned error: ' . implode(', ', $result['error']));
}
}
return ['success' => true, 'error' => []];
}
}