Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
< ? php
2019-08-22 02:45:53 +00:00
namespace Drupal\Composer\Plugin\Scaffold\Operations ;
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
use Composer\IO\IOInterface ;
use Composer\Util\Filesystem ;
2019-08-22 02:45:53 +00:00
use Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath ;
use Drupal\Composer\Plugin\Scaffold\ScaffoldOptions ;
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
/**
* Scaffold operation to copy or symlink from source to destination .
2020-02-21 09:34:07 +00:00
*
* @ internal
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
*/
2019-10-09 02:55:24 +00:00
class ReplaceOp extends AbstractOperation {
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
/**
* Identifies Replace operations .
*/
const ID = 'replace' ;
/**
* The relative path to the source file .
*
2019-08-22 02:45:53 +00:00
* @ var \Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
*/
protected $source ;
/**
* Whether to overwrite existing files .
*
* @ var bool
*/
protected $overwrite ;
/**
* Constructs a ReplaceOp .
*
2019-08-22 02:45:53 +00:00
* @ param \Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath $sourcePath
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
* 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 ;
}
2020-05-03 10:42:30 +00:00
/**
* { @ inheritdoc }
*/
protected function generateContents () {
return file_get_contents ( $this -> source -> fullPath ());
}
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
/**
* { @ 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 .
*
2019-08-22 02:45:53 +00:00
* @ param \Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath $destination
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
* Scaffold file to process .
* @ param \Composer\IO\IOInterface $io
* IOInterface to writing to .
*
2019-08-22 02:45:53 +00:00
* @ return \Drupal\Composer\Plugin\Scaffold\Operations\ScaffoldResult
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
* The scaffold result .
*/
protected function copyScaffold ( ScaffoldFilePath $destination , IOInterface $io ) {
$interpolator = $destination -> getInterpolator ();
$this -> source -> addInterpolationData ( $interpolator );
2022-06-19 16:14:06 +00:00
if ( file_put_contents ( $destination -> fullPath (), $this -> contents ()) === FALSE ) {
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
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 .
*
2019-08-22 02:45:53 +00:00
* @ param \Drupal\Composer\Plugin\Scaffold\ScaffoldFilePath $destination
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
* Scaffold file to process .
* @ param \Composer\IO\IOInterface $io
* IOInterface to writing to .
*
2019-08-22 02:45:53 +00:00
* @ return \Drupal\Composer\Plugin\Scaffold\Operations\ScaffoldResult
Issue #2982684 by greg.1.anderson, Mile23, Mixologic, webflo, alexpott, yogeshmpawar, pingwin4eg, vijaycs85, larowlan, dww, borisson_, phenaproxima, kim.pepper, bojanz, grasmash, hctom, kmbremner, pingers, Jax, sherakama, derhasi, claudiu.cristea, jhedstrom, Xano, Grimreaper: Add a composer scaffolding plugin to core
2019-07-10 21:47:33 +00:00
* 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 );
}
}