You are here

class ReadOnlyFilesystem in Automatic Updates 8

Read only filesystem checker.

Hierarchy

Expanded class hierarchy of ReadOnlyFilesystem

1 file declares its use of ReadOnlyFilesystem
ReadOnlyFilesystemTest.php in tests/src/Kernel/ReadinessChecker/ReadOnlyFilesystemTest.php
1 string reference to 'ReadOnlyFilesystem'
automatic_updates.services.yml in ./automatic_updates.services.yml
automatic_updates.services.yml
1 service uses ReadOnlyFilesystem
automatic_updates.readonly_checker in ./automatic_updates.services.yml
Drupal\automatic_updates\ReadinessChecker\ReadOnlyFilesystem

File

src/ReadinessChecker/ReadOnlyFilesystem.php, line 13

Namespace

Drupal\automatic_updates\ReadinessChecker
View source
class ReadOnlyFilesystem extends Filesystem {

  /**
   * The logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The file system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * ReadOnlyFilesystem constructor.
   *
   * @param string $app_root
   *   The app root.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   */
  public function __construct($app_root, LoggerInterface $logger, FileSystemInterface $file_system) {
    parent::__construct($app_root);
    $this->logger = $logger;
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  protected function doCheck() {
    return $this
      ->readOnlyCheck();
  }

  /**
   * Check if the filesystem is read only.
   *
   * @return array
   *   An array of translatable strings if any checks fail.
   */
  protected function readOnlyCheck() {
    $messages = [];
    if ($this
      ->areSameLogicalDisk($this
      ->getRootPath(), $this
      ->getVendorPath())) {
      $error = $this
        ->t('Logical disk at "@path" is read only. Updates to Drupal cannot be applied against a read only file system.', [
        '@path' => $this->rootPath,
      ]);
      $this
        ->doReadOnlyCheck($this
        ->getRootPath(), 'core/core.api.php', $messages, $error);
    }
    else {
      $error = $this
        ->t('Drupal core filesystem at "@path" is read only. Updates to Drupal core cannot be applied against a read only file system.', [
        '@path' => $this->rootPath . '/core',
      ]);
      $this
        ->doReadOnlyCheck($this
        ->getRootPath(), implode(DIRECTORY_SEPARATOR, [
        'core',
        'core.api.php',
      ]), $messages, $error);
      $error = $this
        ->t('Vendor filesystem at "@path" is read only. Updates to the site\'s code base cannot be applied against a read only file system.', [
        '@path' => $this->vendorPath,
      ]);
      $this
        ->doReadOnlyCheck($this
        ->getVendorPath(), 'composer/LICENSE', $messages, $error);
    }
    return $messages;
  }

  /**
   * Do the read only check.
   *
   * @param string $file_path
   *   The filesystem to test.
   * @param string $file
   *   The file path.
   * @param array $messages
   *   The messages array of translatable strings.
   * @param \Drupal\Component\Render\MarkupInterface $message
   *   The error message to add if the file is read only.
   */
  protected function doReadOnlyCheck($file_path, $file, array &$messages, MarkupInterface $message) {

    // Ignore check if the path doesn't exit.
    if (!is_file($file_path . DIRECTORY_SEPARATOR . $file)) {
      return;
    }
    try {

      // If we can copy and delete a file, then we don't have a read only
      // file system.
      if ($this->fileSystem
        ->copy($file_path . DIRECTORY_SEPARATOR . $file, $file_path . DIRECTORY_SEPARATOR . "{$file}.automatic_updates", FileSystemInterface::EXISTS_REPLACE)) {

        // Delete it after copying.
        $this->fileSystem
          ->delete($file_path . DIRECTORY_SEPARATOR . "{$file}.automatic_updates");
      }
      else {
        $this->logger
          ->error($message);
        $messages[] = $message;
      }
    } catch (FileException $exception) {
      $messages[] = $message;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Filesystem::$rootPath protected property The root file path.
Filesystem::$vendorPath protected property The vendor file path.
Filesystem::areSameLogicalDisk protected function Determine if the root and vendor file system are the same logical disk. 2
Filesystem::getRootPath protected function Get the root file path.
Filesystem::getVendorPath protected function Get the vendor file path.
Filesystem::run public function Run check. Overrides ReadinessCheckerInterface::run
ReadOnlyFilesystem::$fileSystem protected property The file system service.
ReadOnlyFilesystem::$logger protected property The logger.
ReadOnlyFilesystem::doCheck protected function Perform checks. Overrides Filesystem::doCheck
ReadOnlyFilesystem::doReadOnlyCheck protected function Do the read only check.
ReadOnlyFilesystem::readOnlyCheck protected function Check if the filesystem is read only.
ReadOnlyFilesystem::__construct public function ReadOnlyFilesystem constructor. Overrides Filesystem::__construct
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.