This commit is contained in:
Daniil Gentili 2024-06-30 16:05:29 +02:00
parent 2cf0f8b6e8
commit 41d37e28c5
9 changed files with 41 additions and 41 deletions

View File

@ -23,10 +23,10 @@ class Client
public static function getInstance(): Client public static function getInstance(): Client
{ {
if (empty(static::$self)) { if (empty(self::$self)) {
static::$self = new static(); self::$self = new static();
} }
return static::$self; return self::$self;
} }
public function connect(array $sessionFiles) public function connect(array $sessionFiles)

View File

@ -11,11 +11,11 @@ class Config
public static function getInstance(): Config public static function getInstance(): Config
{ {
if (null === static::$instance) { if (null === self::$instance) {
static::$instance = new static(); self::$instance = new static();
} }
return static::$instance; return self::$instance;
} }
/** /**

View File

@ -12,8 +12,8 @@ class EventHandler extends \danog\MadelineProto\EventHandler
public function onStart() public function onStart()
{ {
$this->sessionName = Files::getSessionName($this->wrapper->getSession()->getSessionPath()); $this->sessionName = Files::getSessionName($this->wrapper->getSession()->getSessionPath());
if (empty(static::$instances[$this->sessionName])) { if (empty(self::$instances[$this->sessionName])) {
static::$instances[$this->sessionName] = true; self::$instances[$this->sessionName] = true;
warning("Event observer CONSTRUCTED: {$this->sessionName}"); warning("Event observer CONSTRUCTED: {$this->sessionName}");
} }
} }
@ -23,7 +23,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler
if (empty($this->sessionName)) { if (empty($this->sessionName)) {
return; return;
} }
unset(static::$instances[$this->sessionName]); unset(self::$instances[$this->sessionName]);
warning("Event observer DESTRUCTED: {$this->sessionName}"); warning("Event observer DESTRUCTED: {$this->sessionName}");
} }

View File

@ -18,7 +18,7 @@ class EventObserver
public static function notify(array $update, string $sessionName) public static function notify(array $update, string $sessionName)
{ {
foreach (static::$subscribers as $clientId => $callback) { foreach (self::$subscribers as $clientId => $callback) {
notice("Pass update to callback. ClientId: {$clientId}"); notice("Pass update to callback. ClientId: {$clientId}");
$callback($update, $sessionName); $callback($update, $sessionName);
} }
@ -26,16 +26,16 @@ class EventObserver
private static function addSessionClient(string $session): void private static function addSessionClient(string $session): void
{ {
if (empty(static::$sessionClients[$session])) { if (empty(self::$sessionClients[$session])) {
static::$sessionClients[$session] = 0; self::$sessionClients[$session] = 0;
} }
++static::$sessionClients[$session]; ++self::$sessionClients[$session];
} }
private static function removeSessionClient(string $session): void private static function removeSessionClient(string $session): void
{ {
if (!empty(static::$sessionClients[$session])) { if (!empty(self::$sessionClients[$session])) {
--static::$sessionClients[$session]; --self::$sessionClients[$session];
} }
} }
@ -49,8 +49,8 @@ class EventObserver
} }
foreach ($sessions as $session) { foreach ($sessions as $session) {
static::addSessionClient($session); self::addSessionClient($session);
if (static::$sessionClients[$session] === 1) { if (self::$sessionClients[$session] === 1) {
warning("Start EventHandler: {$session}"); warning("Start EventHandler: {$session}");
try { try {
$instance = Client::getInstance()->getSession($session); $instance = Client::getInstance()->getSession($session);
@ -60,7 +60,7 @@ class EventObserver
EventHandler::cachePlugins(EventHandler::class); EventHandler::cachePlugins(EventHandler::class);
$wrapper->getAPI()->setEventHandler(EventHandler::class); $wrapper->getAPI()->setEventHandler(EventHandler::class);
} catch (Throwable $e) { } catch (Throwable $e) {
static::removeSessionClient($session); self::removeSessionClient($session);
error('Cant set EventHandler', [ error('Cant set EventHandler', [
'session' => $session, 'session' => $session,
'exception' => Logger::getExceptionAsArray($e), 'exception' => Logger::getExceptionAsArray($e),
@ -79,11 +79,11 @@ class EventObserver
$sessions[] = $requestedSession; $sessions[] = $requestedSession;
} }
foreach ($sessions as $session) { foreach ($sessions as $session) {
static::removeSessionClient($session); self::removeSessionClient($session);
if (empty(static::$sessionClients[$session]) || $force) { if (empty(self::$sessionClients[$session]) || $force) {
warning("Stopping EventHandler: {$session}"); warning("Stopping EventHandler: {$session}");
Client::getInstance()->instances[$session]->unsetEventHandler(); Client::getInstance()->instances[$session]->unsetEventHandler();
unset(EventHandler::$instances[$session], static::$sessionClients[$session]); unset(EventHandler::$instances[$session], self::$sessionClients[$session]);
} }
} }

View File

@ -11,7 +11,7 @@ class LogObserver
public static function notify(string $level, string $message, array $context = []): void public static function notify(string $level, string $message, array $context = []): void
{ {
foreach (static::$subscribers as $clientId => $callback) { foreach (self::$subscribers as $clientId => $callback) {
$callback($level, $message, $context); $callback($level, $message, $context);
} }
} }

View File

@ -11,17 +11,17 @@ trait ObserverTrait
public static function addSubscriber($clientId, callable $callback): void public static function addSubscriber($clientId, callable $callback): void
{ {
notice("Add event listener. ClientId: {$clientId}"); notice("Add event listener. ClientId: {$clientId}");
static::$subscribers[$clientId] = $callback; self::$subscribers[$clientId] = $callback;
} }
public static function removeSubscriber($clientId): void public static function removeSubscriber($clientId): void
{ {
notice("Removing listener: {$clientId}"); notice("Removing listener: {$clientId}");
unset(static::$subscribers[$clientId]); unset(self::$subscribers[$clientId]);
$listenersCount = count(static::$subscribers); $listenersCount = count(self::$subscribers);
notice("Event listeners left: {$listenersCount}"); notice("Event listeners left: {$listenersCount}");
if ($listenersCount === 0) { if ($listenersCount === 0) {
static::$subscribers = []; self::$subscribers = [];
} }
} }
} }

View File

@ -29,7 +29,7 @@ class Files
} }
preg_match( preg_match(
'~' . static::SESSION_FOLDER . "/(?'sessionName'.*?)" . static::SESSION_EXTENSION . '~', '~' . self::SESSION_FOLDER . "/(?'sessionName'.*?)" . self::SESSION_EXTENSION . '~',
$sessionFile, $sessionFile,
$matches $matches
); );
@ -50,14 +50,14 @@ class Files
return null; return null;
} }
$session = trim(trim($session), '/'); $session = trim(trim($session), '/');
$session = static::SESSION_FOLDER . '/' . $session . $extension; $session = self::SESSION_FOLDER . '/' . $session . $extension;
$session = str_replace('//', '/', $session); $session = str_replace('//', '/', $session);
return $session; return $session;
} }
public static function getSessionSettings(string $session): array public static function getSessionSettings(string $session): array
{ {
$settingsFile = static::getSessionFile($session, static::SETTINGS_EXTENSION); $settingsFile = self::getSessionFile($session, self::SETTINGS_EXTENSION);
$settings = []; $settings = [];
if (file_exists($settingsFile)) { if (file_exists($settingsFile)) {
$settings = json_decode( $settings = json_decode(
@ -73,7 +73,7 @@ class Files
public static function saveSessionSettings(string $session, array $settings = []): void public static function saveSessionSettings(string $session, array $settings = []): void
{ {
$settingsFile = static::getSessionFile($session, static::SETTINGS_EXTENSION); $settingsFile = self::getSessionFile($session, self::SETTINGS_EXTENSION);
file_put_contents( file_put_contents(
$settingsFile, $settingsFile,
json_encode( json_encode(
@ -91,7 +91,7 @@ class Files
{ {
$files = glob($pattern, $flags) ?: []; $files = glob($pattern, $flags) ?: [];
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
$files = [...$files, ...static::globRecursive($dir . '/' . basename($pattern), $flags)]; $files = [...$files, ...self::globRecursive($dir . '/' . basename($pattern), $flags)];
} }
return $files; return $files;
} }

View File

@ -111,14 +111,14 @@ class Logger extends AbstractLogger
public static function getInstance(): Logger public static function getInstance(): Logger
{ {
if (!static::$instanse) { if (!self::$instanse) {
$settings = Config::getInstance()->get('telegram'); $settings = Config::getInstance()->get('telegram');
$loggerLevel = static::$madelineLevels[$settings['logger']['level']]; $loggerLevel = self::$madelineLevels[$settings['logger']['level']];
static::$instanse = new static($loggerLevel); self::$instanse = new static($loggerLevel);
} }
return static::$instanse; return self::$instanse;
} }
/** /**
@ -168,7 +168,7 @@ class Logger extends AbstractLogger
$replacements["{{$key}}"] = $val; $replacements["{{$key}}"] = $val;
} else { } else {
if ($val instanceof DateTimeInterface) { if ($val instanceof DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(static::$dateTimeFormat); $replacements["{{$key}}"] = $val->format(self::$dateTimeFormat);
} else { } else {
if (is_object($val)) { if (is_object($val)) {
$replacements["{{$key}}"] = '[object ' . get_class($val) . ']'; $replacements["{{$key}}"] = '[object ' . get_class($val) . ']';
@ -184,7 +184,7 @@ class Logger extends AbstractLogger
return sprintf( return sprintf(
'[%s] [%s] %s %s', '[%s] [%s] %s %s',
date(static::$dateTimeFormat), date(self::$dateTimeFormat),
$level, $level,
$message, $message,
$context ? $context ?

View File

@ -96,7 +96,7 @@ class ApiExtensions
$textFormated = sprintf($template, $text); $textFormated = sprintf($template, $text);
} }
$message = static::substringReplace($message, $textFormated, $entity['offset'], $entity['length']); $message = self::substringReplace($message, $textFormated, $entity['offset'], $entity['length']);
//Увеличим оффсеты всех следующих entity //Увеличим оффсеты всех следующих entity
foreach ($entities as $nextKey => &$nextEntity) { foreach ($entities as $nextKey => &$nextEntity) {
@ -167,7 +167,7 @@ class ApiExtensions
'peer' => $data['to_peer'], 'peer' => $data['to_peer'],
'entities' => $message['entities'] ?? [], 'entities' => $message['entities'] ?? [],
]; ];
if (static::hasMedia($message, false)) { if (self::hasMedia($message, false)) {
$messageData['media'] = $message; //MadelineProto сама достанет все media из сообщения. $messageData['media'] = $message; //MadelineProto сама достанет все media из сообщения.
$result[] = $this->madelineProto->messages->sendMedia(...$messageData); $result[] = $this->madelineProto->messages->sendMedia(...$messageData);
} else { } else {
@ -203,7 +203,7 @@ class ApiExtensions
throw new NoMediaException('Empty message'); throw new NoMediaException('Empty message');
} }
if (!static::hasMedia($message, true)) { if (!self::hasMedia($message, true)) {
throw new NoMediaException('Message has no media'); throw new NoMediaException('Message has no media');
} }
@ -246,7 +246,7 @@ class ApiExtensions
throw new NoMediaException('Empty message'); throw new NoMediaException('Empty message');
} }
if (!static::hasMedia($message, true)) { if (!self::hasMedia($message, true)) {
throw new NoMediaException('Message has no media'); throw new NoMediaException('Message has no media');
} }