2022-12-30 21:54:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2020-01-31 19:29:43 +01:00
|
|
|
|
2019-06-06 19:54:22 +02:00
|
|
|
/**
|
|
|
|
* Shutdown 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>
|
2023-01-04 12:43:01 +01:00
|
|
|
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
|
2019-06-06 19:54:22 +02:00
|
|
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
2019-10-31 15:07:35 +01:00
|
|
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
2019-06-06 19:54:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace danog\MadelineProto;
|
|
|
|
|
2023-01-15 16:12:12 +01:00
|
|
|
use function Amp\ByteStream\getStdin;
|
|
|
|
|
2020-10-07 19:31:52 +02:00
|
|
|
/**
|
|
|
|
* Class that controls script shutdown.
|
2019-06-06 19:54:22 +02:00
|
|
|
*/
|
2023-01-15 12:05:38 +01:00
|
|
|
final class Shutdown
|
2019-06-06 19:54:22 +02:00
|
|
|
{
|
|
|
|
/**
|
2019-09-02 17:08:36 +02:00
|
|
|
* Callbacks to call on shutdown.
|
2019-06-06 19:54:22 +02:00
|
|
|
*
|
|
|
|
* @var array<callable>
|
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
private static array $callbacks = [];
|
2019-06-06 19:54:22 +02:00
|
|
|
/**
|
2019-09-02 17:08:36 +02:00
|
|
|
* Whether the main shutdown was registered.
|
2019-06-06 19:54:22 +02:00
|
|
|
*
|
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
private static bool $registered = false;
|
2019-06-06 19:54:22 +02:00
|
|
|
/**
|
2019-09-02 17:08:36 +02:00
|
|
|
* Incremental ID for new callback.
|
2019-06-06 19:54:22 +02:00
|
|
|
*
|
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
private static int $id = 0;
|
2019-06-06 19:54:22 +02:00
|
|
|
/**
|
2019-09-02 17:08:36 +02:00
|
|
|
* Function to be called on shutdown.
|
2019-06-06 19:54:22 +02:00
|
|
|
*/
|
2022-12-08 20:16:40 +01:00
|
|
|
private static function shutdown(): void
|
2019-06-06 19:54:22 +02:00
|
|
|
{
|
|
|
|
foreach (self::$callbacks as $callback) {
|
|
|
|
$callback();
|
|
|
|
}
|
2021-04-09 17:21:37 +02:00
|
|
|
self::$callbacks = [];
|
2023-01-15 16:12:12 +01:00
|
|
|
|
|
|
|
if (\defined('STDIN')) {
|
|
|
|
getStdin()->unreference();
|
|
|
|
}
|
|
|
|
API::finalize();
|
|
|
|
MTProto::serializeAll();
|
2023-08-29 16:23:26 +02:00
|
|
|
Logger::finalize();
|
2023-10-01 20:05:04 +02:00
|
|
|
if (class_exists(Installer::class)) {
|
2023-01-15 16:12:12 +01:00
|
|
|
Installer::unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Register shutdown function.
|
|
|
|
*/
|
|
|
|
public static function init(): void
|
|
|
|
{
|
|
|
|
if (!self::$registered) {
|
2023-11-11 16:55:29 +01:00
|
|
|
register_shutdown_function(static fn () => self::shutdown());
|
2023-01-15 16:12:12 +01:00
|
|
|
self::$registered = true;
|
|
|
|
}
|
2019-06-06 19:54:22 +02:00
|
|
|
}
|
|
|
|
/**
|
2019-09-02 17:08:36 +02:00
|
|
|
* Add a callback for script shutdown.
|
|
|
|
*
|
2023-09-06 21:12:19 +02:00
|
|
|
* @param callable $callback The callback to set
|
|
|
|
* @param null|string $id The optional callback ID
|
|
|
|
* @return int|string The callback ID
|
2019-06-06 19:54:22 +02:00
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
public static function addCallback(callable $callback, ?string $id = null): int|string
|
2019-06-06 19:54:22 +02:00
|
|
|
{
|
|
|
|
if (!$id) {
|
|
|
|
$id = self::$id++;
|
|
|
|
}
|
|
|
|
self::$callbacks[$id] = $callback;
|
2023-01-15 16:12:12 +01:00
|
|
|
self::init();
|
2019-06-06 19:54:22 +02:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
/**
|
2019-09-02 17:08:36 +02:00
|
|
|
* Remove a callback from the script shutdown callable list.
|
|
|
|
*
|
2023-09-06 21:12:19 +02:00
|
|
|
* @param null|string|int $id The optional callback ID
|
|
|
|
* @return bool true if the callback was removed correctly, false otherwise
|
2019-06-06 19:54:22 +02:00
|
|
|
*/
|
2023-01-04 15:13:55 +01:00
|
|
|
public static function removeCallback(string|int|null $id): bool
|
2019-06-06 19:54:22 +02:00
|
|
|
{
|
|
|
|
if (isset(self::$callbacks[$id])) {
|
|
|
|
unset(self::$callbacks[$id]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-02 17:08:36 +02:00
|
|
|
}
|