You are here

public function AppendOp::scaffoldAtNewLocation in Drupal 8

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

Determines what to do if operation is used without a previous operation.

Default behavior is to scaffold this operation at the specified destination. Most operations overwrite rather than modify existing files, and therefore do not need to do anything special when there is no existing file.

Parameters

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

Return value

OperationInterface The op to use at this destination.

Overrides AbstractOperation::scaffoldAtNewLocation

File

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

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 scaffoldAtNewLocation(ScaffoldFilePath $destination) {

  // If there is no existing scaffold file at the target location, then any
  // append we do will be to an unmanaged file.
  $this->managed = FALSE;

  // Default: do not allow an append over a file that was not scaffolded.
  if (!$this->forceAppend) {
    $message = "  - Skip <info>[dest-rel-path]</info>: cannot append to a path that was not scaffolded unless 'force-append' property is set.";
    return new SkipOp($message);
  }

  // If the target file does not exist, then we will allow the append to
  // happen if we have default data to provide for it.
  if (!file_exists($destination
    ->fullPath())) {
    if (!empty($this->default)) {
      return $this;
    }
    $message = "  - Skip <info>[dest-rel-path]</info>: no file exists at the target path, and no default data provided.";
    return new SkipOp($message);
  }

  // If the target file DOES exist, and it already contains the append/prepend
  // data, then we will skip the operation.
  $existingData = file_get_contents($destination
    ->fullPath());
  if ($this
    ->existingFileHasData($existingData, $this->append) || $this
    ->existingFileHasData($existingData, $this->prepend)) {
    $message = "  - Skip <info>[dest-rel-path]</info>: the file already has the append/prepend data.";
    return new SkipOp($message);
  }

  // Cache the original data to use during append.
  $this->originalContents = $existingData;
  return $this;
}