1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 05:58:58 +01:00

Add IPC support to buttons, start writing PHPDOC documentor

This commit is contained in:
Daniil Gentili 2020-10-07 19:31:52 +02:00
parent 0d1ef0aebe
commit 17ed5c0991
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
51 changed files with 367 additions and 20 deletions

View File

@ -44,12 +44,12 @@
"amphp/websocket-client": "^1.0"
},
"require-dev": {
"phpdocumentor/reflection-docblock": "^5.2",
"vlucas/phpdotenv": "^3",
"phpdocumentor/reflection-docblock": "^4.3",
"ennexa/amp-update-cache": "dev-master",
"phpunit/phpunit": "^8",
"amphp/php-cs-fixer-config": "dev-master",
"haydenpierce/class-finder": "^0.4",
"haydenpierce/class-finder": "^0.4.2",
"amphp/http-server": "dev-master",
"amphp/http": "^1.6",
"ext-ctype": "*",

View File

@ -179,7 +179,7 @@ class API extends InternalDoc
/**
* Connect to MadelineProto.
*
* @param Settings|SettingsEmpty $settings Settings
* @param SettingsAbstract $settings Settings
* @param bool $forceFull Whether to force full initialization
*
* @return \Generator

View File

@ -210,4 +210,14 @@ final class APIWrapper
return true;
})());
}
/**
* Get session path.
*
* @return SessionPaths
*/
public function getSession(): SessionPaths
{
return $this->session;
}
}

View File

