Final fixes

This commit is contained in:
Daniil Gentili 2020-02-25 19:10:49 +01:00
parent b5267beaa2
commit e03e707815
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
10 changed files with 73 additions and 44 deletions

6
.gitmodules vendored
View File

@ -1,9 +1,9 @@
[submodule "docs"] [submodule "docs"]
path = docs path = docs
url = https://github.com/danog/MadelineProtoDocs url = git@github.com:danog/MadelineProtoDocs
[submodule "examples/magnaluna"] [submodule "examples/magnaluna"]
path = examples/magnaluna path = examples/magnaluna
url = https://github.com/danog/magnaluna url = git@github.com:danog/magnaluna
[submodule "examples/pipesbot"] [submodule "examples/pipesbot"]
path = examples/pipesbot path = examples/pipesbot
url = https://github.com/danog/pipesbot url = git@github.com:danog/pipesbot

2
docs

@ -1 +1 @@
Subproject commit de466b0b0962007f79b4405bdd21b3a0dd08f9ee Subproject commit e8cd2a7bb534007357d2c07e886ea37bb223e91e

View File

@ -19,6 +19,7 @@
* @link https://docs.madelineproto.xyz MadelineProto documentation * @link https://docs.madelineproto.xyz MadelineProto documentation
*/ */
use Amp\Loop;
use danog\MadelineProto\API; use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Exception; use danog\MadelineProto\Exception;
@ -95,7 +96,7 @@ class MyEventHandler extends EventHandler
} }
$settings = [ $settings = [
'logger' => [ 'logger' => [
'logger_level' => 5 'logger_level' => 4
], ],
'serialization' => [ 'serialization' => [
'serialization_interval' => 30, 'serialization_interval' => 30,

View File

@ -21,6 +21,7 @@
use Amp\Http\Server\HttpServer; use Amp\Http\Server\HttpServer;
use danog\MadelineProto\API; use danog\MadelineProto\API;
use danog\MadelineProto\APIWrapper;
use danog\MadelineProto\MTProtoTools\Files; use danog\MadelineProto\MTProtoTools\Files;
use danog\MadelineProto\RPCErrorException; use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\Tools; use danog\MadelineProto\Tools;
@ -80,7 +81,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler
* *
* @param ?API $API API * @param ?API $API API
*/ */
public function __construct(?API $API) public function __construct(?APIWrapper $API)
{ {
$this->UPLOAD = \class_exists(HttpServer::class); $this->UPLOAD = \class_exists(HttpServer::class);
parent::__construct($API); parent::__construct($API);

View File

@ -19,6 +19,8 @@
* @link https://docs.madelineproto.xyz MadelineProto documentation * @link https://docs.madelineproto.xyz MadelineProto documentation
*/ */
use danog\MadelineProto\APIWrapper;
\set_include_path(\get_include_path().':'.\realpath(\dirname(__FILE__).'/MadelineProto/')); \set_include_path(\get_include_path().':'.\realpath(\dirname(__FILE__).'/MadelineProto/'));
/* /*
@ -36,7 +38,7 @@ if (\file_exists(__DIR__.'/../vendor/autoload.php')) {
class SecretHandler extends \danog\MadelineProto\EventHandler class SecretHandler extends \danog\MadelineProto\EventHandler
{ {
private $sent = [-440592694 => true]; private $sent = [-440592694 => true];
public function __construct($API) public function __construct(?APIWrapper $API)
{ {
parent::__construct($API); parent::__construct($API);
$this->sent = []; $this->sent = [];

View File

@ -134,7 +134,6 @@ class API extends InternalDoc
$unserialized->session = $session; $unserialized->session = $session;
APIWrapper::link($this, $unserialized); APIWrapper::link($this, $unserialized);
APIWrapper::link($this->wrapper, $this); APIWrapper::link($this->wrapper, $this);
var_dump($this, $unserialized);
if (isset($this->API)) { if (isset($this->API)) {
$this->storage = $this->API->storage ?? $this->storage; $this->storage = $this->API->storage ?? $this->storage;
@ -172,11 +171,6 @@ class API extends InternalDoc
public function async(bool $async): void public function async(bool $async): void
{ {
parent::async($async); parent::async($async);
if ($this->API) {
if ($this->API->event_handler && \class_exists($this->API->event_handler) && \is_subclass_of($this->API->event_handler, EventHandler::class)) {
$this->API->setEventHandler($this->API->event_handler);
}
}
} }
/** /**
* Destruct function. * Destruct function.

View File

@ -117,6 +117,7 @@ abstract class AbstractAPIFactory extends AsyncConstruct
{ {
$yielded = Tools::call($this->__call_async($name, $arguments)); $yielded = Tools::call($this->__call_async($name, $arguments));
$async = !$this->lua && ((\is_array(\end($arguments)) ? \end($arguments) : [])['async'] ?? ($this->async && $name !== 'loop')); $async = !$this->lua && ((\is_array(\end($arguments)) ? \end($arguments) : [])['async'] ?? ($this->async && $name !== 'loop'));
if ($async) { if ($async) {
return $yielded; return $yielded;
} }
@ -133,6 +134,20 @@ abstract class AbstractAPIFactory extends AsyncConstruct
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()]; return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
} }
} }
/**
* Info to dump
*
* @return array
*/
public function __debugInfo(): array
{
$keys = APIWrapper::__sleep();
$res = [];
foreach ($keys as $key) {
$res[$key] = Tools::getVar($this, $key);
}
return $res;
}
/** /**
* Call async wrapper function. * Call async wrapper function.
* *

File diff suppressed because one or more lines are too long

View File

@ -43,7 +43,7 @@ trait Events
* *
* @var array<string> * @var array<string>
*/ */
private $event_handler_methods = []; private $eventHandlerMethods = [];
/** /**
* Set event handler. * Set event handler.
* *
@ -63,19 +63,21 @@ trait Events
} elseif ($this->wrapper) { } elseif ($this->wrapper) {
$this->event_handler_instance->__construct($this->wrapper); $this->event_handler_instance->__construct($this->wrapper);
} }
$this->event_handler_methods = []; $this->eventHandlerMethods = [];
foreach (\get_class_methods($this->event_handler) as $method) { foreach (\get_class_methods($this->event_handler) as $method) {
if ($method === 'onLoop') { if ($method === 'onLoop') {
$this->loop_callback = [$this->event_handler_instance, 'onLoop']; $this->loop_callback = [$this->event_handler_instance, 'onLoop'];
} elseif ($method === 'onAny') { } elseif ($method === 'onAny') {
foreach ($this->getTL()->getConstructors()->by_id as $id => $constructor) { foreach ($this->getTL()->getConstructors()->by_id as $id => $constructor) {
if ($constructor['type'] === 'Update' && !isset($this->event_handler_methods[$constructor['predicate']])) { if ($constructor['type'] === 'Update' && !isset($this->eventHandlerMethods[$constructor['predicate']])) {
$this->event_handler_methods[$constructor['predicate']] = [$this->event_handler_instance, 'onAny']; $this->eventHandlerMethods[$constructor['predicate']] = [$this->event_handler_instance, 'onAny'];
} }
} }
} else { } else {
$method_name = \lcfirst(\substr($method, 2)); $method_name = \lcfirst(\substr($method, 2));
$this->event_handler_methods[$method_name] = [$this->event_handler_instance, $method]; if (($constructor = $this->getTL()->getConstructors()->findByPredicate($method_name)) && $constructor['type'] === 'Update') {
$this->eventHandlerMethods[$method_name] = [$this->event_handler_instance, $method];
}
} }
} }
$this->setReportPeers($this->event_handler_instance->getReportPeers()); $this->setReportPeers($this->event_handler_instance->getReportPeers());
@ -97,7 +99,7 @@ trait Events
{ {
$this->event_handler = null; $this->event_handler = null;
$this->event_handler_instance = null; $this->event_handler_instance = null;
$this->event_handler_methods = []; $this->eventHandlerMethods = [];
$this->setNoop(); $this->setNoop();
if ($disableUpdateHandling) { if ($disableUpdateHandling) {
$this->settings['updates']['handle_updates'] = false; $this->settings['updates']['handle_updates'] = false;
@ -132,8 +134,8 @@ trait Events
*/ */
public function eventUpdateHandler(array $update) public function eventUpdateHandler(array $update)
{ {
if (isset($this->event_handler_methods[$update['_']])) { if (isset($this->eventHandlerMethods[$update['_']])) {
return $this->event_handler_methods[$update['_']]($update); return $this->eventHandlerMethods[$update['_']]($update);
} }
} }
} }

View File

@ -23,6 +23,13 @@ use function Amp\ByteStream\getOutputBufferStream;
trait Templates trait Templates
{ {
/**
* Echo page to console
*
* @param string $message Error message
*
* @return \Generator
*/
private function webEcho(string $message = ''): \Generator private function webEcho(string $message = ''): \Generator
{ {
$stdout = getOutputBufferStream(); $stdout = getOutputBufferStream();
@ -49,21 +56,21 @@ trait Templates
break; break;
} }
} }
private $web_template = '<!DOCTYPE html> /**
<html> * Web template
<head> *
<title>MadelineProto</title> * @var string
</head> */
<body> private $web_template = '<!DOCTYPE html><html><head><title>MadelineProto</title></head><body><h1>MadelineProto</h1><form method="POST">%s<button type="submit"/>Go</button></form><p>%s</p></body></html>';
<h1>MadelineProto</h1> /**
<form method="POST"> * Format message according to template
%s *
<button type="submit"/>Go</button> * @param string $message Message
</form> * @param string $form Form contents
<p>%s</p> *
</body> * @return string
</html>'; */
private function webEchoTemplate($message, $form): string private function webEchoTemplate(string $message, string $form): string
{ {
return \sprintf($this->web_template, $form, $message); return \sprintf($this->web_template, $form, $message);
} }