You are here

public static function Utility::registerUniqueShutdownFunction in Helper 8

Registers a unique function call for execution on shutdown.

Wrapper for drupal_register_shutdown_function() that does not add the function call if it already exists in the shutdown function stack.

Parameters

callable $callback: The shutdown function to register.

...: Additional arguments to pass to the shutdown function.

Return value

bool TRUE if the function was added, or FALSE if it was already in the stack.

See also

drupal_register_shutdown_function()

File

src/Utility.php, line 26

Class

Utility
Provides various utility helpers.

Namespace

Drupal\helper

Code

public static function registerUniqueShutdownFunction(callable $callback = NULL) {
  $args = func_get_args();
  array_shift($args);
  $existing_callbacks = drupal_register_shutdown_function();
  foreach ($existing_callbacks as $existing_callback) {
    if ($existing_callback['callback'] === $callback && $existing_callback['arguments'] === $args) {
      return FALSE;
    }
  }
  array_unshift($args, $callback);
  call_user_func_array('drupal_register_shutdown_function', $args);
  return TRUE;
}