trait ComposerStagerExceptionTrait

Trait to make Composer Stager throw pre-determined exceptions in tests.

@internal

Hierarchy

File

core/modules/package_manager/tests/modules/package_manager_bypass/src/ComposerStagerExceptionTrait.php, line 12

Namespace

Drupal\package_manager_bypass
View source
trait ComposerStagerExceptionTrait {
  
  /**
   * Sets an exception to be thrown.
   *
   * @param string|null $class
   *   The class of exception to throw, or NULL to delete a stored exception.
   * @param mixed ...$arguments
   *   Arguments to pass to the exception constructor.
   */
  public static function setException(?string $class = \Exception::class, mixed ...$arguments) : void {
    if ($class) {
      \Drupal::state()->set(static::class . '-exception', func_get_args());
    }
    else {
      \Drupal::state()->delete(static::class . '-exception');
    }
  }
  
  /**
   * Throws the exception if set.
   */
  private function throwExceptionIfSet() : void {
    if ($exception = $this->state
      ->get(static::class . '-exception')) {
      $class = array_shift($exception);
      throw new $class(...$exception);
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary
ComposerStagerExceptionTrait::setException public static function Sets an exception to be thrown.
ComposerStagerExceptionTrait::throwExceptionIfSet private function Throws the exception if set.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.