X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fconsolidation%2Frobo%2Fsrc%2FTask%2FFile%2FReplace.php;fp=vendor%2Fconsolidation%2Frobo%2Fsrc%2FTask%2FFile%2FReplace.php;h=0107df13c3e7c20e75c34ae07bcbe88ff1d325fc;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/consolidation/robo/src/Task/File/Replace.php b/vendor/consolidation/robo/src/Task/File/Replace.php new file mode 100644 index 000000000..0107df13c --- /dev/null +++ b/vendor/consolidation/robo/src/Task/File/Replace.php @@ -0,0 +1,141 @@ +taskReplaceInFile('VERSION') + * ->from('0.2.0') + * ->to('0.3.0') + * ->run(); + * + * $this->taskReplaceInFile('README.md') + * ->from(date('Y')-1) + * ->to(date('Y')) + * ->run(); + * + * $this->taskReplaceInFile('config.yml') + * ->regex('~^service:~') + * ->to('services:') + * ->run(); + * + * $this->taskReplaceInFile('box/robo.txt') + * ->from(array('##dbname##', '##dbhost##')) + * ->to(array('robo', 'localhost')) + * ->run(); + * ?> + * ``` + */ +class Replace extends BaseTask +{ + /** + * @var string + */ + protected $filename; + + /** + * @var string|string[] + */ + protected $from; + + /** + * @var string|string[] + */ + protected $to; + + /** + * @var string + */ + protected $regex; + + /** + * @param string $filename + */ + public function __construct($filename) + { + $this->filename = $filename; + } + + /** + * @param string $filename + * + * @return $this + */ + public function filename($filename) + { + $this->filename = $filename; + return $this; + } + + /** + * String(s) to be replaced. + * + * @param string|string[] $from + * + * @return $this + */ + public function from($from) + { + $this->from = $from; + return $this; + } + + /** + * Value(s) to be set as a replacement. + * + * @param string|string[] $to + * + * @return $this + */ + public function to($to) + { + $this->to = $to; + return $this; + } + + /** + * Regex to match string to be replaced. + * + * @param string $regex + * + * @return $this + */ + public function regex($regex) + { + $this->regex = $regex; + return $this; + } + + /** + * {@inheritdoc} + */ + public function run() + { + if (!file_exists($this->filename)) { + $this->printTaskError('File {filename} does not exist', ['filename' => $this->filename]); + return false; + } + + $text = file_get_contents($this->filename); + if ($this->regex) { + $text = preg_replace($this->regex, $this->to, $text, -1, $count); + } else { + $text = str_replace($this->from, $this->to, $text, $count); + } + if ($count > 0) { + $res = file_put_contents($this->filename, $text); + if ($res === false) { + return Result::error($this, "Error writing to file {filename}.", ['filename' => $this->filename]); + } + $this->printTaskSuccess("{filename} updated. {count} items replaced", ['filename' => $this->filename, 'count' => $count]); + } else { + $this->printTaskInfo("{filename} unchanged. {count} items replaced", ['filename' => $this->filename, 'count' => $count]); + } + return Result::success($this, '', ['replaced' => $count]); + } +}