1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-12 06:38:20 +01:00
MadelineProto/src/Shutdown.php

121 lines
3.6 KiB
PHP
Raw Normal View History

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;
use ReflectionClass;
use Revolt\EventLoop;
2023-02-08 10:12:14 +01:00
use Revolt\EventLoop\Internal\AbstractDriver;
2023-01-15 16:12:12 +01:00
use function Amp\ByteStream\getStdin;
/**
* 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
{
$obj = EventLoop::getSuspension();
$reflection = new ReflectionClass($obj);
$reflection->getProperty('pending')->setValue($obj, false);
$obj = EventLoop::getDriver();
$reflection = new ReflectionClass(AbstractDriver::class);
if (!$reflection->getProperty('callbackFiber')->isInitialized($obj)
|| $reflection->getProperty('callbackFiber')->getValue($obj)->isTerminated()
) {
$reflection->getMethod('createCallbackFiber')->invoke($obj);
}
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();
if (\class_exists(Installer::class)) {
Installer::unlock();
}
}
/**
* Register shutdown function.
*/
public static function init(): void
{
if (!self::$registered) {
\register_shutdown_function(fn () => self::shutdown());
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.
*
2019-06-06 19:54:22 +02:00
* @param callable $callback The callback to set
* @param null|string $id The optional callback ID
2020-09-22 23:10:56 +02:00
* @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.
*
2020-09-22 23:10:56 +02:00
* @param null|string|int $id The optional callback ID
2019-06-06 19:54:22 +02:00
* @return bool true if the callback was removed correctly, false otherwise
*/
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
}