MadelineProto/src/danog/MadelineProto/AbstractAPIFactory.php

263 lines
7.8 KiB
PHP
Raw Permalink Normal View History

2019-12-14 16:47:04 +01:00
<?php
/**
* APIFactory 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>
2020-02-17 14:13:46 +01:00
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
2019-12-14 16:47:04 +01:00
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
use danog\MadelineProto\Async\AsyncConstruct;
2020-09-24 11:45:20 +02:00
use danog\MadelineProto\Ipc\Client;
2019-12-14 16:47:04 +01:00
abstract class AbstractAPIFactory extends AsyncConstruct
{
2019-12-28 17:34:04 +01:00
/**
* Namespace.
*
* @internal
*
* @var string
*/
private string $namespace = '';
2019-12-28 17:34:04 +01:00
/**
* Whether lua is being used.
*
* @internal
*
* @var boolean
*/
public bool $lua = false;
2019-12-28 17:34:04 +01:00
/**
* Whether async is enabled.
*
* @internal
*
* @var boolean
*/
2020-02-26 18:12:00 +01:00
protected bool $async = false;
2019-12-28 17:34:04 +01:00
/**
* Method list.
2019-12-28 17:34:04 +01:00
*
2020-10-03 15:36:03 +02:00
* @var array<string, callable>
2019-12-28 17:34:04 +01:00
*/
protected array $methods = [];
2020-09-24 11:45:20 +02:00
/**
* Main API instance.
*/
private API $mainAPI;
2019-12-28 17:34:04 +01:00
/**
* Export APIFactory instance with the specified namespace.
2019-12-28 17:34:04 +01:00
*
* @param string $namespace Namespace
*
* @return self
*/
protected function exportNamespace(string $namespace = ''): self
{
2020-02-27 00:36:33 +01:00
$class = \array_reverse(\array_values(\class_parents(static::class)))[$namespace ? 2 : 3];
$instance = new $class;
2020-02-27 00:05:53 +01:00
$instance->namespace = $namespace ? $namespace.'.' : '';
self::link($instance, $this);
return $instance;
}
/**
2020-02-26 11:47:30 +01:00
* Link two APIFactory instances.
*
* @param self $a First instance
* @param self $b Second instance
2020-02-26 11:47:30 +01:00
*
* @return void
2019-12-28 17:34:04 +01:00
*/
protected static function link(self $a, self $b): void
2019-12-14 16:47:04 +01:00
{
$a->lua =& $b->lua;
$a->async =& $b->async;
$a->methods =& $b->methods;
2020-09-24 11:45:20 +02:00
if ($b instanceof API) {
$a->mainAPI = $b;
2020-09-24 20:49:34 +02:00
$b->mainAPI = $b;
} elseif ($a instanceof API) {
$a->mainAPI = $a;
$b->mainAPI = $a;
2020-09-24 11:45:20 +02:00
} else {
$a->mainAPI =& $b->mainAPI;
}
2020-09-23 00:57:49 +02:00
if (!$b->inited()) {
$a->setInitPromise($b->initAsynchronously());
}
2019-12-14 16:47:04 +01:00
}
2019-12-28 17:34:04 +01:00
/**
* Enable or disable async.
*
* @param bool $async Whether to enable or disable async
*
* @return void
*/
public function async(bool $async): void
2019-12-14 18:31:28 +01:00
{
$this->async = $async;
}
2019-12-28 17:34:04 +01:00
/**
* Call async wrapper function.
*
* @param string $name Method name
* @param array $arguments Arguments
*
* @internal
*
* @return mixed
*/
public function __call(string $name, array $arguments)
2019-12-14 16:47:04 +01:00
{
$yielded = Tools::call($this->__call_async($name, $arguments));
$async = !$this->lua && ((\is_array(\end($arguments)) ? \end($arguments) : [])['async'] ?? ($this->async && $name !== 'loop'));
2020-02-25 19:10:49 +01:00
2019-12-14 16:47:04 +01:00
if ($async) {
return $yielded;
}
$yielded = Tools::wait($yielded);
2019-12-14 16:47:04 +01:00
if (!$this->lua) {
return $yielded;
2019-12-14 16:47:04 +01:00
}
try {
Lua::convertObjects($yielded);
return $yielded;
} catch (\Throwable $e) {
return ['error_code' => $e->getCode(), 'error' => $e->getMessage()];
}
}
2020-02-25 19:10:49 +01:00
/**
2020-02-26 11:47:30 +01:00
* Info to dump.
2020-02-25 19:10:49 +01:00
*
* @return array
*/
public function __debugInfo(): array
{
2020-10-08 21:35:32 +02:00
$keys = APIWrapper::properties();
2020-02-25 19:10:49 +01:00
$res = [];
foreach ($keys as $key) {
2020-10-08 21:35:32 +02:00
if (Tools::hasVar($this, $key)) {
$res[$key] = Tools::getVar($this, $key);
}
2020-02-25 19:10:49 +01:00
}
return $res;
}
2020-09-28 11:56:14 +02:00
/**
* Sleep function.
*
* @return array
*/
public function __sleep()
{
return [];
}
2019-12-28 17:34:04 +01:00
/**
* Call async wrapper function.
*
* @param string $name Method name
* @param array $arguments Arguments
*
* @internal
*
* @return \Generator
*/
public function __call_async(string $name, array $arguments): \Generator
2019-12-14 16:47:04 +01:00
{
yield from $this->initAsynchronously();
2019-12-14 16:47:04 +01:00
$lower_name = \strtolower($name);
if ($this->namespace !== '' || !isset($this->methods[$lower_name])) {
2020-02-05 17:18:22 +01:00
$name = $this->namespace.$name;
2019-12-14 16:47:04 +01:00
$aargs = isset($arguments[1]) && \is_array($arguments[1]) ? $arguments[1] : [];
$aargs['apifactory'] = true;
$args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : [];
return yield from $this->mainAPI->API->methodCallAsyncRead($name, $args, $aargs);
2019-12-14 16:47:04 +01:00
}
2020-09-24 20:49:34 +02:00
if ($lower_name === 'seteventhandler'
|| ($lower_name === 'loop' && !isset($arguments[0]))
2020-09-24 11:45:20 +02:00
) {
2020-09-24 20:49:34 +02:00
yield from $this->mainAPI->reconnectFull();
2020-09-24 11:45:20 +02:00
}
$res = $this->methods[$lower_name](...$arguments);
2020-02-05 17:18:22 +01:00
return $res instanceof \Generator ? yield from $res : yield $res;
2019-12-14 16:47:04 +01:00
}
/**
* Get fully resolved method list for object, including snake_case and camelCase variants.
*
2020-09-24 23:25:54 +02:00
* @param API|MTProto|Client $value Value
* @param string $class Custom class name
*
* @return array
*/
2020-07-11 20:13:06 +02:00
protected static function getInternalMethodList($value, string $class = null): array
{
return \array_map(fn ($method) => [$value, $method], self::getInternalMethodListClass($class ?? \get_class($value)));
}
/**
* Get fully resolved method list for object, including snake_case and camelCase variants.
*
* @param string $class Class name
*
* @return array
*/
protected static function getInternalMethodListClass(string $class): array
{
static $cache = [];
if (isset($cache[$class])) {
2020-07-11 20:13:06 +02:00
return $cache[$class];
}
2020-07-11 20:13:06 +02:00
$methods = \get_class_methods($class);
foreach ($methods as $method) {
if ($method == 'methodCallAsyncRead') {
unset($methods[\array_search('methodCall', $methods)]);
} elseif (\stripos($method, 'async') !== false) {
if (\strpos($method, '_async') !== false) {
unset($methods[\array_search(\str_ireplace('_async', '', $method), $methods)]);
} else {
unset($methods[\array_search(\str_ireplace('async', '', $method), $methods)]);
}
}
}
$finalMethods = [];
foreach ($methods as $method) {
$actual_method = $method;
if ($method == 'methodCallAsyncRead') {
$method = 'methodCall';
} elseif (\stripos($method, 'async') !== false) {
if (\strpos($method, '_async') !== false) {
$method = \str_ireplace('_async', '', $method);
} else {
$method = \str_ireplace('async', '', $method);
}
}
$finalMethods[\strtolower($method)] = $actual_method;
if (\strpos($method, '_') !== false) {
$finalMethods[\strtolower(\str_replace('_', '', $method))] = $actual_method;
} else {
2020-06-16 17:52:55 +02:00
$finalMethods[\strtolower(StrTools::toSnakeCase($method))] = $actual_method;
}
}
$cache[$class] = $finalMethods;
2020-07-11 20:13:06 +02:00
return $finalMethods;
}
2019-12-14 16:47:04 +01:00
}