Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ShutdownHandler.php

class ShutdownHandler

Hierarchy

  • class \OpenTelemetry\SDK\Common\Util\ShutdownHandler

Expanded class hierarchy of ShutdownHandler

3 files declare their use of ShutdownHandler
AutoRootSpan.php in vendor/open-telemetry/sdk/Trace/AutoRootSpan.php
SdkAutoloader.php in vendor/open-telemetry/sdk/SdkAutoloader.php
SdkBuilder.php in vendor/open-telemetry/sdk/SdkBuilder.php

File

vendor/open-telemetry/sdk/Common/Util/ShutdownHandler.php, line 12

Namespace

OpenTelemetry\SDK\Common\Util
View source
final class ShutdownHandler {
    
    /** @var array<int, Closure>|null */
    private static ?array $handlers = null;
    
    /** @var WeakMap<object, self>|null  */
    private static ?WeakMap $weakMap = null;
    private array $ids = [];
    private function __construct() {
    }
    public function __destruct() {
        if (!self::$handlers) {
            return;
        }
        foreach ($this->ids as $id) {
            unset(self::$handlers[$id]);
        }
    }
    
    /**
     * Registers a function that will be executed on shutdown.
     *
     * If the given function is bound to an object, then the function will only
     * be executed if the bound object is still referenced on shutdown handler
     * invocation.
     *
     * ```php
     * ShutdownHandler::register([$tracerProvider, 'shutdown']);
     * ```
     *
     * @param callable $shutdownFunction function to register
     *
     * @see register_shutdown_function
     */
    public static function register(callable $shutdownFunction) : void {
        self::registerShutdownFunction();
        self::$handlers[] = weaken(closure($shutdownFunction), $target);
        if (!($object = $target)) {
            return;
        }
        self::$weakMap ??= new WeakMap();
        $handler = self::$weakMap[$object] ??= new self();
        $handler->ids[] = array_key_last(self::$handlers);
    }
    private static function registerShutdownFunction() : void {
        if (self::$handlers === null) {
            register_shutdown_function(static function () : void {
                $handlers = self::$handlers;
                self::$handlers = null;
                self::$weakMap = null;
                // Push shutdown to end of queue
                // @phan-suppress-next-line PhanTypeMismatchArgumentInternal
                register_shutdown_function(static function (array $handlers) : void {
                    foreach (array_reverse($handlers) as $handler) {
                        $handler();
                    }
                }, $handlers);
            });
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
ShutdownHandler::$handlers private static property @var array&lt;int, Closure&gt;|null
ShutdownHandler::$ids private property
ShutdownHandler::$weakMap private static property @var WeakMap&lt;object, self&gt;|null
ShutdownHandler::register public static function Registers a function that will be executed on shutdown.
ShutdownHandler::registerShutdownFunction private static function
ShutdownHandler::__construct private function
ShutdownHandler::__destruct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal