class ReplaceOp in Drupal 8
Same name and namespace in other branches
- 9 composer/Plugin/Scaffold/Operations/ReplaceOp.php \Drupal\Composer\Plugin\Scaffold\Operations\ReplaceOp
 - 10 composer/Plugin/Scaffold/Operations/ReplaceOp.php \Drupal\Composer\Plugin\Scaffold\Operations\ReplaceOp
 
Scaffold operation to copy or symlink from source to destination.
@internal
Hierarchy
- class \Drupal\Composer\Plugin\Scaffold\Operations\AbstractOperation implements OperationInterface
- class \Drupal\Composer\Plugin\Scaffold\Operations\ReplaceOp
 
 
Expanded class hierarchy of ReplaceOp
2 files declare their use of ReplaceOp
- Fixtures.php in core/
tests/ Drupal/ Tests/ Composer/ Plugin/ Scaffold/ Fixtures.php  - ReplaceOpTest.php in core/
tests/ Drupal/ Tests/ Composer/ Plugin/ Scaffold/ Integration/ ReplaceOpTest.php  
File
- composer/
Plugin/ Scaffold/ Operations/ ReplaceOp.php, line 15  
Namespace
Drupal\Composer\Plugin\Scaffold\OperationsView source
class ReplaceOp extends AbstractOperation {
  /**
   * Identifies Replace operations.
   */
  const ID = 'replace';
  /**
   * The relative path to the source file.
   *
   * @var \Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath
   */
  protected $source;
  /**
   * Whether to overwrite existing files.
   *
   * @var bool
   */
  protected $overwrite;
  /**
   * Constructs a ReplaceOp.
   *
   * @param \Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath $sourcePath
   *   The relative path to the source file.
   * @param bool $overwrite
   *   Whether to allow this scaffold file to overwrite files already at
   *   the destination. Defaults to TRUE.
   */
  public function __construct(ScaffoldFilePath $sourcePath, $overwrite = TRUE) {
    $this->source = $sourcePath;
    $this->overwrite = $overwrite;
  }
  /**
   * {@inheritdoc}
   */
  protected function generateContents() {
    return file_get_contents($this->source
      ->fullPath());
  }
  /**
   * {@inheritdoc}
   */
  public function process(ScaffoldFilePath $destination, IOInterface $io, ScaffoldOptions $options) {
    $fs = new Filesystem();
    $destination_path = $destination
      ->fullPath();
    // Do nothing if overwrite is 'false' and a file already exists at the
    // destination.
    if ($this->overwrite === FALSE && file_exists($destination_path)) {
      $interpolator = $destination
        ->getInterpolator();
      $io
        ->write($interpolator
        ->interpolate("  - Skip <info>[dest-rel-path]</info> because it already exists and overwrite is <comment>false</comment>."));
      return new ScaffoldResult($destination, FALSE);
    }
    // Get rid of the destination if it exists, and make sure that
    // the directory where it's going to be placed exists.
    $fs
      ->remove($destination_path);
    $fs
      ->ensureDirectoryExists(dirname($destination_path));
    if ($options
      ->symlink()) {
      return $this
        ->symlinkScaffold($destination, $io);
    }
    return $this
      ->copyScaffold($destination, $io);
  }
  /**
   * Copies the scaffold file.
   *
   * @param \Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath $destination
   *   Scaffold file to process.
   * @param \Composer\IO\IOInterface $io
   *   IOInterface to writing to.
   *
   * @return \Drupal\Composer\Plugin\Scaffold\Operations\ScaffoldResult
   *   The scaffold result.
   */
  protected function copyScaffold(ScaffoldFilePath $destination, IOInterface $io) {
    $interpolator = $destination
      ->getInterpolator();
    $this->source
      ->addInterpolationData($interpolator);
    $success = file_put_contents($destination
      ->fullPath(), $this
      ->contents());
    if (!$success) {
      throw new \RuntimeException($interpolator
        ->interpolate("Could not copy source file <info>[src-rel-path]</info> to <info>[dest-rel-path]</info>!"));
    }
    $io
      ->write($interpolator
      ->interpolate("  - Copy <info>[dest-rel-path]</info> from <info>[src-rel-path]</info>"));
    return new ScaffoldResult($destination, $this->overwrite);
  }
  /**
   * Symlinks the scaffold file.
   *
   * @param \Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath $destination
   *   Scaffold file to process.
   * @param \Composer\IO\IOInterface $io
   *   IOInterface to writing to.
   *
   * @return \Drupal\Composer\Plugin\Scaffold\Operations\ScaffoldResult
   *   The scaffold result.
   */
  protected function symlinkScaffold(ScaffoldFilePath $destination, IOInterface $io) {
    $interpolator = $destination
      ->getInterpolator();
    try {
      $fs = new Filesystem();
      $fs
        ->relativeSymlink($this->source
        ->fullPath(), $destination
        ->fullPath());
    } catch (\Exception $e) {
      throw new \RuntimeException($interpolator
        ->interpolate("Could not symlink source file <info>[src-rel-path]</info> to <info>[dest-rel-path]</info>!"), [], $e);
    }
    $io
      ->write($interpolator
      ->interpolate("  - Link <info>[dest-rel-path]</info> from <info>[src-rel-path]</info>"));
    return new ScaffoldResult($destination, $this->overwrite);
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            AbstractOperation:: | 
                  protected | property | Cached contents of scaffold file to be written to disk. | |
| 
            AbstractOperation:: | 
                  final public | function | 
            Returns the exact data that will be written to the scaffold files. Overrides OperationInterface:: | 
                  |
| 
            AbstractOperation:: | 
                  public | function | 
            Determines what to do if operation is used without a previous operation. Overrides OperationInterface:: | 
                  1 | 
| 
            AbstractOperation:: | 
                  public | function | 
            Determines what to do if operation is used at same path as a previous op. Overrides OperationInterface:: | 
                  1 | 
| 
            ReplaceOp:: | 
                  protected | property | Whether to overwrite existing files. | |
| 
            ReplaceOp:: | 
                  protected | property | The relative path to the source file. | |
| 
            ReplaceOp:: | 
                  protected | function | Copies the scaffold file. | |
| 
            ReplaceOp:: | 
                  protected | function | 
            Load the scaffold contents or otherwise generate what is needed. Overrides AbstractOperation:: | 
                  |
| 
            ReplaceOp:: | 
                  constant | Identifies Replace operations. | ||
| 
            ReplaceOp:: | 
                  public | function | 
            Process this scaffold operation. Overrides OperationInterface:: | 
                  |
| 
            ReplaceOp:: | 
                  protected | function | Symlinks the scaffold file. | |
| 
            ReplaceOp:: | 
                  public | function | Constructs a ReplaceOp. |