You are here

public function AppendOp::process in Drupal 8

Same name and namespace in other branches
  1. 9 composer/Plugin/Scaffold/Operations/AppendOp.php \Drupal\Composer\Plugin\Scaffold\Operations\AppendOp::process()
  2. 10 composer/Plugin/Scaffold/Operations/AppendOp.php \Drupal\Composer\Plugin\Scaffold\Operations\AppendOp::process()

Process this scaffold operation.

Parameters

\Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath $destination: Scaffold file's destination path.

\Composer\IO\IOInterface $io: IOInterface to write to.

\Drupal\Composer\Plugin\Scaffold\ScaffoldOptions $options: Various options that may alter the behavior of the operation.

Return value

\Drupal\Composer\Plugin\Scaffold\Operations\ScaffoldResult Result of the scaffolding operation.

Overrides OperationInterface::process

File

composer/Plugin/Scaffold/Operations/AppendOp.php, line 107

Class

AppendOp
Scaffold operation to add to the beginning and/or end of a scaffold file.

Namespace

Drupal\Composer\Plugin\Scaffold\Operations

Code

public function process(ScaffoldFilePath $destination, IOInterface $io, ScaffoldOptions $options) {
  $destination_path = $destination
    ->fullPath();
  $interpolator = $destination
    ->getInterpolator();

  // Be extra-noisy of creating a new file or appending to a non-scaffold
  // file. Note that if the file already has the append contents, then the
  // OperationFactory will make a SkipOp instead, and we will not get here.
  if (!$this->managed) {
    $message = '  - <info>NOTICE</info> Modifying existing file at <info>[dest-rel-path]</info>.';
    if (!file_exists($destination_path)) {
      $message = '  - <info>NOTICE</info> Creating a new file at <info>[dest-rel-path]</info>.';
    }
    $message .= ' Examine the contents and ensure that it came out correctly.';
    $io
      ->write($interpolator
      ->interpolate($message));
  }

  // Notify that we are prepending, if there is prepend data.
  if (!empty($this->prepend)) {
    $this->prepend
      ->addInterpolationData($interpolator, 'prepend');
    $io
      ->write($interpolator
      ->interpolate("  - Prepend to <info>[dest-rel-path]</info> from <info>[prepend-rel-path]</info>"));
  }

  // Notify that we are appending, if there is append data.
  $append_contents = '';
  if (!empty($this->append)) {
    $this->append
      ->addInterpolationData($interpolator, 'append');
    $io
      ->write($interpolator
      ->interpolate("  - Append to <info>[dest-rel-path]</info> from <info>[append-rel-path]</info>"));
  }

  // Write the resulting data
  file_put_contents($destination_path, $this
    ->contents());

  // Return a ScaffoldResult with knowledge of whether this file is managed.
  return new ScaffoldResult($destination, $this->managed);
}