1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-22 16:31:11 +01:00

Choose simpler way of interrupting execution flow to install plugins

This commit is contained in:
Daniil Gentili 2023-07-09 13:54:41 +02:00
parent 8b65f51bb0
commit b51f388696
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 16 additions and 13 deletions

View File

@ -187,10 +187,6 @@ class MyEventHandler extends EventHandler
} }
} }
if (MyEventHandler::isPluginMode()) {
return MyEventHandler::class;
}
$settings = new Settings; $settings = new Settings;
$settings->getLogger()->setLevel(Logger::LEVEL_ULTRA_VERBOSE); $settings->getLogger()->setLevel(Logger::LEVEL_ULTRA_VERBOSE);
@ -200,7 +196,7 @@ $settings->getLogger()->setLevel(Logger::LEVEL_ULTRA_VERBOSE);
// $settings->setDb((new Mysql)->setDatabase('MadelineProto')->setUsername('daniil')->setPassword('pony')); // $settings->setDb((new Mysql)->setDatabase('MadelineProto')->setUsername('daniil')->setPassword('pony'));
// For users or bots // For users or bots
return MyEventHandler::startAndLoop('bot.madeline', $settings); MyEventHandler::startAndLoop('bot.madeline', $settings);
// For bots only // For bots only
// return MyEventHandler::startAndLoopBot('bot.madeline', 'bot token', $settings); // MyEventHandler::startAndLoopBot('bot.madeline', 'bot token', $settings);

View File

@ -23,12 +23,14 @@ namespace danog\MadelineProto;
use Amp\DeferredFuture; use Amp\DeferredFuture;
use Amp\Future; use Amp\Future;
use Amp\Sync\LocalMutex; use Amp\Sync\LocalMutex;
use AssertionError;
use Closure; use Closure;
use danog\MadelineProto\Db\DbPropertiesTrait; use danog\MadelineProto\Db\DbPropertiesTrait;
use danog\MadelineProto\EventHandler\Filter\Combinator\FiltersAnd; use danog\MadelineProto\EventHandler\Filter\Combinator\FiltersAnd;
use danog\MadelineProto\EventHandler\Filter\Filter; use danog\MadelineProto\EventHandler\Filter\Filter;
use danog\MadelineProto\EventHandler\Handler; use danog\MadelineProto\EventHandler\Handler;
use danog\MadelineProto\EventHandler\Update; use danog\MadelineProto\EventHandler\Update;
use Fiber;
use Generator; use Generator;
use ReflectionAttribute; use ReflectionAttribute;
use ReflectionClass; use ReflectionClass;
@ -49,9 +51,6 @@ abstract class EventHandler extends AbstractAPI
DbPropertiesTrait::initDb as private internalInitDb; DbPropertiesTrait::initDb as private internalInitDb;
} }
private static bool $includingPlugins = false; private static bool $includingPlugins = false;
final public static function isPluginMode(): bool {
return self::$includingPlugins;
}
/** /**
* Start MadelineProto and the event handler. * Start MadelineProto and the event handler.
* *
@ -62,6 +61,10 @@ abstract class EventHandler extends AbstractAPI
*/ */
final public static function startAndLoop(string $session, SettingsAbstract $settings): void final public static function startAndLoop(string $session, SettingsAbstract $settings): void
{ {
if (self::$includingPlugins) {
Fiber::suspend(static::class);
throw new AssertionError("Unreachable!");
}
$API = new API($session, $settings); $API = new API($session, $settings);
$API->startAndLoopInternal(static::class); $API->startAndLoopInternal(static::class);
} }
@ -76,6 +79,10 @@ abstract class EventHandler extends AbstractAPI
*/ */
final public static function startAndLoopBot(string $session, string $token, SettingsAbstract $settings): void final public static function startAndLoopBot(string $session, string $token, SettingsAbstract $settings): void
{ {
if (self::$includingPlugins) {
Fiber::suspend(static::class);
throw new AssertionError("Unreachable!");
}
$API = new API($session, $settings); $API = new API($session, $settings);
$API->botLogin($token); $API->botLogin($token);
$API->startAndLoopInternal(static::class); $API->startAndLoopInternal(static::class);
@ -256,15 +263,15 @@ abstract class EventHandler extends AbstractAPI
$paths = []; $paths = [];
} }
$plugins = $this->getPlugins(); $plugins = \array_values($this->getPlugins());
$plugins = \array_values(\array_unique($plugins));
$recurse = static function (string $path) use (&$recurse, &$plugins): void { $recurse = static function (string $path) use (&$recurse, &$plugins): void {
foreach (listFiles($path) as $file) { foreach (listFiles($path) as $file) {
if (isDirectory($file)) { if (isDirectory($file)) {
$recurse($file); $recurse($file);
} elseif (isFile($file) && \str_ends_with($file, ".plugin.php")) { } elseif (isFile($file) && \str_ends_with($file, ".plugin.php")) {
$plugins []= require $file; $f = new Fiber(fn () => require $file);
$plugins []= $f->start();
} }
} }
}; };
@ -276,7 +283,7 @@ abstract class EventHandler extends AbstractAPI
self::$includingPlugins = false; self::$includingPlugins = false;
} }
$plugins = \array_values(\array_unique($plugins)); $plugins = \array_values(\array_unique($plugins, SORT_REGULAR));
foreach ($plugins as $plugin) { foreach ($plugins as $plugin) {
Assert::classExists($plugin); Assert::classExists($plugin);