Skip to content

Commit

Permalink
Makes copying & removing to support 'include' & 'exclude' options.
Browse files Browse the repository at this point in the history
  • Loading branch information
jails committed May 1, 2016
1 parent b9a36e3 commit 842e984
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 10 deletions.
89 changes: 86 additions & 3 deletions spec/Suite/DirSpec.php
Expand Up @@ -192,6 +192,42 @@

});

it("copies a directory recursively respecting the include option", function() {

Dir::copy('spec/Fixture', $this->tmpDir, ['include' => '*.txt']);

$files = Dir::scan($this->tmpDir, [
'type' => 'file'
]);

sort($files);
expect($files)->toBe($this->normalize([
$this->tmpDir . '/Fixture/Extensions/Childs/child1.txt',
$this->tmpDir . '/Fixture/Nested/Childs/child1.txt',
$this->tmpDir . '/Fixture/Nested/nested_file1.txt',
$this->tmpDir . '/Fixture/Nested/nested_file2.txt',
$this->tmpDir . '/Fixture/file1.txt'
]));

});

it("copies a directory recursively respecting the exclude option", function() {

Dir::copy('spec/Fixture', $this->tmpDir, ['exclude' => '*.txt']);

$files = Dir::scan($this->tmpDir, [
'type' => 'file'
]);

sort($files);
expect($files)->toBe($this->normalize([
$this->tmpDir . '/Fixture/Extensions/file.xml',
$this->tmpDir . '/Fixture/Extensions/index.html',
$this->tmpDir . '/Fixture/Extensions/index.php'
]));

});

it("copies a directory recursively but not following symlinks", function() {

Dir::copy('spec/Fixture', $this->tmpDir, ['followSymlinks' => false]);
Expand Down Expand Up @@ -223,9 +259,15 @@

describe("::remove()", function() {

it("removes a directory recursively", function() {

beforeEach(function() {
$this->tmpDir = Dir::tempnam(sys_get_temp_dir(), 'spec');
});

afterEach(function() {
Dir::remove($this->tmpDir);
});

it("removes a directory recursively", function() {

Dir::copy('spec/Fixture', $this->tmpDir);

Expand All @@ -242,6 +284,47 @@

});

it("removes a directory recursively respecting the include option", function() {

Dir::copy('spec/Fixture', $this->tmpDir);

Dir::remove($this->tmpDir, ['include' => '*.txt']);

$files = Dir::scan($this->tmpDir, [
'type' => 'file'
]);

sort($files);
expect($files)->toBe($this->normalize([
$this->tmpDir . '/Fixture/Extensions/file.xml',
$this->tmpDir . '/Fixture/Extensions/index.html',
$this->tmpDir . '/Fixture/Extensions/index.php'
]));


});

it("removes a directory recursively respecting the exclude option", function() {

Dir::copy('spec/Fixture', $this->tmpDir);

Dir::remove($this->tmpDir, ['exclude' => '*.txt']);

$files = Dir::scan($this->tmpDir, [
'type' => 'file'
]);

sort($files);
expect($files)->toBe($this->normalize([
$this->tmpDir . '/Fixture/Extensions/Childs/child1.txt',
$this->tmpDir . '/Fixture/Nested/Childs/child1.txt',
$this->tmpDir . '/Fixture/Nested/nested_file1.txt',
$this->tmpDir . '/Fixture/Nested/nested_file2.txt',
$this->tmpDir . '/Fixture/file1.txt'
]));

});

});

describe("::make()", function() {
Expand All @@ -252,7 +335,7 @@
});

afterEach(function() {
Dir::remove($this->tmpDir, ['recursive' => true]);
Dir::remove($this->tmpDir);
umask($this->umask);
});

Expand Down
38 changes: 31 additions & 7 deletions src/Dir.php
Expand Up @@ -144,6 +144,8 @@ public static function _dirFlags($options)
* @param array $options Scanning options. Possible values are:
* -`'followSymlinks'` _boolean_ : Follows Symlinks if `true`.
* -`'recursive'` _boolean_ : Scans recursively if `true`.
* -`'include'` _string|array_: An array of includes.
* -`'exclude'` _string|array_: An array of excludes.
*/
public static function remove($path, $options = [])
{
Expand All @@ -157,13 +159,31 @@ public static function remove($path, $options = [])
$options['skipDots'] = true;
$options['leavesOnly'] = false;
$options['iterator'] = RecursiveIteratorIterator::CHILD_FIRST;
unset($options['include']);
unset($options['exclude']);

$paths = array_merge(static::scan($path, $options), (array) $path);
try {
$paths = array_merge(static::scan($path, $options), (array) $path);
} catch (Exception $e) {
return true;
}

$isDirEmpty = function($path) {
$handle = opendir($path);
while (($entry = readdir($handle)) !== false) {
if ($entry !== '.' && $entry !== '..') {
return false;
}
}
return true;
};

foreach ($paths as $path) {
is_dir($path) ? rmdir($path) : unlink($path);
if (is_dir($path)) {
if ($isDirEmpty($path)) {
rmdir($path);
}
} else {
unlink($path);
}
}
}

Expand All @@ -177,6 +197,8 @@ public static function remove($path, $options = [])
* -`'childsOnly'` _boolean_ : Excludes parent directory if `true`.
* -`'followSymlinks'` _boolean_ : Follows Symlinks if `true`.
* -`'recursive'` _boolean_ : Scans recursively if `true`.
* -`'include'` _string|array_: An array of includes.
* -`'exclude'` _string|array_: An array of excludes.
* @return array
* @throws Exception
*/
Expand All @@ -194,8 +216,6 @@ public static function copy($path, $dest, $options = [])
$options['skipDots'] = true;
$options['leavesOnly'] = false;
$options['iterator'] = RecursiveIteratorIterator::SELF_FIRST;
unset($options['include']);
unset($options['exclude']);

$sources = (array) $path;

Expand Down Expand Up @@ -236,7 +256,11 @@ protected static function _copy($path, $dest, $options)
if (is_dir($path)) {
mkdir($dest . $ds . ltrim($target, $ds), $options['mode'], true);
} else {
copy($path, $dest . $ds . ltrim($target, $ds));
$target = $dest . $ds . ltrim($target, $ds);
if (!file_exists(dirname($target))) {
mkdir(dirname($target), $options['mode'], true);
}
copy($path, $target);
}
}
}
Expand Down

0 comments on commit 842e984

Please sign in to comment.