Completed
Pull Request — master (#555)
by
unknown
04:31
created

CopyDir::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Robo\Task\Filesystem;
3
4
use Robo\Common\ResourceExistenceChecker;
5
use Robo\Result;
6
use Robo\Exception\TaskException;
7
8
/**
9
 * Copies one dir into another
10
 *
11
 * ``` php
12
 * <?php
13
 * $this->taskCopyDir(['dist/config' => 'config'])->run();
14
 * // as shortcut
15
 * $this->_copyDir('dist/config', 'config');
16
 * ?>
17
 * ```
18
 */
19
class CopyDir extends BaseDir
20
{
21
    use ResourceExistenceChecker;
22
23
    /**
24
     * @var int
25
     */
26
    protected $chmod = 0755;
27
28
    /**
29
     * Files to exclude on copying.
30
     *
31
     * @var string[]
32
     */
33
    protected $exclude = [];
34
35
    /**
36
     * Overwrite destination files newer than source files.
37
     */
38
    protected $overwrite;
39
40
    /**
41
     * @param string|string[] $dirs
42
     * @param bool $overwrite
43
     */
44
    public function __construct($dirs, $overwrite)
45
    {
46
      $this->overwrite = $overwrite;
47
      parent::__construct($dirs);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function run()
54
    {
55
        if (!$this->checkResources($this->dirs, 'dir')) {
56
            return Result::error($this, 'Source directories are missing!');
57
        }
58
        foreach ($this->dirs as $src => $dst) {
59
            $this->copyDir($src, $dst, $this->overwrite);
60
            $this->printTaskInfo('Copied from {source} to {destination}', ['source' => $src, 'destination' => $dst]);
61
        }
62
        return Result::success($this);
63
    }
64
65
    /**
66
     * Sets the default folder permissions for the destination if it doesn't exist
67
     *
68
     * @link http://en.wikipedia.org/wiki/Chmod
69
     * @link http://php.net/manual/en/function.mkdir.php
70
     * @link http://php.net/manual/en/function.chmod.php
71
     *
72
     * @param int $value
73
     *
74
     * @return $this
75
     */
76
    public function dirPermissions($value)
77
    {
78
        $this->chmod = (int)$value;
79
        return $this;
80
    }
81
82
    /**
83
     * List files to exclude.
84
     *
85
     * @param string[] $exclude
86
     *
87
     * @return $this
88
     */
89
    public function exclude($exclude = [])
90
    {
91
        $this->exclude = $exclude;
92
        return $this;
93
    }
94
95
    /**
96
     * Copies a directory to another location.
97
     *
98
     * @param string $src Source directory
99
     * @param string $dst Destination directory
100
     * @param bool   $overwrite If true, destination files newer than source files are overwritten
101
     *
102
     *
103
     * @throws \Robo\Exception\TaskException
104
     */
105
    protected function copyDir($src, $dst, $overwrite)
106
    {
107
        $dir = @opendir($src);
108
        if (false === $dir) {
109
            throw new TaskException($this, "Cannot open source directory '" . $src . "'");
110
        }
111
        if (!is_dir($dst)) {
112
            mkdir($dst, $this->chmod, true);
113
        }
114
        while (false !== ($file = readdir($dir))) {
115
            if (in_array($file, $this->exclude)) {
116
                 continue;
117
            }
118
            if (($file !== '.') && ($file !== '..')) {
119
                $srcFile = $src . '/' . $file;
120
                $destFile = $dst . '/' . $file;
121
                if (is_dir($srcFile)) {
122
                    $this->copyDir($srcFile, $destFile, $overwrite);
123
                } else {
124
                    $this->fs->copy($srcFile, $destFile, $overwrite);
125
                }
126
            }
127
        }
128
        closedir($dir);
129
    }
130
}
131