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.php'; |
||
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 $command_on_first = true; |
||
54 | |||
55 | protected $commandNamespace = null; |
||
56 | |||
57 | protected $entityEventNamespace = null; |
||
58 | |||
59 | protected $botDir = null; |
||
60 | |||
61 | /** |
||
62 | * Constructs configuration object with either bot name or bot config file passed. |
||
63 | * |
||
64 | * @param string $botName The name of the bot to execute |
||
65 | * @param string $botConfig Path to bot's configuration file |
||
66 | */ |
||
67 | public function __construct(string $botName = '', $botConfig = '') |
||
68 | { |
||
69 | $this->initBotConfiguration($botName, $botConfig); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Initialises bot configuration via bot name or configuration file. |
||
74 | * |
||
75 | * @param string $botName The name of the bot to execute |
||
76 | * @param string $botConfig Path to bot's configuration file |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function initBotConfiguration(string $botName = '', $botConfig = '') |
||
81 | { |
||
82 | $this->botName = $botName; |
||
83 | |||
84 | try { |
||
85 | if (!empty($botName)) { |
||
86 | $this->botDir = $this->getBotDir($botName); |
||
87 | $botConfig = $this->botDir . static::CONFIG_FILENAME; |
||
88 | |||
89 | $this->setNamespaces($botName); |
||
90 | } |
||
91 | |||
92 | if (empty($botConfig)) { |
||
93 | Output::log(new Fatal("Path to configuration file was not sent!")); |
||
94 | } |
||
95 | |||
96 | $this->loadConfigFile($botConfig); |
||
97 | } catch (Fatal $e) { |
||
98 | Output::log($e); |
||
99 | } |
||
100 | |||
101 | return true; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Returns bot directory built with default path pattern. |
||
106 | * |
||
107 | * @param string $botName The name of the bot to execute |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | protected function getBotDir(string $botName) : string |
||
112 | { |
||
113 | $dir = sprintf( |
||
114 | static::BOT_DIR_PATTERN, |
||
115 | __DIR__, |
||
116 | $botName |
||
117 | ); |
||
118 | |||
119 | if (!file_exists($dir)) { |
||
120 | Output::log(new Fatal('Bot does not exist!')); |
||
121 | } |
||
122 | |||
123 | return realpath($dir) . "/"; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Sets namespace for command and event entities classes to be able to load them via autoloader in the future. |
||
128 | * |
||
129 | * @param string $botName The name of the bot to execute |
||
130 | */ |
||
131 | protected function setNamespaces(string $botName) |
||
132 | { |
||
133 | $this->commandNamespace = sprintf(static::COMMAND_NAMESPACE_PATTERN, $botName); |
||
134 | $this->entityEventNamespace = sprintf(static::EVENT_NAMESPACE_PATTERN, $botName); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Loads configuration file in JSON format. |
||
139 | * |
||
140 | * @param string $configFile Path to configuration file |
||
141 | */ |
||
142 | protected function loadConfigFile($configFile) |
||
143 | { |
||
144 | if (!is_file($configFile) || !is_readable($configFile)) { |
||
145 | Output::log(new Fatal('File "' . $configFile . '" does not exists or not readable!')); |
||
146 | } |
||
147 | |||
148 | $config = require_once($configFile); |
||
149 | |||
150 | $this->setProperties($config); |
||
151 | } |
||
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 whether command should be searched on first position in text. |
||
205 | * |
||
206 | * @return boolean |
||
207 | */ |
||
208 | public function getCommandOnFirst() |
||
212 | |||
213 | /** |
||
214 | * Returns command's name space if bots are placed in default Bot directory |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | public function getCommandNamespace() |
||
222 | |||
223 | /** |
||
224 | * Returns entity's name space if bots are placed in default Bot directory |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getEntityEventNamespace() |
||
232 | |||
233 | /** |
||
234 | * Returns base url from the files to download from Telegram's storage servers, if the value |
||
235 | * was not set in config - default value will be used |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getFileUrl() |
||
243 | |||
244 | /** |
||
245 | * Returns path to log file for Errors, if not set - all errors will be echoed. |
||
246 | * |
||
247 | * @return null|string |
||
248 | */ |
||
249 | public function getLogFile() |
||
253 | |||
254 | /** |
||
255 | * Returns an array with defined events map, if not set default namespaces and mapping will be used. |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | public function getEvents() |
||
263 | |||
264 | /** |
||
265 | * Returns base url for the files to download from Telegram's storage servers, token will be also added. |
||
266 | * If the value of file url was not set in config - default value will be used |
||
267 | * |
||
268 | * @return null|string |
||
269 | */ |
||
270 | public function getFileBasePath() |
||
278 | } |
||
279 |