From e296836df5db2041406eebfe32bc3849517d804b Mon Sep 17 00:00:00 2001 From: Alexander Pankratov Date: Sat, 28 Mar 2020 13:44:14 +0300 Subject: [PATCH] Recursive session search --- server.php | 5 +- src/Client.php | 52 ++-------------- src/EventObservers/EventHandler.php | 4 +- src/Files.php | 62 +++++++++++++++++++ .../SystemApiExtensions.php | 5 +- src/Migrations/SessionsMigration.php | 10 +-- 6 files changed, 79 insertions(+), 59 deletions(-) create mode 100644 src/Files.php diff --git a/server.php b/server.php index 46edbb2..202569d 100644 --- a/server.php +++ b/server.php @@ -1,5 +1,6 @@ API->authorized ?? MTProto::NOT_LOGGED_IN) === MTProto::LOGGED_IN; @@ -71,7 +27,7 @@ class Client warning(PHP_EOL . 'Starting MadelineProto...' . PHP_EOL); foreach ($sessionFiles as $file) { - $sessionName = static::getSessionName($file); + $sessionName = Files::getSessionName($file); $instance = $this->addSession($sessionName); $this->runSession($instance); } @@ -90,8 +46,8 @@ class Client if (isset($this->instances[$session])) { throw new InvalidArgumentException('Session already exists'); } - $file = static::getSessionFile($session); - static::checkOrCreateSessionFolder($file); + $file = Files::getSessionFile($session); + Files::checkOrCreateSessionFolder($file); $settings = array_replace_recursive((array) Config::getInstance()->get('telegram'), $settings); $instance = new MadelineProto\API($file, $settings); $instance->async(true); @@ -190,7 +146,7 @@ class Client private function loop(MadelineProto\API $instance, callable $callback = null): void { - $sessionName = static::getSessionName($instance->session); + $sessionName = Files::getSessionName($instance->session); try { $callback ? $instance->loop($callback) : $instance->loop(); } catch (\Throwable $e) { diff --git a/src/EventObservers/EventHandler.php b/src/EventObservers/EventHandler.php index 999e4a1..f07bca2 100644 --- a/src/EventObservers/EventHandler.php +++ b/src/EventObservers/EventHandler.php @@ -3,7 +3,7 @@ namespace TelegramApiServer\EventObservers; use danog\MadelineProto\APIWrapper; -use TelegramApiServer\Client; +use TelegramApiServer\Files; class EventHandler extends \danog\MadelineProto\EventHandler { @@ -12,7 +12,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler public function __construct(APIWrapper $MadelineProto) { - $this->sessionName = Client::getSessionName($MadelineProto->session); + $this->sessionName = Files::getSessionName($MadelineProto->session); if (empty(static::$instances[$this->sessionName])) { static::$instances[$this->sessionName] = true; parent::__construct($MadelineProto); diff --git a/src/Files.php b/src/Files.php new file mode 100644 index 0000000..532020b --- /dev/null +++ b/src/Files.php @@ -0,0 +1,62 @@ + $session, - 'file' => Client::getSessionFile($session), + 'file' => Files::getSessionFile($session), 'status' => $status, ]; } @@ -74,7 +75,7 @@ class SystemApiExtensions public function removeSessionFile($session) { return call(static function() use($session) { - $file = Client::getSessionFile($session); + $file = Files::getSessionFile($session); if (is_file($file)) { yield \Amp\File\unlink($file); yield \Amp\File\unlink($file . '.lock'); diff --git a/src/Migrations/SessionsMigration.php b/src/Migrations/SessionsMigration.php index b2fb43a..fc9b265 100644 --- a/src/Migrations/SessionsMigration.php +++ b/src/Migrations/SessionsMigration.php @@ -2,22 +2,22 @@ namespace TelegramApiServer\Migrations; -use TelegramApiServer\Client; +use TelegramApiServer\Files; class SessionsMigration { public static function move($rootDir = ROOT_DIR) { - foreach (glob("$rootDir/*" . Client::$sessionExtension) as $oldFile) { + foreach (glob("$rootDir/*" . Files::$sessionExtension) as $oldFile) { preg_match( - '~^' . "{$rootDir}(?'session'.*)" . preg_quote(Client::$sessionExtension, '\\') . '$~', + '~^' . "{$rootDir}(?'session'.*)" . preg_quote(Files::$sessionExtension, '\\') . '$~', $oldFile, $matches ); if ($session = $matches['session'] ?? null) { - $session = Client::getSessionFile($session); - Client::checkOrCreateSessionFolder($session); + $session = Files::getSessionFile($session); + Files::checkOrCreateSessionFolder($session); rename($oldFile, "{$rootDir}/{$session}"); rename("{$oldFile}.lock", "{$rootDir}/{$session}.lock");