drupal_register_shutdown_function example

/** * A simple page callback that uses a CacheableResponse object. */
  public function respondWithCacheableResponse(Request $request) {
    return new CacheableResponse('test');
  }

  /** * A simple page callback which adds a register shutdown function. */
  public function shutdownFunctions($arg1$arg2) {
    drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1$arg2);
    // If using PHP-FPM then fastcgi_finish_request() will have been fired     // preventing further output to the browser which means that the escaping of     // the exception message can not be tested.     // @see _drupal_shutdown_function()     // @see \Drupal\system\Tests\System\ShutdownFunctionsTest     if (function_exists('fastcgi_finish_request')) {
      return ['#markup' => 'The function fastcgi_finish_request exists when serving the request.'];
    }
    return [];
  }

  


    // Ensure that internal logged in variable is reset.     $this->loggedInUser = FALSE;

    if ($this->mink) {
      $this->mink->stopSessions();
    }

    // Restore original shutdown callbacks.     if (function_exists('drupal_register_shutdown_function')) {
      $callbacks = &drupal_register_shutdown_function();
      $callbacks = $this->originalShutdownCallbacks;
    }
  }

  /** * Returns Mink session. * * @param string $name * (optional) Name of the session. Defaults to the active session. * * @return \Behat\Mink\Session * The active Mink session object. */

  protected $shutDownTwoCalled = FALSE;

  /** * Tests that shutdown functions can be added by other shutdown functions. */
  public function testShutdownFunctionInShutdownFunction() {
    // Ensure there are no shutdown functions registered before starting the     // test.     $this->assertEmpty(drupal_register_shutdown_function());
    // Register a shutdown function that, when called, will register another     // shutdown function.     drupal_register_shutdown_function([$this, 'shutdownOne']);
    $this->assertCount(1, drupal_register_shutdown_function());

    // Simulate the Drupal shutdown.     _drupal_shutdown_function();

    // Test that the expected functions are called.     $this->assertTrue($this->shutDownOneCalled);
    $this->assertTrue($this->shutDownTwoCalled);
    
protected function setUp(): void {
    // Initialize static variable prior to testing.     self::$shutdownCalled = [];
    parent::setUp();
  }

  /** * @covers ::assertPostConditions */
  public function testShutdownFunction() {
    $this->expectedShutdownCalled = ['shutdownFunction', 'shutdownFunction2'];
    drupal_register_shutdown_function([$this, 'shutdownFunction']);
  }

  /** * @covers ::assertPostConditions */
  public function testNoShutdownFunction() {
    $this->expectedShutdownCalled = [];
  }

  /** * Registers that this shutdown function has been called. */
protected $database;

  /** * Constructs a new DatabaseLockBackend. * * @param \Drupal\Core\Database\Connection $database * The database connection. */
  public function __construct(Connection $database) {
    // __destruct() is causing problems with garbage collections, register a     // shutdown function instead.     drupal_register_shutdown_function([$this, 'releaseAll']);
    $this->database = $database;
  }

  /** * {@inheritdoc} */
  public function acquire($name$timeout = 30.0) {
    $name = $this->normalizeName($name);

    // Insure that the timeout is at least 1 ms.     $timeout = max($timeout, 0.001);
    

    // Filter out any duplicates.     return array_unique($exceptions);
  }

  /** * {@inheritdoc} */
  protected function assertPostConditions(): void {
    // Execute registered Drupal shutdown functions prior to tearing down.     // @see _drupal_shutdown_function()     $callbacks = &drupal_register_shutdown_function();
    while ($callback = array_shift($callbacks)) {
      call_user_func_array($callback['callback']$callback['arguments']);
    }

    // Shut down the kernel (if bootKernel() was called).     // @see \Drupal\KernelTests\Core\DrupalKernel\DrupalKernelTest     if ($this->container) {
      $this->container->get('kernel')->shutdown();
    }

    parent::assertPostConditions();
  }
    new Settings([
      // For performance, simply use the database prefix as hash salt.       'hash_salt' => $this->databasePrefix,
    ]);

    Environment::setTimeLimit($this->timeLimit);

    // Save and clean the shutdown callbacks array because it is static cached     // and will be changed by the test run. Otherwise it will contain callbacks     // from both environments and the testing environment will try to call the     // handlers defined by the original one.     $callbacks = &drupal_register_shutdown_function();
    $this->originalShutdownCallbacks = $callbacks;
    $callbacks = [];
  }

  /** * Returns all supported database driver installer objects. * * This wraps drupal_get_database_types() for use without a current container. * * @return \Drupal\Core\Database\Install\Tasks[] * An array of available database driver installer objects. */
Home | Imprint | This part of the site doesn't use cookies.