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