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"]
path = docs
url = https://github.com/danog/MadelineProtoDocs
url = git@github.com:danog/MadelineProtoDocs
[submodule "examples/magnaluna"]
path = examples/magnaluna
url = https://github.com/danog/magnaluna
url = git@github.com:danog/magnaluna
[submodule "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
*/
use Amp\Loop;
use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Exception;
@ -95,7 +96,7 @@ class MyEventHandler extends EventHandler
}
$settings = [
'logger' => [
'logger_level' => 5
'logger_level' => 4
],
'serialization' => [
'serialization_interval' => 30,

View File

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

View File

@ -19,6 +19,8 @@
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
use danog\MadelineProto\APIWrapper;
\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
{
private $sent = [-440592694 => true];
public function __construct($API)
public function __construct(?APIWrapper $API)
{
parent::__construct($API);
$this->sent = [];

View File

@ -134,7 +134,6 @@ class API extends InternalDoc
$unserialized->session = $session;
APIWrapper::link($this, $unserialized);
APIWrapper::link($this->wrapper, $this);
var_dump($this, $unserialized);
if (isset($this->API)) {
$this->storage = $this->API->storage ?? $this->storage;
@ -172,11 +171,6 @@ class API extends InternalDoc
public function async(bool $async): void
{
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.

View File

@ -117,6 +117,7 @@ abstract class AbstractAPIFactory extends AsyncConstruct
{
$yielded = Tools::call($this->__call_async($name, $arguments));
$async = !$this->lua && ((\is_array(\end($arguments)) ? \end($arguments) : [])['async'] ?? ($this->async && $name !== 'loop'));
if ($async) {
return $yielded;
}
@ -133,6 +134,20 @@ abstract class AbstractAPIFactory extends AsyncConstruct
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.
*

File diff suppressed because one or more lines are too long

View File

@ -43,7 +43,7 @@ trait Events
*
* @var array<string>
*/
private $event_handler_methods = [];
private $eventHandlerMethods = [];
/**
* Set event handler.
*
@ -63,19 +63,21 @@ trait Events
} elseif ($this->wrapper) {
$this->event_handler_instance->__construct($this->wrapper);
}
$this->event_handler_methods = [];
$this->eventHandlerMethods = [];
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->getTL()->getConstructors()->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'];
if ($constructor['type'] === 'Update' && !isset($this->eventHandlerMethods[$constructor['predicate']])) {
$this->eventHandlerMethods[$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];
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());
@ -97,7 +99,7 @@ trait Events
{
$this->event_handler = null;
$this->event_handler_instance = null;
$this->event_handler_methods = [];
$this->eventHandlerMethods = [];
$this->setNoop();
if ($disableUpdateHandling) {
$this->settings['updates']['handle_updates'] = false;
@ -132,8 +134,8 @@ trait Events
*/
public function eventUpdateHandler(array $update)
{
if (isset($this->event_handler_methods[$update['_']])) {
return $this->event_handler_methods[$update['_']]($update);
if (isset($this->eventHandlerMethods[$update['_']])) {
return $this->eventHandlerMethods[$update['_']]($update);
}
}
}

View File

@ -23,6 +23,13 @@ use function Amp\ByteStream\getOutputBufferStream;
trait Templates
{
/**
* Echo page to console
*
* @param string $message Error message
*
* @return \Generator
*/
private function webEcho(string $message = ''): \Generator
{
$stdout = getOutputBufferStream();
@ -49,21 +56,21 @@ trait Templates
break;
}
}
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>';
private function webEchoTemplate($message, $form): string
/**
* Web template
*
* @var string
*/
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>';
/**
* Format message according to template
*
* @param string $message Message
* @param string $form Form contents
*
* @return string
*/
private function webEchoTemplate(string $message, string $form): string
{
return \sprintf($this->web_template, $form, $message);
}