@ -9,7 +9,10 @@ use danog\MadelineProto\Settings\Database\Postgres;
use danog\MadelineProto\Settings\Database\Redis;
use danog\MadelineProto\Settings\DatabaseAbstract;
class DbPropertiesFactory
/**
* This factory class initializes the correct database backend for MadelineProto.
*/
abstract class DbPropertiesFactory
{
/**
* Indicates a simple K-V array stored in a database backend.
@ -24,6 +27,8 @@ class DbPropertiesFactory
*
* @return Promise<DbType>
*
* @internal
*
* @uses \danog\MadelineProto\Db\MemoryArray
* @uses \danog\MadelineProto\Db\MysqlArray
* @uses \danog\MadelineProto\Db\PostgresArray

View File

@ -4,6 +4,15 @@ namespace danog\MadelineProto\Db;
use danog\MadelineProto\MTProto;
/**
* Include this trait and call DbPropertiesTrait::initDb to use MadelineProto's database backend for properties.
*
* You will have to define a `$dbProperties` static array property, with a list of properties you want to store to a database.
*
* @see DbPropertiesFactory For a list of allowed property types
*
* @property array<string, DbPropertiesFactory::TYPE_*> $dbProperties
*/
trait DbPropertiesTrait
{
/**

View File

@ -9,6 +9,9 @@ use danog\MadelineProto\Settings\Database\Mysql as DatabaseMysql;
use function Amp\Mysql\Pool;
/**
* MySQL driver wrapper.
*/
class Mysql
{
/** @var Pool[] */

View File

@ -9,6 +9,9 @@ use danog\MadelineProto\Settings\Database\Postgres as DatabasePostgres;
use function Amp\Postgres\Pool;
/**
* Postgres driver wrapper.
*/
class Postgres
{
/** @var Pool[] */

View File

@ -7,6 +7,9 @@ use Amp\Redis\Redis as RedisRedis;
use Amp\Redis\RemoteExecutorFactory;
use danog\MadelineProto\Settings\Database\Redis as DatabaseRedis;
/**
* Redis driver wrapper.
*/
class Redis
{
/** @var RedisRedis[] */

View File

@ -10,6 +10,9 @@ use danog\MadelineProto\Settings\Database\Memory;
use function Amp\call;
/**
* Memory database backend.
*/
class MemoryArray extends \ArrayIterator implements DbArray
{
protected function __construct($array = [], $flags = 0)

View File

@ -13,6 +13,9 @@ use danog\MadelineProto\Settings\Database\Mysql as DatabaseMysql;
use function Amp\call;
/**
* MySQL database backend.
*/
class MysqlArray extends SqlArray
{
protected string $table;

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Db\NullCache;
use danog\MadelineProto\Db\MysqlArray as DbMysqlArray;
/**
* MySQL database backend, no caching.
*/
class MysqlArray extends DbMysqlArray
{
use NullCacheTrait;

View File

@ -2,6 +2,11 @@
namespace danog\MadelineProto\Db\NullCache;
/**
* Trait that disables database caching.
*
* @internal
*/
trait NullCacheTrait
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Db\NullCache;
use danog\MadelineProto\Db\PostgresArray as DbPostgresArray;
/**
* Postgres database backend, no caching.
*/
class PostgresArray extends DbPostgresArray
{
use NullCacheTrait;

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Db\NullCache;
use danog\MadelineProto\Db\RedisArray as DbRedisArray;
/**
* Redis database backend, no caching.
*/
class RedisArray extends DbRedisArray
{
use NullCacheTrait;

View File

@ -13,6 +13,9 @@ use danog\MadelineProto\Settings\Database\Postgres as DatabasePostgres;
use function Amp\call;
/**
* Postgres database backend.
*/
class PostgresArray extends SqlArray
{
protected string $table;

View File

@ -13,6 +13,9 @@ use Generator;
use function Amp\call;
/**
* Redis database backend.
*/
class RedisArray extends SqlArray
{
protected DatabaseRedis $dbSettings;

View File

@ -7,6 +7,9 @@ use danog\MadelineProto\Settings\Database\DatabaseAbstract;
use function Amp\call;
/**
* Generic SQL database backend.
*/
abstract class SqlArray extends DriverArray
{
protected string $table;

View File

@ -19,6 +19,9 @@
namespace danog\MadelineProto;
/**
* Basic exception.
*/
class Exception extends \Exception
{
use TL\PrettyException;

View File

@ -6061,13 +6061,6 @@ class InternalDoc extends APIFactory
{
$this->__call(__FUNCTION__, [$hook_url, $pem_path]);
}
/**
* Set API wrapper needed for triggering serialization functions.
*/
public function setWrapper(\danog\MadelineProto\APIWrapper $wrapper)
{
$this->__call(__FUNCTION__, [$wrapper]);
}
/**
* Setup logger.
*

View File

@ -19,13 +19,15 @@
namespace danog\MadelineProto;
/**
* Lua interface.
*/
class Lua
{
use \danog\Serializable;
public API $MadelineProto;
protected $Lua;
protected $script;
public function __magic_construct($script, $MadelineProto)
protected string $script;
public function __construct(string $script, API $MadelineProto)
{
if (!\file_exists($script)) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['script_not_exist']);

View File

@ -601,11 +601,22 @@ class MTProto extends AsyncConstruct implements TLCallback
}
/**
* Set API wrapper needed for triggering serialization functions.
*
* @internal
*/
public function setWrapper(APIWrapper $wrapper): void
{
$this->wrapper = $wrapper;
}
/**
* Get API wrapper.
*
* @internal
*/
public function getWrapper(): APIWrapper
{
return $this->wrapper;
}
/**
* Sleep function.
*

View File

@ -19,6 +19,9 @@
namespace danog\MadelineProto;
/**
* Internal error indicating a problem with Telegram's servers.
*/
class PTSException extends \Exception
{
use TL\PrettyException;

View File

@ -0,0 +1,177 @@
<?php
/**
* PhpDocBuilder module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
use danog\MadelineProto\Async\AsyncConstruct;
use danog\MadelineProto\Db\DbPropertiesTrait;
use danog\MadelineProto\Files\Server;
use danog\MadelineProto\MTProtoTools\Crypt;
use danog\MadelineProto\MTProtoTools\GarbageCollector;
use danog\MadelineProto\MTProtoTools\MinDatabase;
use danog\MadelineProto\MTProtoTools\PasswordCalculator;
use danog\MadelineProto\MTProtoTools\ReferenceDatabase;
use danog\MadelineProto\MTProtoTools\UpdatesState;
use danog\MadelineProto\TL\TL;
use danog\MadelineProto\TL\TLConstructors;
use danog\MadelineProto\TL\TLMethods;
use danog\MadelineProto\TON\ADNLConnection;
use danog\MadelineProto\TON\APIFactory as TAPIFactory;
use danog\MadelineProto\TON\InternalDoc as TInternalDoc;
use danog\MadelineProto\TON\Lite;
use HaydenPierce\ClassFinder\ClassFinder;
use phpDocumentor\Reflection\DocBlock\Tags\Author;
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Property;
use phpDocumentor\Reflection\DocBlock\Tags\See;
use phpDocumentor\Reflection\DocBlockFactory;
use ReflectionClass;
use ReflectionClassConstant;
class PhpDocBuilder
{
private DocBlockFactory $factory;
public function __construct()
{
$this->factory = DocBlockFactory::createInstance();
}
public function run()
{
$classes = ClassFinder::getClassesInNamespace('danog\\MadelineProto', ClassFinder::RECURSIVE_MODE);
foreach ($classes as $class) {
if (\in_array($class, [
AnnotationsBuilder::class,
APIFactory::class,
APIWrapper::class,
AbstractAPIFactory::class,
Bug74586Exception::class,
Connection::class,
ContextConnector::class,
DataCenter::class,
DataCenterConnection::class,
DoHConnector::class,
DocsBuilder::class,
InternalDoc::class,
Lang::class,
LightState::class,
Magic::class,
PhpDocBuilder::class,
RSA::class,
Serialization::class,
SessionPaths::class,
SettingsEmpty::class,
SettingsAbstract::class,
Snitch::class,
AsyncConstruct::class,
Server::class, // Remove when done
VoIP::class,
Crypt::class,
NothingInTheSocketException::class,
GarbageCollector::class,
MinDatabase::class,
PasswordCalculator::class,
ReferenceDatabase::class,
UpdatesState::class,
TL::class,
TLConstructors::class,
TLMethods::class,
ADNLConnection::class,
TAPIFactory::class,
TInternalDoc::class,
Lite::class,
]) || str_starts_with($class, 'danog\\MadelineProto\\Ipc')
|| str_starts_with($class, 'danog\\MadelineProto\\Loop\\Update')
|| str_starts_with($class, 'danog\\MadelineProto\\Loop\\Connection')
|| str_starts_with($class, 'danog\\MadelineProto\\MTProto\\')
|| str_starts_with($class, 'danog\\MadelineProto\\MTProtoSession\\')
|| str_starts_with($class, 'danog\\MadelineProto\\Db\\NullCache')) {
continue;
}
$class = new ReflectionClass($class);
if ($class->isTrait()) {
continue;
}
$this->generate($class);
}
$this->generate(new ReflectionClass(DbPropertiesTrait::class));
}
private function generate(ReflectionClass $class): void
{
$name = $class->getName();
$doc = $class->getDocComment();
if (!$doc) {
throw new Exception("$name has no PHPDOC!");
}
$doc = $this->factory->create($doc);
$title = $doc->getSummary();
$description = $doc->getDescription();
$tags = $doc->getTags();
$seeAlso = [];
$properties = [];
$author = new Author("Daniil Gentili", "daniil@daniil.it");
foreach ($tags as $tag) {
if ($tag instanceof Author) {
$author = $tag;
}
if ($tag instanceof Deprecated) {
return;
}
if ($tag instanceof Generic && $tag->getName() === 'internal') {
return;
}
if ($tag instanceof See) {
$seeAlso[$tag->getReference()->__toString()] = $tag->render();
}
if ($tag instanceof Property) {
$properties[$tag->getVariableName()] = [
$tag->getType(),
$tag->getDescription()
];
}
if ($tag instanceof InvalidTag && $tag->getName() === 'property') {
[$type, $description] = \explode(" $", $tag->render(), 2);
$description .= ' ';
[$varName, $description] = \explode(" ", $description, 2);
$properties[$varName] = [
\str_replace('@property ', '', $type),
$description ?? ''
];
}
}
$constants = [];
foreach ($class->getConstants() as $key => $value) {
$refl = new ReflectionClassConstant($name, $key);
$doc = $this->factory->create($refl->getDocComment() ?? '');
$constants[$key] = [
$value,
$description,
$doc->getDescription()
];
}
}
}

View File

@ -5,6 +5,9 @@ namespace danog\MadelineProto;
use Psr\Log\AbstractLogger;
use Psr\Log\LogLevel;
/**
* PSR-3 wrapper for MadelineProto's Logger.
*/
class PsrLogger extends AbstractLogger
{
private const LEVEL_MAP = [

View File

@ -19,6 +19,9 @@
namespace danog\MadelineProto;
/**
* Indicates an error returned by Telegram's API.
*/
class RPCErrorException extends \Exception
{
use TL\PrettyException;

View File

@ -19,6 +19,9 @@
namespace danog\MadelineProto;
/**
* Indicates an error thrown when an unexpected response is received from telegram's servers.
*/
class ResponseException extends \Exception
{
}

View File

@ -19,6 +19,9 @@
namespace danog\MadelineProto;
/**
* Indicates a security error.
*/
class SecurityException extends \Exception
{
}

View File

@ -21,6 +21,9 @@ use danog\MadelineProto\Settings\Serialization;
use danog\MadelineProto\Settings\Templates;
use danog\MadelineProto\Settings\TLSchema;
/**
* Settings class used for configuring MadelineProto.
*/
class Settings extends SettingsAbstract
{
/**

View File

@ -8,6 +8,9 @@ use danog\MadelineProto\Magic;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\SettingsAbstract;
/**
* App information.
*/
class AppInfo extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* Cryptography settings.
*/
class Auth extends SettingsAbstract
{
/**

View File

@ -21,6 +21,9 @@ use danog\MadelineProto\Stream\Transport\DefaultStream;
use danog\MadelineProto\Stream\Transport\WssStream;
use danog\MadelineProto\Stream\Transport\WsStream;
/**
* Connection settings.
*/
class Connection extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings\Database;
use danog\MadelineProto\Settings\DatabaseAbstract;
/**
* Memory backend settings.
*/
class Memory extends DatabaseAbstract
{
/**

View File

@ -2,6 +2,9 @@
namespace danog\MadelineProto\Settings\Database;
/**
* MySQL backend settings.
*/
class Mysql extends SqlAbstract
{
public function mergeArray(array $settings): void

View File

@ -2,6 +2,9 @@
namespace danog\MadelineProto\Settings\Database;
/**
* Postgres backend settings.
*/
class Postgres extends SqlAbstract
{
public function mergeArray(array $settings): void

View File

@ -2,6 +2,9 @@
namespace danog\MadelineProto\Settings\Database;
/**
* Redis backend settings.
*/
class Redis extends DatabaseAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings\Database;
use danog\MadelineProto\Settings\Database;
/**
* Generic db backend settings.
*/
abstract class SqlAbstract extends DatabaseAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* File management settings.
*/
class Files extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* IPC server settings.
*/
class Ipc extends SettingsAbstract
{
/**

View File

@ -7,6 +7,9 @@ use danog\MadelineProto\Magic;
use danog\MadelineProto\SettingsAbstract;
use danog\MadelineProto\Tools;
/**
* Logger settings.
*/
class Logger extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* Peer database settings.
*/
class Peer extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* PWRTelegram settings.
*/
class Pwr extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* RPC settings.
*/
class RPC extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* Secret chat settings.
*/
class SecretChats extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* Serialization settings.
*/
class Serialization extends SettingsAbstract
{
/**

View File

@ -4,6 +4,9 @@ namespace danog\MadelineProto\Settings;
use danog\MadelineProto\SettingsAbstract;
/**
* TL schema settings.
*/
class TLSchema extends SettingsAbstract
{
/**

View File

@ -18,6 +18,8 @@ abstract class SettingsAbstract
*
* @param array $settings Settings array
*
* @internal
*
* @return void
*/
public function mergeArray(array $settings): void
@ -29,6 +31,8 @@ abstract class SettingsAbstract
*
* @param self $other
*
* @internal
*
* @return void
*/
public function merge(self $other): void
@ -84,6 +88,8 @@ abstract class SettingsAbstract
/**
* Apply changes.
*
* @internal
*
* @return static
*/
public function applyChanges(): self

View File

@ -19,8 +19,8 @@
namespace danog\MadelineProto;
/*
* Controls script shutdown
/**
* Class that controls script shutdown.
*/
class Shutdown
{

View File

@ -19,6 +19,9 @@
namespace danog\MadelineProto\TL\Conversion;
/**
* TL conversion exception.
*/
class Exception extends \Exception
{
use \danog\MadelineProto\TL\PrettyException;

View File

@ -19,6 +19,9 @@
namespace danog\MadelineProto\TL;
/**
* TL deserialization exception.
*/
class Exception extends \Exception
{
use PrettyException;

View File

@ -19,9 +19,13 @@
namespace danog\MadelineProto\TL\Types;
use danog\MadelineProto\API;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\Tools;
/**
* Clickable button.
*/
class Button implements \JsonSerializable, \ArrayAccess
{
/**
@ -31,9 +35,15 @@ class Button implements \JsonSerializable, \ArrayAccess
*/
private array $button;
/**
* MTProto instance.
* Session name.
*/
private MTProto $API;
private string $session;
/**
* MTProto instance.
*
* @var MTProto|API
*/
private $API;
/**
* Message ID.
*/
@ -62,6 +72,7 @@ class Button implements \JsonSerializable, \ArrayAccess
: $message['peer_id'];
$this->id = $message['id'];
$this->API = $API;
$this->session = $API->getWrapper()->getSession()->getLegacySessionPath();
}
/**
* Sleep function.
@ -70,7 +81,7 @@ class Button implements \JsonSerializable, \ArrayAccess
*/
public function __sleep(): array
{
return ['button', 'peer', 'id', 'API'];
return ['button', 'peer', 'id', 'session'];
}
/**
* Click on button.
@ -81,7 +92,11 @@ class Button implements \JsonSerializable, \ArrayAccess
*/
public function click(bool $donotwait = true)
{
$async = isset($this->API->wrapper) ? $this->API->wrapper->isAsync() : true;
if (!isset($this->API)) {
$this->API = new API($this->session);
} else {
$async = isset($this->API->wrapper) ? $this->API->wrapper->isAsync() : true;
}
$method = $donotwait ? 'methodCallAsyncWrite' : 'methodCallAsyncRead';
switch ($this->button['_']) {
default:

View File

@ -19,6 +19,9 @@
namespace danog\MadelineProto\TL\Types;
/**
* Bytes wrapper.
*/
class Bytes implements \JsonSerializable, \ArrayAccess
{
/**