3 namespace Robo\Task\File;
6 use Robo\Task\BaseTask;
13 * $this->taskWriteToFile('blogpost.md')
15 * ->line(date('Y-m-d').' '.$title)
21 class Write extends BaseTask
26 protected $stack = [];
36 protected $append = false;
41 protected $originalContents = null;
44 * @param string $filename
46 public function __construct($filename)
48 $this->filename = $filename;
52 * @param string $filename
56 public function filename($filename)
58 $this->filename = $filename;
67 public function append($append = true)
69 $this->append = $append;
78 * @return $this The current instance
80 public function line($line)
82 $this->text($line . "\n");
91 * @return $this The current instance
93 public function lines(array $lines)
95 $this->text(implode("\n", $lines) . "\n");
102 * @param string $text
104 * @return $this The current instance
106 public function text($text)
108 $this->stack[] = array_merge([__FUNCTION__ . 'Collect'], func_get_args());
113 * add a text from a file.
115 * Note that the file is read in the run() method of this task.
116 * To load text from the current state of a file (e.g. one that may
117 * be deleted or altered by other tasks prior the execution of this one),
119 * $task->text(file_get_contents($filename));
121 * @param string $filename
123 * @return $this The current instance
125 public function textFromFile($filename)
127 $this->stack[] = array_merge([__FUNCTION__ . 'Collect'], func_get_args());
132 * substitute a placeholder with value, placeholder must be enclosed by `{}`.
134 * @param string $name
137 * @return $this The current instance
139 public function place($name, $val)
141 $this->replace('{'.$name.'}', $val);
147 * replace any string with value.
149 * @param string $string
150 * @param string $replacement
152 * @return $this The current instance
154 public function replace($string, $replacement)
156 $this->stack[] = array_merge([__FUNCTION__ . 'Collect'], func_get_args());
161 * replace any string with value using regular expression.
163 * @param string $pattern
164 * @param string $replacement
166 * @return $this The current instance
168 public function regexReplace($pattern, $replacement)
170 $this->stack[] = array_merge([__FUNCTION__ . 'Collect'], func_get_args());
175 * Append the provided text to the end of the buffer if the provided
176 * regex pattern matches any text already in the buffer.
178 * @param string $pattern
179 * @param string $text
183 public function appendIfMatches($pattern, $text)
185 $this->stack[] = array_merge(['appendIfMatchesCollect'], [$pattern, $text, true]);
190 * Append the provided text to the end of the buffer unless the provided
191 * regex pattern matches any text already in the buffer.
193 * @param string $pattern
194 * @param string $text
198 public function appendUnlessMatches($pattern, $text)
200 $this->stack[] = array_merge(['appendIfMatchesCollect'], [$pattern, $text, false]);
205 * @param $contents string
206 * @param $filename string
210 protected function textFromFileCollect($contents, $filename)
212 if (file_exists($filename)) {
213 $contents .= file_get_contents($filename);
219 * @param string|string[] $contents
220 * @param string|string[] $string
221 * @param string|string[] $replacement
223 * @return string|string[]
225 protected function replaceCollect($contents, $string, $replacement)
227 return str_replace($string, $replacement, $contents);
231 * @param string|string[] $contents
232 * @param string|string[] $pattern
233 * @param string|string[] $replacement
235 * @return string|string[]
237 protected function regexReplaceCollect($contents, $pattern, $replacement)
239 return preg_replace($pattern, $replacement, $contents);
243 * @param string $contents
244 * @param string $text
248 protected function textCollect($contents, $text)
250 return $contents . $text;
254 * @param string $contents
255 * @param string $pattern
256 * @param string $text
257 * @param bool $shouldMatch
261 protected function appendIfMatchesCollect($contents, $pattern, $text, $shouldMatch)
263 if (preg_match($pattern, $contents) == $shouldMatch) {
272 public function originalContents()
274 if (!isset($this->originalContents)) {
275 $this->originalContents = '';
276 if (file_exists($this->filename)) {
277 $this->originalContents = file_get_contents($this->filename);
280 return $this->originalContents;
286 public function wouldChange()
288 return $this->originalContents() != $this->getContentsToWrite();
294 protected function getContentsToWrite()
298 $contents = $this->originalContents();
300 foreach ($this->stack as $action) {
301 $command = array_shift($action);
302 if (method_exists($this, $command)) {
303 array_unshift($action, $contents);
304 $contents = call_user_func_array([$this, $command], $action);
313 public function run()
315 $this->printTaskInfo("Writing to {filename}.", ['filename' => $this->filename]);
316 $contents = $this->getContentsToWrite();
317 if (!file_exists(dirname($this->filename))) {
318 mkdir(dirname($this->filename), 0777, true);
320 $res = file_put_contents($this->filename, $contents);
321 if ($res === false) {
322 return Result::error($this, "File {$this->filename} couldn't be created");
325 return Result::success($this);
331 public function getPath()
333 return $this->filename;