2 namespace Robo\Task\File;
5 use Robo\Common\ResourceExistenceChecker;
7 use Robo\Task\BaseTask;
10 * Merges files into one. Used for preparing assets.
15 * 'web/assets/screen.css',
16 * 'web/assets/print.css',
17 * 'web/assets/theme.css'
19 * ->to('web/assets/style.css')
24 class Concat extends BaseTask
26 use ResourceExistenceChecker;
41 * @param array|Iterator $files
43 public function __construct($files)
45 $this->files = $files;
49 * set the destination file
55 public function to($dst)
67 if (is_null($this->dst) || "" === $this->dst) {
68 return Result::error($this, 'You must specify a destination file with to() method.');
71 if (!$this->checkResources($this->files, 'file')) {
72 return Result::error($this, 'Source files are missing!');
75 if (file_exists($this->dst) && !is_writable($this->dst)) {
76 return Result::error($this, 'Destination already exists and cannot be overwritten.');
81 foreach ($this->files as $path) {
82 foreach (glob($path) as $file) {
83 $dump .= file_get_contents($file) . "\n";
87 $this->printTaskInfo('Writing {destination}', ['destination' => $this->dst]);
89 $dst = $this->dst . '.part';
90 $write_result = file_put_contents($dst, $dump);
92 if (false === $write_result) {
94 return Result::error($this, 'File write failed.');
96 // Cannot be cross-volume; should always succeed.
97 @rename($dst, $this->dst);
99 return Result::success($this);