You are here

class WritableFileSystemValidator in Automatic Updates 8.2

Checks that the file system is writable.

Hierarchy

Expanded class hierarchy of WritableFileSystemValidator

1 string reference to 'WritableFileSystemValidator'
automatic_updates.services.yml in ./automatic_updates.services.yml
automatic_updates.services.yml
1 service uses WritableFileSystemValidator
automatic_updates.validator.file_system_permissions in ./automatic_updates.services.yml
Drupal\automatic_updates\Validator\WritableFileSystemValidator

File

src/Validator/WritableFileSystemValidator.php, line 16

Namespace

Drupal\automatic_updates\Validator
View source
class WritableFileSystemValidator implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The path locator service.
   *
   * @var \Drupal\automatic_updates\PathLocator
   */
  protected $pathLocator;

  /**
   * The Drupal root.
   *
   * @var string
   */
  protected $appRoot;

  /**
   * Constructs a WritableFileSystemValidator object.
   *
   * @param \Drupal\automatic_updates\PathLocator $path_locator
   *   The path locator service.
   * @param string $app_root
   *   The Drupal root.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The translation service.
   */
  public function __construct(PathLocator $path_locator, string $app_root, TranslationInterface $translation) {
    $this->pathLocator = $path_locator;
    $this->appRoot = $app_root;
    $this
      ->setStringTranslation($translation);
  }

  /**
   * Checks that the file system is writable.
   *
   * @param \Drupal\automatic_updates\Event\UpdateEvent $event
   *   The event object.
   *
   * @todo It might make sense to use a more sophisticated method of testing
   *   writability than is_writable(), since it's not clear if that can return
   *   false negatives/positives due to things like SELinux, exotic file
   *   systems, and so forth.
   */
  public function checkPermissions(UpdateEvent $event) : void {
    $messages = [];
    if (!is_writable($this->appRoot)) {
      $messages[] = $this
        ->t('The Drupal directory "@dir" is not writable.', [
        '@dir' => $this->appRoot,
      ]);
    }
    $dir = $this->pathLocator
      ->getVendorDirectory();
    if (!is_writable($dir)) {
      $messages[] = $this
        ->t('The vendor directory "@dir" is not writable.', [
        '@dir' => $dir,
      ]);
    }
    if ($messages) {
      $result = ValidationResult::createError($messages, $this
        ->t('The file system is not writable.'));
      $event
        ->addValidationResult($result);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      AutomaticUpdatesEvents::READINESS_CHECK => 'checkPermissions',
      AutomaticUpdatesEvents::PRE_START => 'checkPermissions',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
WritableFileSystemValidator::$appRoot protected property The Drupal root.
WritableFileSystemValidator::$pathLocator protected property The path locator service.
WritableFileSystemValidator::checkPermissions public function Checks that the file system is writable.
WritableFileSystemValidator::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
WritableFileSystemValidator::__construct public function Constructs a WritableFileSystemValidator object.