1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TelegramBot\Traits; |
5
|
|
|
|
6
|
|
|
use TelegramBot\Entities\Response; |
7
|
|
|
use TelegramBot\Entities\Update; |
8
|
|
|
use TelegramBot\Request; |
9
|
|
|
use TelegramBot\UpdateHandler; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* PluginTrait class |
13
|
|
|
* |
14
|
|
|
* @link https://github.com/telegram-bot-php/core |
15
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
16
|
|
|
* @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License) |
17
|
|
|
*/ |
18
|
|
|
trait PluginTrait |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var UpdateHandler |
23
|
|
|
*/ |
24
|
|
|
protected UpdateHandler $hook; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This property is used to kill the plugin when you yield a Response object. |
28
|
|
|
* |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected bool $KILL_ON_YIELD = true; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Generator |
35
|
|
|
*/ |
36
|
|
|
private \Generator $returns; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the plugin. |
40
|
|
|
* |
41
|
|
|
* @param UpdateHandler $receiver |
42
|
|
|
* @param Update $update |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function __execute(UpdateHandler $receiver, Update $update): void |
46
|
|
|
{ |
47
|
|
|
$this->hook = $receiver; |
48
|
|
|
|
49
|
|
|
if (method_exists($this, '__process')) { |
50
|
|
|
$this->__process($update); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (method_exists($this, 'onReceivedUpdate')) { |
54
|
|
|
$return = $this->onUpdate($update); |
|
|
|
|
55
|
|
|
$this->__checkExit($return); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$type = $this->__identify($update); |
59
|
|
|
if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
60
|
|
|
$this->__checkExit($this->__callEvent($method, $update)); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check for the exit of the plugin. |
66
|
|
|
* |
67
|
|
|
* @param \Generator $return |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
private function __checkExit(\Generator $return): void |
71
|
|
|
{ |
72
|
|
|
if ($return->valid()) { |
73
|
|
|
if ($return->current() !== null && $this->KILL_ON_YIELD === true) { |
74
|
|
|
if ($return->current() instanceof Response) { |
75
|
|
|
$this->stop(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($return->valid()) { |
81
|
|
|
$return->next(); |
82
|
|
|
$this->__checkExit($return); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Identify the update type. e.g. Message, EditedMessage, etc. |
88
|
|
|
* |
89
|
|
|
* @param Update $update |
90
|
|
|
* @return string|null |
91
|
|
|
*/ |
92
|
|
|
private function __identify(Update $update): ?string |
93
|
|
|
{ |
94
|
|
|
$type = $update->getUpdateType(); |
95
|
|
|
|
96
|
|
|
if ($type === null) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return str_replace('_', '', ucwords($type, '_')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Pass data to the method. |
105
|
|
|
* |
106
|
|
|
* @param string $method The method name. |
107
|
|
|
* @param Update $update The update object. |
108
|
|
|
* @return \Generator |
109
|
|
|
*/ |
110
|
|
|
private function __callEvent(string $method, Update $update): \Generator |
111
|
|
|
{ |
112
|
|
|
$upperName = 'get' . ucfirst(substr($method, 2)); |
113
|
|
|
return match ($method) { |
114
|
|
|
'onWebAppData' => $this->onWebAppData($update->getWebAppData()), |
|
|
|
|
115
|
|
|
default => $this->$method($update->getUpdateId(), $update->$upperName()), |
116
|
|
|
}; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |