class WritableFileSystemValidator

Same name and namespace in other branches
  1. main core/modules/package_manager/src/Validator/WritableFileSystemValidator.php \Drupal\package_manager\Validator\WritableFileSystemValidator

Checks that the file system is writable.

@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\WritableFileSystemValidator implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses \Drupal\package_manager\Validator\BaseRequirementValidatorTrait, \Drupal\Core\StringTranslation\StringTranslationTrait

Expanded class hierarchy of WritableFileSystemValidator

2 files declare their use of WritableFileSystemValidator
SandboxManagerBaseTest.php in core/modules/package_manager/tests/src/Kernel/SandboxManagerBaseTest.php
WritableFileSystemValidatorTest.php in core/modules/package_manager/tests/src/Kernel/WritableFileSystemValidatorTest.php

File

core/modules/package_manager/src/Validator/WritableFileSystemValidator.php, line 21

Namespace

Drupal\package_manager\Validator
View source
class WritableFileSystemValidator implements EventSubscriberInterface {
  use BaseRequirementValidatorTrait;
  use StringTranslationTrait;
  public function __construct(private readonly PathLocator $pathLocator) {
  }
  
  /**
   * Checks that the file system is writable.
   *
   * @todo Determine if 'is_writable()' is a sufficiently robust test across
   *   different operating systems in https://drupal.org/i/3348253.
   */
  public function validate(SandboxValidationEvent $event) : void {
    $messages = [];
    $project_root = $this->pathLocator
      ->getProjectRoot();
    // If the web (Drupal) root and project root are different, validate the
    // web root separately.
    $web_root = $this->pathLocator
      ->getWebRoot();
    if ($web_root) {
      $drupal_root = $project_root . DIRECTORY_SEPARATOR . $web_root;
      if (!is_writable($drupal_root)) {
        $messages[] = $this->t('The Drupal directory "@dir" is not writable.', [
          '@dir' => $drupal_root,
        ]);
      }
    }
    if (!is_writable($project_root)) {
      $messages[] = $this->t('The project root directory "@dir" is not writable.', [
        '@dir' => $project_root,
      ]);
    }
    $dir = $this->pathLocator
      ->getVendorDirectory();
    if (!is_writable($dir)) {
      $messages[] = $this->t('The vendor directory "@dir" is not writable.', [
        '@dir' => $dir,
      ]);
    }
    // During pre-apply don't check whether the staging root is writable.
    if ($event instanceof PreApplyEvent) {
      if ($messages) {
        $event->addError($messages, $this->t('The file system is not writable.'));
      }
      return;
    }
    // Ensure the staging root is writable. If it doesn't exist, ensure we will
    // be able to create it.
    $dir = $this->pathLocator
      ->getStagingRoot();
    if (!file_exists($dir)) {
      $dir = dirname($dir);
      if (!is_writable($dir)) {
        $messages[] = $this->t('The stage root directory will not able to be created at "@dir".', [
          '@dir' => $dir,
        ]);
      }
    }
    elseif (!is_writable($dir)) {
      $messages[] = $this->t('The stage root directory "@dir" is not writable.', [
        '@dir' => $dir,
      ]);
    }
    if ($messages) {
      $event->addError($messages, $this->t('The file system is not writable.'));
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BaseRequirementValidatorTrait::getSubscribedEvents public static function Implements EventSubscriberInterface::getSubscribedEvents().
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
WritableFileSystemValidator::validate public function Checks that the file system is writable. Overrides BaseRequirementValidatorTrait::validate
WritableFileSystemValidator::__construct public function

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