class PhpExtensionsValidator

Same name and namespace in other branches
  1. 11.x core/modules/package_manager/src/Validator/PhpExtensionsValidator.php \Drupal\package_manager\Validator\PhpExtensionsValidator

Performs validation if certain PHP extensions are enabled.

@internal This is an internal part of Package Manager and may be changed or removed at any time without warning. External code should not interact with this class.

Hierarchy

  • class \Drupal\package_manager\Validator\PhpExtensionsValidator implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses \Drupal\Core\StringTranslation\StringTranslationTrait

Expanded class hierarchy of PhpExtensionsValidator

1 file declares its use of PhpExtensionsValidator
PhpExtensionsValidatorTest.php in core/modules/package_manager/tests/src/Kernel/PhpExtensionsValidatorTest.php

File

core/modules/package_manager/src/Validator/PhpExtensionsValidator.php, line 23

Namespace

Drupal\package_manager\Validator
View source
class PhpExtensionsValidator implements EventSubscriberInterface {
  use StringTranslationTrait;
  
  /**
   * Indicates if a particular PHP extension is loaded.
   *
   * @param string $name
   *   The name of the PHP extension to check for.
   *
   * @return bool
   *   TRUE if the given extension is loaded, FALSE otherwise.
   */
  final protected function isExtensionLoaded(string $name) : bool {
    // If and ONLY if we're currently running a test, allow the list of loaded
    // extensions to be overridden by a state variable.
    if (self::insideTest()) {
      // By default, assume OpenSSL is enabled and Xdebug isn't. This allows us
      // to run tests in environments that we might not support in production,
      // such as a configured CI environment.
      $loaded_extensions = \Drupal::state()->get('package_manager_loaded_php_extensions', [
        'openssl',
      ]);
      return in_array($name, $loaded_extensions, TRUE);
    }
    return extension_loaded($name);
  }
  
  /**
   * Flags a warning if Xdebug is enabled.
   *
   * @param \Drupal\package_manager\Event\StatusCheckEvent $event
   *   The event object.
   */
  public function validateXdebug(StatusCheckEvent $event) : void {
    if ($this->isExtensionLoaded('xdebug')) {
      $event->addWarning([
        $this->t('Xdebug is enabled, which may have a negative performance impact on Package Manager and any modules that use it.'),
      ]);
    }
  }
  
  /**
   * Flags an error if the OpenSSL extension is not installed.
   *
   * @param \Drupal\package_manager\Event\SandboxValidationEvent $event
   *   The event object.
   */
  public function validateOpenSsl(SandboxValidationEvent $event) : void {
    if (!$this->isExtensionLoaded('openssl')) {
      $message = $this->t('The OpenSSL extension is not enabled, which is a security risk. See <a href=":url">the PHP documentation</a> for information on how to enable this extension.', [
        ':url' => 'https://www.php.net/manual/en/openssl.installation.php',
      ]);
      $event->addError([
        $message,
      ]);
    }
  }
  
  /**
   * Whether this validator is running inside a test.
   *
   * @return bool
   *   TRUE if the validator is running in a test. FALSE otherwise.
   */
  private static function insideTest() : bool {
    // @see \Drupal\Core\CoreServiceProvider::registerTest()
    $in_functional_test = drupal_valid_test_ua();
    // @see \Drupal\Core\DependencyInjection\DependencySerializationTrait::__wakeup()
    $in_kernel_test = isset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
    // @see \Drupal\BuildTests\Framework\BuildTestBase::setUp()
    $in_build_test = str_contains(__FILE__, DrupalFilesystem::getOsTemporaryDirectory() . '/build_workspace_');
    return $in_functional_test || $in_kernel_test || $in_build_test;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    return [
      StatusCheckEvent::class => [
        [
          'validateXdebug',
        ],
        [
          'validateOpenSsl',
        ],
      ],
      PreCreateEvent::class => [
        'validateOpenSsl',
      ],
      PreApplyEvent::class => [
        'validateOpenSsl',
      ],
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
PhpExtensionsValidator::getSubscribedEvents public static function
PhpExtensionsValidator::insideTest private static function Whether this validator is running inside a test.
PhpExtensionsValidator::isExtensionLoaded final protected function Indicates if a particular PHP extension is loaded.
PhpExtensionsValidator::validateOpenSsl public function Flags an error if the OpenSSL extension is not installed.
PhpExtensionsValidator::validateXdebug public function Flags a warning if Xdebug is enabled.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

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