1 | <?php |
||
16 | class Config |
||
17 | { |
||
18 | const DEFAULT_LIMIT = 1; |
||
19 | |||
20 | const DEFAULT_OFFSET = -1; |
||
21 | |||
22 | const REQUEST_TIMEOUT = 6; |
||
23 | |||
24 | const BOT_PREFIX = 'bot'; |
||
25 | |||
26 | const COMMAND_NAMESPACE_PATTERN = 'Teebot\\Bot\\%s\\Command'; |
||
27 | |||
28 | const EVENT_NAMESPACE_PATTERN = 'Teebot\\Bot\\%s\\EntityEvent'; |
||
29 | |||
30 | const CONFIG_FILENAME = 'config.json'; |
||
31 | |||
32 | const BOT_DIR_PATTERN = '%s/../Bot/%s'; |
||
33 | |||
34 | protected $botName = null; |
||
35 | |||
36 | protected $token; |
||
37 | |||
38 | protected $url = 'https://api.telegram.org'; |
||
39 | |||
40 | protected $timeout = 1; |
||
41 | |||
42 | protected $method; |
||
43 | |||
44 | protected $file_url = 'https://api.telegram.org/file/bot'; |
||
45 | |||
46 | protected $log_file = null; |
||
47 | |||
48 | protected $events; |
||
49 | |||
50 | protected $commandNamespace = null; |
||
51 | |||
52 | protected $entityEventNamespace = null; |
||
53 | |||
54 | protected $botDir = null; |
||
55 | |||
56 | /** |
||
57 | * Constructs configuration object with either bot name or bot config file passed. |
||
58 | * |
||
59 | * @param string $botName The name of the bot to execute |
||
60 | * @param string $botConfig Path to bot's configuration file |
||
61 | */ |
||
62 | public function __construct(string $botName = '', $botConfig = '') |
||
66 | |||
67 | /** |
||
68 | * Initialises bot configuration via bot name or configuration file. |
||
69 | * |
||
70 | * @param string $botName The name of the bot to execute |
||
71 | * @param string $botConfig Path to bot's configuration file |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | public function initBotConfiguration(string $botName = '', $botConfig = '') |
||
98 | |||
99 | /** |
||
100 | * Returns bot directory built with default path pattern. |
||
101 | * |
||
102 | * @param string $botName The name of the bot to execute |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | protected function getBotDir(string $botName) : string |
||
120 | |||
121 | /** |
||
122 | * Sets namespace for command and event entities classes to be able to load them via autoloader in the future. |
||
123 | * |
||
124 | * @param string $botName The name of the bot to execute |
||
125 | */ |
||
126 | protected function setNamespaces(string $botName) |
||
131 | |||
132 | /** |
||
133 | * Loads configuration file in JSON format. |
||
134 | * |
||
135 | * @param string $configFile Path to configuration file |
||
136 | */ |
||
137 | protected function loadConfig($configFile) |
||
152 | |||
153 | /** |
||
154 | * Returns bot token string, if the value was not set in config - default value will be used |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getToken() |
||
162 | |||
163 | /** |
||
164 | * Returns Bot-API request url |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getUrl() |
||
172 | |||
173 | /** |
||
174 | * Returns request timeout in seconds, if the value was not set in config - default value will be used |
||
175 | * |
||
176 | * @return int |
||
177 | */ |
||
178 | public function getTimeout() |
||
182 | |||
183 | /** |
||
184 | * Returns name of the bot if it was set |
||
185 | * |
||
186 | * @return null|string |
||
187 | */ |
||
188 | public function getBotName() |
||
192 | |||
193 | /** |
||
194 | * Returns request method name |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getMethod() |
||
202 | |||
203 | /** |
||
204 | * Returns command's name space if bots are placed in default Bot directory |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getCommandNamespace() |
||
212 | |||
213 | /** |
||
214 | * Returns entity's name space if bots are placed in default Bot directory |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public function getEntityEventNamespace() |
||
222 | |||
223 | /** |
||
224 | * Returns base url from the files to download from Telegram's storage servers, if the value |
||
225 | * was not set in config - default value will be used |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getFileUrl() |
||
233 | |||
234 | /** |
||
235 | * Returns path to log file for Errors, if not set - all errors will be echoed. |
||
236 | * |
||
237 | * @return null|string |
||
238 | */ |
||
239 | public function getLogFile() |
||
243 | |||
244 | /** |
||
245 | * Returns an array with defined events map, if not set default namespaces and mapping will be used. |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | public function getEvents() |
||
253 | |||
254 | /** |
||
255 | * Returns base url for the files to download from Telegram's storage servers, token will be also added. |
||
256 | * If the value of file url was not set in config - default value will be used |
||
257 | * |
||
258 | * @return null|string |
||
259 | */ |
||
260 | public function getFileBasePath() |
||
268 | } |
||
269 |