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
|
|
|
/** |
21
|
|
|
* @var UpdateHandler |
22
|
|
|
*/ |
23
|
|
|
protected UpdateHandler $hook; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Generator |
27
|
|
|
*/ |
28
|
|
|
private \Generator $returns; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This property is used to kill the plugin when you yield a Response object. |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected bool $KILL_ON_YIELD = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Execute the plugin. |
39
|
|
|
* |
40
|
|
|
* @param UpdateHandler $receiver |
41
|
|
|
* @param Update $update |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function __execute(UpdateHandler $receiver, Update $update): void |
45
|
|
|
{ |
46
|
|
|
$this->hook = $receiver; |
47
|
|
|
|
48
|
|
|
if (method_exists($this, '__process')) { |
49
|
|
|
$this->__process($update); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (method_exists($this, 'onReceivedUpdate')) { |
53
|
|
|
$return = $this->onReceivedUpdate($update); |
54
|
|
|
$this->__checkExit($return); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$type = $this->__identify($update); |
58
|
|
|
if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
59
|
|
|
$catchMethod = 'get' . ucfirst($type); |
60
|
|
|
$return = $this->$method($update->getUpdateId(), $update->$catchMethod()); |
61
|
|
|
$this->__checkExit($return); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Check for the exit of the plugin. |
67
|
|
|
* |
68
|
|
|
* @param \Generator $return |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
private function __checkExit(\Generator $return): void |
72
|
|
|
{ |
73
|
|
|
if ($return->valid()) { |
74
|
|
|
if ($return->current() !== null && $this->KILL_ON_YIELD === true) { |
75
|
|
|
if ($return->current() instanceof Response) { |
76
|
|
|
$this->stop(); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($return->valid()) { |
82
|
|
|
$return->next(); |
83
|
|
|
$this->__checkExit($return); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Identify the update type. e.g. Message, EditedMessage, etc. |
89
|
|
|
* |
90
|
|
|
* @param Update $update |
91
|
|
|
* @return string|null |
92
|
|
|
*/ |
93
|
|
|
private function __identify(Update $update): ?string |
94
|
|
|
{ |
95
|
|
|
$type = $update->getUpdateType(); |
96
|
|
|
|
97
|
|
|
if ($type === null) { |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return str_replace('_', '', ucwords($type, '_')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |