1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-12-02 14:17:45 +01:00

Allow usage of getPlugin from IPC clients thanks to the PluginEventHandlerProxy

This commit is contained in:
Daniil Gentili 2023-07-10 19:21:22 +02:00
parent e5fddecce9
commit f314cd9f2c
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
7 changed files with 94 additions and 18 deletions

View File

@ -263,7 +263,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/stickers.changeStickerPosition.html" name="stickers.changeStickerPosition">Changes the absolute position of a sticker in the set to which it belongs; for bots only. The sticker set must have been created by the bot: stickers.changeStickerPosition</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/stickers.changeStickerPosition.html" name="stickers.changeStickerPosition">Changes the absolute position of a sticker in the set to which it belongs; for bots only. The sticker set must have been created by the bot: stickers.changeStickerPosition</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.setDefaultHistoryTTL.html" name="messages.setDefaultHistoryTTL">Changes the default value of the Time-To-Live setting, applied to all new chats: messages.setDefaultHistoryTTL</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.setDefaultHistoryTTL.html" name="messages.setDefaultHistoryTTL">Changes the default value of the Time-To-Live setting, applied to all new chats: messages.setDefaultHistoryTTL</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.updateUsername.html" name="account.updateUsername">Changes username for the current user: account.updateUsername</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/account.updateUsername.html" name="account.updateUsername">Changes username for the current user: account.updateUsername</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#hasplugininstance-class-string-eventhandler-class-bool" name="hasPluginInstance">Check if a certain event handler plugin is installed: hasPluginInstance</a> * <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#hasplugin-class-string-eventhandler-class-bool" name="hasPlugin">Check if a certain event handler plugin is installed: hasPlugin</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.checkUsername.html" name="channels.checkUsername">Check if a username is free and can be assigned to a channel/supergroup: channels.checkUsername</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.checkUsername.html" name="channels.checkUsername">Check if a username is free and can be assigned to a channel/supergroup: channels.checkUsername</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#haseventhandler-bool" name="hasEventHandler">Check if an event handler instance is present: hasEventHandler</a> * <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#haseventhandler-bool" name="hasEventHandler">Check if an event handler instance is present: hasEventHandler</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#hasreportpeers-bool" name="hasReportPeers">Check if has report peers: hasReportPeers</a> * <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#hasreportpeers-bool" name="hasReportPeers">Check if has report peers: hasReportPeers</a>
@ -628,7 +628,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.readMessageContents.html" name="messages.readMessageContents">Notifies the sender about the recipient having listened a voice message or watched a video: messages.readMessageContents</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.readMessageContents.html" name="messages.readMessageContents">Notifies the sender about the recipient having listened a voice message or watched a video: messages.readMessageContents</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendScreenshotNotification.html" name="messages.sendScreenshotNotification">Notify the other user in a private chat that a screenshot of the chat was taken: messages.sendScreenshotNotification</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.sendScreenshotNotification.html" name="messages.sendScreenshotNotification">Notify the other user in a private chat that a screenshot of the chat was taken: messages.sendScreenshotNotification</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/users.setSecureValueErrors.html" name="users.setSecureValueErrors">Notify the user that the sent passport data contains some errors The user will not be able to re-submit their Passport data to you until the errors are fixed (the contents of the field for which you returned the error must change): users.setSecureValueErrors</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/users.setSecureValueErrors.html" name="users.setSecureValueErrors">Notify the user that the sent passport data contains some errors The user will not be able to re-submit their Passport data to you until the errors are fixed (the contents of the field for which you returned the error must change): users.setSecureValueErrors</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getplugininstance-class-string-t-class-danog-madelineproto-eventhandler" name="getPluginInstance">Obtain a certain event handler plugin instance: getPluginInstance</a> * <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getplugin-class-string-t-class-danog-madelineproto-plugineventhandler-danog-madelineproto-ipc-plugineventhandlerproxy-null" name="getPlugin">Obtain a certain event handler plugin instance: getPlugin</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.getBotCommands.html" name="bots.getBotCommands">Obtain a list of bot commands for the specified bot scope and language code: bots.getBotCommands</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/bots.getBotCommands.html" name="bots.getBotCommands">Obtain a list of bot commands for the specified bot scope and language code: bots.getBotCommands</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getEmojiKeywordsLanguages.html" name="messages.getEmojiKeywordsLanguages">Obtain a list of related languages that must be used when fetching emoji keyword lists »: messages.getEmojiKeywordsLanguages</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getEmojiKeywordsLanguages.html" name="messages.getEmojiKeywordsLanguages">Obtain a list of related languages that must be used when fetching emoji keyword lists »: messages.getEmojiKeywordsLanguages</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getAvailableReactions.html" name="messages.getAvailableReactions">Obtain available message reactions »: messages.getAvailableReactions</a> * <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getAvailableReactions.html" name="messages.getAvailableReactions">Obtain available message reactions »: messages.getAvailableReactions</a>

View File

@ -949,11 +949,11 @@ abstract class InternalDoc
* *
* @param class-string<T> $class * @param class-string<T> $class
* *
* return T * return T|null
*/ */
public function getPluginInstance(string $class): \danog\MadelineProto\EventHandler public function getPlugin(string $class): \danog\MadelineProto\PluginEventHandler|\danog\MadelineProto\Ipc\PluginEventHandlerProxy|null
{ {
return $this->wrapper->getAPI()->getPluginInstance($class); return $this->wrapper->getAPI()->getPlugin($class);
} }
/** /**
* Get download info of the propic of a user * Get download info of the propic of a user
@ -1081,9 +1081,9 @@ abstract class InternalDoc
* *
* @param class-string<EventHandler> $class * @param class-string<EventHandler> $class
*/ */
public function hasPluginInstance(string $class): bool public function hasPlugin(string $class): bool
{ {
return $this->wrapper->getAPI()->hasPluginInstance($class); return $this->wrapper->getAPI()->hasPlugin($class);
} }
/** /**
* Check if has report peers. * Check if has report peers.

View File

@ -78,6 +78,11 @@ final class Client extends ClientAbstract
self::$instances[$session->getSessionDirectoryPath()] = $this; self::$instances[$session->getSessionDirectoryPath()] = $this;
EventLoop::queue($this->loopInternal(...)); EventLoop::queue($this->loopInternal(...));
} }
/** @internal */
public function getSession(): SessionPaths
{
return $this->session;
}
/** /**
* Run the provided async callable. * Run the provided async callable.
* *
@ -316,4 +321,8 @@ final class Client extends ClientAbstract
{ {
throw new Exception("Can't use ".__FUNCTION__.' in an IPC client instance, please use startAndLoop, instead!'); throw new Exception("Can't use ".__FUNCTION__.' in an IPC client instance, please use startAndLoop, instead!');
} }
public function getPlugin(string $class): ?PluginEventHandlerProxy
{
return $this->hasPlugin($class) ? new PluginEventHandlerProxy($class, $this) : null;
}
} }

View File

@ -16,10 +16,14 @@ abstract class IpcCapable
/** @internal */ /** @internal */
protected function __construct( protected function __construct(
MTProto $API, MTProto|Client $API,
) { ) {
$this->API = $API; $this->API = $API;
if ($API instanceof MTProto) {
$this->session = $API->wrapper->getSession()->getSessionDirectoryPath(); $this->session = $API->wrapper->getSession()->getSessionDirectoryPath();
} else {
$this->session = $API->getSession()->getSessionDirectoryPath();
}
} }
/** @internal */ /** @internal */

View File

@ -0,0 +1,40 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\Ipc;
/**
* Plugin event handler proxy object, for use through the IPC API.
*/
final class PluginEventHandlerProxy extends IpcCapable
{
public function __construct(
private readonly ?string $__plugin,
Client $API
) {
parent::__construct($API);
}
public function __call(string $name, array $arguments): mixed
{
return $this->API->callPluginMethod(
$this->__plugin,
$name,
$arguments
);
}
public function __get(string $name): mixed
{
return $this->API->getPluginProperty($this->__plugin, $name);
}
public function __set(string $name, mixed $value): void
{
$this->API->setPluginProperty($this->__plugin, $name, $value);
}
public function __isset(string $name): bool
{
return $this->API->issetPluginProperty($this->__plugin, $name);
}
public function __unset(string $name): void
{
$this->API->unsetPluginProperty($this->__plugin, $name);
}
}

View File

@ -1740,7 +1740,7 @@ final class MTProto implements TLCallback, LoggerGetter
throw Exception::extension('memprof'); throw Exception::extension('memprof');
} }
if (!\memprof_enabled()) { if (!\memprof_enabled()) {
throw new Exception("Memory profiling is not enabled in the database settings, set the MEMPROF_PROFILE=1 environment variable or GET parameter to enable it."); throw new Exception("Memory profiling is not enabled, set the MEMPROF_PROFILE=1 environment variable or GET parameter to enable it.");
} }
$current = "Current memory usage: ".\round(\memory_get_usage()/1024/1024, 1) . " MB"; $current = "Current memory usage: ".\round(\memory_get_usage()/1024/1024, 1) . " MB";

View File

@ -21,9 +21,10 @@ declare(strict_types=1);
namespace danog\MadelineProto\Wrappers; namespace danog\MadelineProto\Wrappers;
use __PHP_Incomplete_Class; use __PHP_Incomplete_Class;
use AssertionError;
use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Exception; use danog\MadelineProto\Exception;
use danog\MadelineProto\Ipc\PluginEventHandlerProxy;
use danog\MadelineProto\PluginEventHandler;
use danog\MadelineProto\Settings; use danog\MadelineProto\Settings;
use danog\MadelineProto\UpdateHandlerType; use danog\MadelineProto\UpdateHandlerType;
@ -107,7 +108,7 @@ trait Events
* *
* @param class-string<EventHandler> $class * @param class-string<EventHandler> $class
*/ */
final public function hasPluginInstance(string $class): bool final public function hasPlugin(string $class): bool
{ {
return isset($this->pluginInstances[$class]); return isset($this->pluginInstances[$class]);
} }
@ -118,14 +119,11 @@ trait Events
* *
* @param class-string<T> $class * @param class-string<T> $class
* *
* return T * return T|null
*/ */
final public function getPluginInstance(string $class): EventHandler final public function getPlugin(string $class): PluginEventHandler|PluginEventHandlerProxy|null
{ {
if (!isset($this->pluginInstances[$class]) || !$this->pluginInstances[$class] instanceof EventHandler) { return $this->pluginInstances[$class] ?? null;
throw new AssertionError("Plugin instance for $class not found!");
}
return $this->pluginInstances[$class];
} }
/** /**
* Unset event handler. * Unset event handler.
@ -154,4 +152,29 @@ trait Events
{ {
return isset($this->event_handler_instance); return isset($this->event_handler_instance);
} }
/** @internal */
public function callPluginMethod(string $class, string $method, array $args): mixed
{
return $this->pluginInstances[$class]->$method(...$args);
}
/** @internal */
public function setPluginProperty(string $class, string $property, mixed $value): void
{
$this->pluginInstances[$class]->$property = $value;
}
/** @internal */
public function getPluginProperty(string $class, string $property): mixed
{
return $this->pluginInstances[$class]->$property;
}
/** @internal */
public function issetPluginProperty(string $class, string $property): bool
{
return isset($this->pluginInstances[$class]->$property);
}
/** @internal */
public function unsetPluginProperty(string $class, string $property): void
{
unset($this->pluginInstances[$class]->$property);
}
} }