AppendOp.php in Drupal 10
File
composer/Plugin/Scaffold/Operations/AppendOp.php
View source
<?php
namespace Drupal\Composer\Plugin\Scaffold\Operations;
use Composer\IO\IOInterface;
use Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath;
use Drupal\Composer\Plugin\Scaffold\ScaffoldOptions;
class AppendOp extends AbstractOperation {
const ID = 'append';
protected $prepend;
protected $append;
protected $default;
protected $managed;
protected $forceAppend;
protected $originalContents;
public function __construct(ScaffoldFilePath $prepend_path = NULL, ScaffoldFilePath $append_path = NULL, $force_append = FALSE, ScaffoldFilePath $default_path = NULL) {
$this->forceAppend = $force_append;
$this->prepend = $prepend_path;
$this->append = $append_path;
$this->default = $default_path;
$this->managed = TRUE;
}
protected function generateContents() {
$prepend_contents = '';
if (!empty($this->prepend)) {
$prepend_contents = file_get_contents($this->prepend
->fullPath()) . "\n";
}
$append_contents = '';
if (!empty($this->append)) {
$append_contents = "\n" . file_get_contents($this->append
->fullPath());
}
$original_contents = $this->originalContents;
if (empty($original_contents) && !empty($this->default)) {
$original_contents = file_get_contents($this->default
->fullPath());
}
return $prepend_contents . $original_contents . $append_contents;
}
public function process(ScaffoldFilePath $destination, IOInterface $io, ScaffoldOptions $options) {
$destination_path = $destination
->fullPath();
$interpolator = $destination
->getInterpolator();
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));
}
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>"));
}
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>"));
}
file_put_contents($destination_path, $this
->contents());
return new ScaffoldResult($destination, $this->managed);
}
public function scaffoldOverExistingTarget(OperationInterface $existing_target) {
$this->originalContents = $existing_target
->contents();
return $this;
}
public function scaffoldAtNewLocation(ScaffoldFilePath $destination) {
$this->managed = FALSE;
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 (!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);
}
$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);
}
$this->originalContents = $existingData;
return $this;
}
protected function existingFileHasData($contents, $data_path) {
if (empty($data_path)) {
return FALSE;
}
$data = file_get_contents($data_path
->fullPath());
return strpos($contents, $data) !== FALSE;
}
}
Classes
Name |
Description |
AppendOp |
Scaffold operation to add to the beginning and/or end of a scaffold file. |