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