. * * @author Daniil Gentili * @copyright 2016-2020 Daniil Gentili * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 * * @link https://docs.madelineproto.xyz MadelineProto documentation */ namespace danog\MadelineProto; /** * Class that controls script shutdown. */ class Shutdown { /** * Callbacks to call on shutdown. * * @var array */ private static $callbacks = []; /** * Whether the main shutdown was registered. * * @var boolean */ private static $registered = false; /** * Incremental ID for new callback. * * @var integer */ private static $id = 0; /** * Function to be called on shutdown. * * @return void */ public static function shutdown() { foreach (self::$callbacks as $callback) { $callback(); } Magic::shutdown(0); } /** * Add a callback for script shutdown. * * @param callable $callback The callback to set * @param null|string $id The optional callback ID * * @return int|string The callback ID */ public static function addCallback($callback, $id = null) { if (!$id) { $id = self::$id++; } self::$callbacks[$id] = $callback; if (!self::$registered) { \register_shutdown_function([__CLASS__, 'shutdown']); self::$registered = true; } return $id; } /** * Remove a callback from the script shutdown callable list. * * @param null|string|int $id The optional callback ID * * @return bool true if the callback was removed correctly, false otherwise */ public static function removeCallback($id) { if (isset(self::$callbacks[$id])) { unset(self::$callbacks[$id]); return true; } return false; } }