. * * @author Daniil Gentili * @copyright 2016-2019 Daniil Gentili * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 * * @link https://docs.madelineproto.xyz MadelineProto documentation */ namespace danog\MadelineProto\Wrappers; /** * Manages logging in and out. */ trait Events { public $event_handler; private $event_handler_instance; private $event_handler_methods = []; public function setEventHandler($event_handler) { if (!\class_exists($event_handler) || !\is_subclass_of($event_handler, '\danog\MadelineProto\EventHandler')) { throw new \danog\MadelineProto\Exception('Wrong event handler was defined'); } $this->event_handler = $event_handler; if (!($this->event_handler_instance instanceof $this->event_handler)) { $class_name = $this->event_handler; $this->event_handler_instance = new $class_name($this->wrapper); } elseif ($this->wrapper) { $this->event_handler_instance->__construct($this->wrapper); } $this->event_handler_methods = []; foreach (\get_class_methods($this->event_handler) as $method) { if ($method === 'onLoop') { $this->loop_callback = [$this->event_handler_instance, 'onLoop']; } elseif ($method === 'onAny') { foreach ($this->constructors->by_id as $id => $constructor) { if ($constructor['type'] === 'Update' && !isset($this->event_handler_methods[$constructor['predicate']])) { $this->event_handler_methods[$constructor['predicate']] = [$this->event_handler_instance, 'onAny']; } } } else { $method_name = \lcfirst(\substr($method, 2)); $this->event_handler_methods[$method_name] = [$this->event_handler_instance, $method]; } } $this->settings['updates']['callback'] = [$this, 'eventUpdateHandler']; $this->settings['updates']['handleUpdates'] = true; $this->settings['updates']['run_callback'] = true; if (!$this->asyncInitPromise) { $this->startUpdateSystem(); } } public function getEventHandler() { return $this->event_handler_instance; } public function eventUpdateHandler($update) { if (isset($this->event_handler_methods[$update['_']])) { return $this->event_handler_methods[$update['_']]($update); } } }