1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-22 14:51:17 +01:00
This commit is contained in:
Daniil Gentili 2023-07-13 15:25:13 +02:00
parent df1019c941
commit 3e8047cd1b
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 30 additions and 43 deletions

View File

@ -79,7 +79,7 @@ abstract class EventHandler extends AbstractAPI
final public static function startAndLoop(string $session, ?SettingsAbstract $settings = null): void
{
if (self::$includingPlugins) {
throw new PluginRegistration(static::class);
return;
}
$settings ??= new SettingsEmpty;
$API = new API($session, $settings);
@ -97,7 +97,7 @@ abstract class EventHandler extends AbstractAPI
final public static function startAndLoopBot(string $session, string $token, ?SettingsAbstract $settings = null): void
{
if (self::$includingPlugins) {
throw new PluginRegistration(static::class);
return;
}
$settings ??= new SettingsEmpty;
$API = new API($session, $settings);
@ -341,7 +341,16 @@ abstract class EventHandler extends AbstractAPI
} else {
$paths = \array_values($paths);
}
$paths = \array_map(realpath(...), $paths);
foreach ($paths as &$path) {
$pathNew = \realpath($path);
if ($pathNew === false) {
$pathNew = \realpath(\dirname((new ReflectionClass(static::class))->getFileName()).DIRECTORY_SEPARATOR.$path);
if ($pathNew === false) {
throw new AssertionError("$path does not exist!");
}
}
$path = $pathNew;
}
if (!$paths) {
return [];
@ -350,19 +359,26 @@ abstract class EventHandler extends AbstractAPI
$pluginsTemp = [];
$recurse = static function (string $path, string $namespace = 'MadelinePlugin') use (&$recurse, &$pluginsTemp): void {
foreach (listFiles($path) as $file) {
$file = $path.DIRECTORY_SEPARATOR.$file;
if (isDirectory($file)) {
$recurse($file, $namespace.'\\'.\basename($file));
} elseif (isFile($file) && \str_ends_with($file, "Plugin.php")) {
} elseif (isFile($file) && \str_ends_with($file, ".php")) {
$file = \realpath($file);
$class = $namespace.'\\'.\basename($file, '.php');
try {
$fileName = \basename($file, '.php');
if ($fileName === 'functions.php') {
require $file;
} catch (PluginRegistration $e) {
Assert::eq($e->plugin, $class);
$pluginsTemp []= $e->plugin;
continue;
}
throw new AssertionError("No plugin was registered after including $file!");
$class = $namespace.'\\'.$fileName;
if (!\class_exists($class)) {
throw new AssertionError("$class was not defined when including $file!");
}
if ((new ReflectionClass($class))->getFileName() !== $file) {
throw new AssertionError("$class was not defined when including $file, the same plugin is present in multiple plugin paths/composer!");
}
if (\is_subclass_of($class, PluginEventHandler::class)) {
$pluginsTemp []= $class;
}
}
}
};

View File

@ -489,6 +489,8 @@ final class MTProto implements TLCallback, LoggerGetter
$this->initPromise = $initDeferred->getFuture();
try {
$this->initialize($settings);
} catch (Throwable $e) {
$this->report((string) $e);
} finally {
$initDeferred->complete();
}
@ -1052,6 +1054,8 @@ final class MTProto implements TLCallback, LoggerGetter
foreach ($this->broadcasts as $broadcast) {
$broadcast->resume();
}
} catch (Throwable $e) {
$this->report((string) $e);
} finally {
$deferred->complete();
}

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
/**
* PTSException 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-2023 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 Error;
/**
* @internal Used to register a plugin
*/
final class PluginRegistration extends Error
{
public function __construct(public readonly string $plugin)
{
}
}