mirror of
https://github.com/danog/TelegramApiServer.git
synced 2024-11-26 11:54:42 +01:00
Use self
This commit is contained in:
parent
2cf0f8b6e8
commit
41d37e28c5
@ -23,10 +23,10 @@ class Client
|
||||
|
||||
public static function getInstance(): Client
|
||||
{
|
||||
if (empty(static::$self)) {
|
||||
static::$self = new static();
|
||||
if (empty(self::$self)) {
|
||||
self::$self = new static();
|
||||
}
|
||||
return static::$self;
|
||||
return self::$self;
|
||||
}
|
||||
|
||||
public function connect(array $sessionFiles)
|
||||
|
@ -11,11 +11,11 @@ class Config
|
||||
|
||||
public static function getInstance(): Config
|
||||
{
|
||||
if (null === static::$instance) {
|
||||
static::$instance = new static();
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new static();
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,8 +12,8 @@ class EventHandler extends \danog\MadelineProto\EventHandler
|
||||
public function onStart()
|
||||
{
|
||||
$this->sessionName = Files::getSessionName($this->wrapper->getSession()->getSessionPath());
|
||||
if (empty(static::$instances[$this->sessionName])) {
|
||||
static::$instances[$this->sessionName] = true;
|
||||
if (empty(self::$instances[$this->sessionName])) {
|
||||
self::$instances[$this->sessionName] = true;
|
||||
warning("Event observer CONSTRUCTED: {$this->sessionName}");
|
||||
}
|
||||
}
|
||||
@ -23,7 +23,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler
|
||||
if (empty($this->sessionName)) {
|
||||
return;
|
||||
}
|
||||
unset(static::$instances[$this->sessionName]);
|
||||
unset(self::$instances[$this->sessionName]);
|
||||
warning("Event observer DESTRUCTED: {$this->sessionName}");
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ class EventObserver
|
||||
|
||||
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}");
|
||||
$callback($update, $sessionName);
|
||||
}
|
||||
@ -26,16 +26,16 @@ class EventObserver
|
||||
|
||||
private static function addSessionClient(string $session): void
|
||||
{
|
||||
if (empty(static::$sessionClients[$session])) {
|
||||
static::$sessionClients[$session] = 0;
|
||||
if (empty(self::$sessionClients[$session])) {
|
||||
self::$sessionClients[$session] = 0;
|
||||
}
|
||||
++static::$sessionClients[$session];
|
||||
++self::$sessionClients[$session];
|
||||
}
|
||||
|
||||
private static function removeSessionClient(string $session): void
|
||||
{
|
||||
if (!empty(static::$sessionClients[$session])) {
|
||||
--static::$sessionClients[$session];
|
||||
if (!empty(self::$sessionClients[$session])) {
|
||||
--self::$sessionClients[$session];
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,8 +49,8 @@ class EventObserver
|
||||
}
|
||||
|
||||
foreach ($sessions as $session) {
|
||||
static::addSessionClient($session);
|
||||
if (static::$sessionClients[$session] === 1) {
|
||||
self::addSessionClient($session);
|
||||
if (self::$sessionClients[$session] === 1) {
|
||||
warning("Start EventHandler: {$session}");
|
||||
try {
|
||||
$instance = Client::getInstance()->getSession($session);
|
||||
@ -60,7 +60,7 @@ class EventObserver
|
||||
EventHandler::cachePlugins(EventHandler::class);
|
||||
$wrapper->getAPI()->setEventHandler(EventHandler::class);
|
||||
} catch (Throwable $e) {
|
||||
static::removeSessionClient($session);
|
||||
self::removeSessionClient($session);
|
||||
error('Cant set EventHandler', [
|
||||
'session' => $session,
|
||||
'exception' => Logger::getExceptionAsArray($e),
|
||||
@ -79,11 +79,11 @@ class EventObserver
|
||||
$sessions[] = $requestedSession;
|
||||
}
|
||||
foreach ($sessions as $session) {
|
||||
static::removeSessionClient($session);
|
||||
if (empty(static::$sessionClients[$session]) || $force) {
|
||||
self::removeSessionClient($session);
|
||||
if (empty(self::$sessionClients[$session]) || $force) {
|
||||
warning("Stopping EventHandler: {$session}");
|
||||
Client::getInstance()->instances[$session]->unsetEventHandler();
|
||||
unset(EventHandler::$instances[$session], static::$sessionClients[$session]);
|
||||
unset(EventHandler::$instances[$session], self::$sessionClients[$session]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ class LogObserver
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -11,17 +11,17 @@ trait ObserverTrait
|
||||
public static function addSubscriber($clientId, callable $callback): void
|
||||
{
|
||||
notice("Add event listener. ClientId: {$clientId}");
|
||||
static::$subscribers[$clientId] = $callback;
|
||||
self::$subscribers[$clientId] = $callback;
|
||||
}
|
||||
|
||||
public static function removeSubscriber($clientId): void
|
||||
{
|
||||
notice("Removing listener: {$clientId}");
|
||||
unset(static::$subscribers[$clientId]);
|
||||
$listenersCount = count(static::$subscribers);
|
||||
unset(self::$subscribers[$clientId]);
|
||||
$listenersCount = count(self::$subscribers);
|
||||
notice("Event listeners left: {$listenersCount}");
|
||||
if ($listenersCount === 0) {
|
||||
static::$subscribers = [];
|
||||
self::$subscribers = [];
|
||||
}
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ class Files
|
||||
}
|
||||
|
||||
preg_match(
|
||||
'~' . static::SESSION_FOLDER . "/(?'sessionName'.*?)" . static::SESSION_EXTENSION . '~',
|
||||
'~' . self::SESSION_FOLDER . "/(?'sessionName'.*?)" . self::SESSION_EXTENSION . '~',
|
||||
$sessionFile,
|
||||
$matches
|
||||
);
|
||||
@ -50,14 +50,14 @@ class Files
|
||||
return null;
|
||||
}
|
||||
$session = trim(trim($session), '/');
|
||||
$session = static::SESSION_FOLDER . '/' . $session . $extension;
|
||||
$session = self::SESSION_FOLDER . '/' . $session . $extension;
|
||||
$session = str_replace('//', '/', $session);
|
||||
return $session;
|
||||
}
|
||||
|
||||
public static function getSessionSettings(string $session): array
|
||||
{
|
||||
$settingsFile = static::getSessionFile($session, static::SETTINGS_EXTENSION);
|
||||
$settingsFile = self::getSessionFile($session, self::SETTINGS_EXTENSION);
|
||||
$settings = [];
|
||||
if (file_exists($settingsFile)) {
|
||||
$settings = json_decode(
|
||||
@ -73,7 +73,7 @@ class Files
|
||||
|
||||
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(
|
||||
$settingsFile,
|
||||
json_encode(
|
||||
@ -91,7 +91,7 @@ class Files
|
||||
{
|
||||
$files = glob($pattern, $flags) ?: [];
|
||||
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;
|
||||
}
|
||||
|
@ -111,14 +111,14 @@ class Logger extends AbstractLogger
|
||||
|
||||
public static function getInstance(): Logger
|
||||
{
|
||||
if (!static::$instanse) {
|
||||
if (!self::$instanse) {
|
||||
$settings = Config::getInstance()->get('telegram');
|
||||
|
||||
$loggerLevel = static::$madelineLevels[$settings['logger']['level']];
|
||||
static::$instanse = new static($loggerLevel);
|
||||
$loggerLevel = self::$madelineLevels[$settings['logger']['level']];
|
||||
self::$instanse = new static($loggerLevel);
|
||||
}
|
||||
|
||||
return static::$instanse;
|
||||
return self::$instanse;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -168,7 +168,7 @@ class Logger extends AbstractLogger
|
||||
$replacements["{{$key}}"] = $val;
|
||||
} else {
|
||||
if ($val instanceof DateTimeInterface) {
|
||||
$replacements["{{$key}}"] = $val->format(static::$dateTimeFormat);
|
||||
$replacements["{{$key}}"] = $val->format(self::$dateTimeFormat);
|
||||
} else {
|
||||
if (is_object($val)) {
|
||||
$replacements["{{$key}}"] = '[object ' . get_class($val) . ']';
|
||||
@ -184,7 +184,7 @@ class Logger extends AbstractLogger
|
||||
|
||||
return sprintf(
|
||||
'[%s] [%s] %s %s',
|
||||
date(static::$dateTimeFormat),
|
||||
date(self::$dateTimeFormat),
|
||||
$level,
|
||||
$message,
|
||||
$context ?
|
||||
|
@ -96,7 +96,7 @@ class ApiExtensions
|
||||
$textFormated = sprintf($template, $text);
|
||||
}
|
||||
|
||||
$message = static::substringReplace($message, $textFormated, $entity['offset'], $entity['length']);
|
||||
$message = self::substringReplace($message, $textFormated, $entity['offset'], $entity['length']);
|
||||
|
||||
//Увеличим оффсеты всех следующих entity
|
||||
foreach ($entities as $nextKey => &$nextEntity) {
|
||||
@ -167,7 +167,7 @@ class ApiExtensions
|
||||
'peer' => $data['to_peer'],
|
||||
'entities' => $message['entities'] ?? [],
|
||||
];
|
||||
if (static::hasMedia($message, false)) {
|
||||
if (self::hasMedia($message, false)) {
|
||||
$messageData['media'] = $message; //MadelineProto сама достанет все media из сообщения.
|
||||
$result[] = $this->madelineProto->messages->sendMedia(...$messageData);
|
||||
} else {
|
||||
@ -203,7 +203,7 @@ class ApiExtensions
|
||||
throw new NoMediaException('Empty message');
|
||||
}
|
||||
|
||||
if (!static::hasMedia($message, true)) {
|
||||
if (!self::hasMedia($message, true)) {
|
||||
throw new NoMediaException('Message has no media');
|
||||
}
|
||||
|
||||
@ -246,7 +246,7 @@ class ApiExtensions
|
||||
throw new NoMediaException('Empty message');
|
||||
}
|
||||
|
||||
if (!static::hasMedia($message, true)) {
|
||||
if (!self::hasMedia($message, true)) {
|
||||
throw new NoMediaException('Message has no media');
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user