1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Commands; |
20
|
|
|
|
21
|
|
|
/* |
22
|
|
|
* \class CommandHandler |
23
|
|
|
* \brief Handle all bot commands. |
24
|
|
|
* \details Add commands, check if commands trigger a user update. |
25
|
|
|
*/ |
26
|
|
|
trait CommandHandler |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* \addtogroup Commands |
30
|
|
|
* \brief Bots command and usage. |
31
|
|
|
* @{ |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
/** @internal |
35
|
|
|
* \brief Contains all command used by the bot. */ |
36
|
|
|
private $_commands = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @internal |
40
|
|
|
* \brief Initialize commands to speed up processing. |
41
|
|
|
* \details Get all command that the bot handle, and put them in priority. |
42
|
|
|
*/ |
43
|
3 |
|
protected function initCommands() |
44
|
|
|
{ |
45
|
3 |
|
$commands_temp = $this->_commands; |
46
|
3 |
|
$this->_commands = []; |
47
|
|
|
|
48
|
|
|
// Iterate over each |
49
|
3 |
|
foreach ($commands_temp as $command) { |
50
|
1 |
|
$this->_commands[$command::$type][] = $command; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
foreach ($this->_commands as $index => $array) { |
54
|
|
|
// Sort them by priority |
55
|
1 |
|
uasort($this->_commands[$index], 'PhpBotFramework\Commands\CommandHandler::sortingPrior'); |
56
|
|
|
} |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @internal |
61
|
|
|
* \brief Process updates prioritizing bot's commands over the general methods (e.g. BaseBot::processMessage()) |
62
|
|
|
* @param array $update Update to process. |
63
|
|
|
* @return bool True if this update trigger any command. |
64
|
|
|
*/ |
65
|
3 |
|
protected function processCommands(array $update) : bool |
66
|
|
|
{ |
67
|
3 |
|
print_r($this->_commands); |
68
|
|
|
// For each command active (checked by initCommands()) |
69
|
3 |
|
foreach ($this->_commands as $entity => $commands) { |
70
|
1 |
|
foreach ($commands as $command) { |
71
|
|
|
// If the update type is right and the update triggered a command |
72
|
1 |
|
if (isset($update[$entity]) && $command->checkCommand($update[$entity])) { |
73
|
1 |
|
$entity = new $command::$object_class($update[$entity]); |
74
|
1 |
|
$this->_chat_id = $entity->getChatID(); |
|
|
|
|
75
|
1 |
|
$command->getScript()($this, $entity); |
76
|
|
|
// Return the id as we already processed this update |
77
|
1 |
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Return -1 because this update didn't trigger any command. |
83
|
2 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* \brief Add a command to the bot. |
88
|
|
|
* @param BasicCommand $command Command to add. Must be an object that inherits BasicCommand class. |
89
|
|
|
*/ |
90
|
1 |
|
public function addCommand(BasicCommand $command) |
91
|
|
|
{ |
92
|
1 |
|
$this->_commands[] = $command; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* \brief Add a message command to the bot. |
97
|
|
|
* @param string $command The command that will trigger the function (e.g. start). |
98
|
|
|
* @param callable $script The function that will be triggered by a command. |
99
|
|
|
*/ |
100
|
|
|
public function addMessageCommand(string $command, callable $script) { |
101
|
|
|
$this->_commands[] = new MessageCommand($command, $script); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* \brief Add a callback command to the bot. |
106
|
|
|
* @param string $data The data that will trigger the function. |
107
|
|
|
* @param callable $script The function that will be triggered by the data. |
108
|
|
|
*/ |
109
|
|
|
public function addCallbackCommand(string $data, callable $script) { |
110
|
|
|
$this->_commands[] = new CallbackCommand($data, $script); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @internal |
115
|
|
|
* \brief Sort an array based on <code>prior</code> index value. |
116
|
|
|
* @param array $a First array. |
117
|
|
|
* @param array $b Second array. |
118
|
|
|
* @return int 1 If $a > $b, -1 if $a < $b, 0 otherwise. |
119
|
|
|
*/ |
120
|
|
|
public static function sortingPrior($a, $b) |
121
|
|
|
{ |
122
|
|
|
$prior_a = $a::$priority; |
123
|
|
|
$prior_b = $b::$priority; |
124
|
|
|
if ($prior_a > $prior_b) { |
125
|
|
|
return 1; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($prior_a < $prior_b) { |
129
|
|
|
return -1; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($prior_a == $prior_b) { |
133
|
|
|
return 0; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** @} */ |
138
|
|
|
} |
139
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: