2 namespace Robo\Task\Filesystem;
4 use Robo\Common\ResourceExistenceChecker;
6 use Robo\Exception\TaskException;
9 * Copies one dir into another
13 * $this->taskCopyDir(['dist/config' => 'config'])->run();
15 * $this->_copyDir('dist/config', 'config');
19 class CopyDir extends BaseDir
21 use ResourceExistenceChecker;
24 * Explicitly declare our consturctor, so that
25 * our copyDir() method does not look like a php4 constructor.
27 public function __construct($dirs)
29 parent::__construct($dirs);
35 protected $chmod = 0755;
38 * Files to exclude on copying.
42 protected $exclude = [];
45 * Overwrite destination files newer than source files.
47 protected $overwrite = true;
54 if (!$this->checkResources($this->dirs, 'dir')) {
55 return Result::error($this, 'Source directories are missing!');
57 foreach ($this->dirs as $src => $dst) {
58 $this->copyDir($src, $dst);
59 $this->printTaskInfo('Copied from {source} to {destination}', ['source' => $src, 'destination' => $dst]);
61 return Result::success($this);
65 * Sets the default folder permissions for the destination if it doesn't exist
67 * @link http://en.wikipedia.org/wiki/Chmod
68 * @link http://php.net/manual/en/function.mkdir.php
69 * @link http://php.net/manual/en/function.chmod.php
75 public function dirPermissions($value)
77 $this->chmod = (int)$value;
82 * List files to exclude.
84 * @param string[] $exclude
88 public function exclude($exclude = [])
90 $this->exclude = $this->simplifyForCompare($exclude);
95 * Destination files newer than source files are overwritten.
97 * @param bool $overwrite
101 public function overwrite($overwrite)
103 $this->overwrite = $overwrite;
108 * Copies a directory to another location.
110 * @param string $src Source directory
111 * @param string $dst Destination directory
112 * @param string $parent Parent directory
114 * @throws \Robo\Exception\TaskException
116 protected function copyDir($src, $dst, $parent = '')
118 $dir = @opendir($src);
119 if (false === $dir) {
120 throw new TaskException($this, "Cannot open source directory '" . $src . "'");
123 mkdir($dst, $this->chmod, true);
125 while (false !== ($file = readdir($dir))) {
126 // Support basename and full path exclusion.
127 if ($this->excluded($file, $src, $parent)) {
130 $srcFile = $src . '/' . $file;
131 $destFile = $dst . '/' . $file;
132 if (is_dir($srcFile)) {
133 $this->copyDir($srcFile, $destFile, $parent . $file . DIRECTORY_SEPARATOR);
135 $this->fs->copy($srcFile, $destFile, $this->overwrite);
142 * Check to see if the current item is excluded.
144 protected function excluded($file, $src, $parent)
149 in_array($file, $this->exclude) ||
150 in_array($this->simplifyForCompare($parent . $file), $this->exclude) ||
151 in_array($this->simplifyForCompare($src . DIRECTORY_SEPARATOR . $file), $this->exclude);
155 * Avoid problems comparing paths on Windows that may have a
156 * combination of DIRECTORY_SEPARATOR and /.
158 protected function simplifyForCompare($item)
160 return str_replace(DIRECTORY_SEPARATOR, '/', $item);