You are here

class Local in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/FileTransfer/Local.php \Drupal\Core\FileTransfer\Local

Defines the local connection class for copying files as the httpd user.

Hierarchy

Expanded class hierarchy of Local

3 files declare their use of Local
TestFileTransferWithSettingsForm.php in core/modules/update/tests/modules/update_test/src/TestFileTransferWithSettingsForm.php
UpdateManagerInstall.php in core/modules/update/src/Form/UpdateManagerInstall.php
UpdateReady.php in core/modules/update/src/Form/UpdateReady.php

File

core/lib/Drupal/Core/FileTransfer/Local.php, line 11

Namespace

Drupal\Core\FileTransfer
View source
class Local extends FileTransfer implements ChmodInterface {
  use DependencySerializationTrait;

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

  /**
   * {@inheritdoc}
   */
  public function __construct($jail, FileSystemInterface $file_system = NULL) {
    parent::__construct($jail);
    if (!isset($file_system)) {
      @trigger_error('The $file_system parameter was added in Drupal 8.8.0 and will be required in 9.0.0. See https://www.drupal.org/node/3021434.', E_USER_DEPRECATED);
      $file_system = \Drupal::service('file_system');
    }
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public function connect() {

    // No-op
  }

  /**
   * {@inheritdoc}
   */
  public static function factory($jail, $settings) {
    return new Local($jail, \Drupal::service('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  protected function copyFileJailed($source, $destination) {
    if (@(!copy($source, $destination))) {
      throw new FileTransferException('Cannot copy %source to %destination.', NULL, [
        '%source' => $source,
        '%destination' => $destination,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function createDirectoryJailed($directory) {
    if (!is_dir($directory) && @(!mkdir($directory, 0777, TRUE))) {
      throw new FileTransferException('Cannot create directory %directory.', NULL, [
        '%directory' => $directory,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function removeDirectoryJailed($directory) {
    if (!is_dir($directory)) {

      // Programmer error assertion, not something we expect users to see.
      throw new FileTransferException('removeDirectoryJailed() called with a path (%directory) that is not a directory.', NULL, [
        '%directory' => $directory,
      ]);
    }

    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $filename => $file) {
      if ($file
        ->isDir()) {
        if (@(!$file_system
          ->rmdir($filename))) {
          throw new FileTransferException('Cannot remove directory %directory.', NULL, [
            '%directory' => $filename,
          ]);
        }
      }
      elseif ($file
        ->isFile()) {
        if (@(!$this->fileSystem
          ->unlink($filename))) {
          throw new FileTransferException('Cannot remove file %file.', NULL, [
            '%file' => $filename,
          ]);
        }
      }
    }
    if (@(!$file_system
      ->rmdir($directory))) {
      throw new FileTransferException('Cannot remove directory %directory.', NULL, [
        '%directory' => $directory,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function removeFileJailed($file) {
    if (@(!$this->fileSystem
      ->unlink($file))) {
      throw new FileTransferException('Cannot remove file %file.', NULL, [
        '%file' => $file,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isDirectory($path) {
    return is_dir($path);
  }

  /**
   * {@inheritdoc}
   */
  public function isFile($path) {
    return is_file($path);
  }

  /**
   * {@inheritdoc}
   */
  public function chmodJailed($path, $mode, $recursive) {
    if ($recursive && is_dir($path)) {
      foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $filename => $file) {
        if (@(!chmod($filename, $mode))) {
          throw new FileTransferException('Cannot chmod %path.', NULL, [
            '%path' => $filename,
          ]);
        }
      }
    }
    elseif (@(!chmod($path, $mode))) {
      throw new FileTransferException('Cannot chmod %path.', NULL, [
        '%path' => $path,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FileTransfer::$hostname protected property The hostname for this file transfer.
FileTransfer::$password protected property The password for this file transfer. 1
FileTransfer::$port protected property The port for this file transfer. 1
FileTransfer::$username protected property The username for this file transfer. 1
FileTransfer::checkPath final protected function Checks that the path is inside the jail and throws an exception if not.
FileTransfer::chmod final public function Changes the permissions of the specified $path (file or directory).
FileTransfer::copyDirectory final public function Copies a directory.
FileTransfer::copyDirectoryJailed protected function Copies a directory. 1
FileTransfer::copyFile final public function Copies a file.
FileTransfer::createDirectory final public function Creates a directory.
FileTransfer::findChroot public function Returns the chroot property for this connection.
FileTransfer::fixRemotePath final protected function Returns a modified path suitable for passing to the server.
FileTransfer::getSettingsForm public function Returns a form to collect connection settings credentials. 3
FileTransfer::removeDirectory final public function Removes a directory.
FileTransfer::removeFile final public function Removes a file.
FileTransfer::sanitizePath public function Changes backslashes to slashes, also removes a trailing slash.
FileTransfer::setChroot public function Sets the chroot and changes the jail to match the correct path scheme.
FileTransfer::__get public function Implements the magic __get() method.
Local::$fileSystem protected property The file system service.
Local::chmodJailed public function Changes the permissions of the file / directory specified in $path Overrides ChmodInterface::chmodJailed
Local::connect public function Connects to the server. Overrides FileTransfer::connect
Local::copyFileJailed protected function Copies a file. Overrides FileTransfer::copyFileJailed
Local::createDirectoryJailed protected function Creates a directory. Overrides FileTransfer::createDirectoryJailed
Local::factory public static function Defines a factory method for this class. Overrides FileTransfer::factory 1
Local::isDirectory public function Checks if a particular path is a directory. Overrides FileTransfer::isDirectory
Local::isFile public function Checks if a particular path is a file (not a directory). Overrides FileTransfer::isFile
Local::removeDirectoryJailed protected function Removes a directory. Overrides FileTransfer::removeDirectoryJailed
Local::removeFileJailed protected function Removes a file. Overrides FileTransfer::removeFileJailed
Local::__construct public function Constructs a Drupal\Core\FileTransfer\FileTransfer object. Overrides FileTransfer::__construct