mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Merge pull request #821 from dereuromark/2.3-folder-merge
allow Folder to merge recursivly and add scheme option
This commit is contained in:
commit
5081be171f
2 changed files with 306 additions and 84 deletions
|
@ -29,11 +29,16 @@ class FolderTest extends CakeTestCase {
|
||||||
protected static $_tmp = array();
|
protected static $_tmp = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save the directory names in TMP
|
* Save the directory names in TMP and make sure default directories exist
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function setUpBeforeClass() {
|
public static function setUpBeforeClass() {
|
||||||
|
$dirs = array('cache', 'logs', 'sessions', 'tests');
|
||||||
|
foreach ($dirs as $dir) {
|
||||||
|
new Folder(TMP . $dir, true);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (scandir(TMP) as $file) {
|
foreach (scandir(TMP) as $file) {
|
||||||
if (is_dir(TMP . $file) && !in_array($file, array('.', '..'))) {
|
if (is_dir(TMP . $file) && !in_array($file, array('.', '..'))) {
|
||||||
self::$_tmp[] = $file;
|
self::$_tmp[] = $file;
|
||||||
|
@ -803,6 +808,64 @@ class FolderTest extends CakeTestCase {
|
||||||
/**
|
/**
|
||||||
* testCopy method
|
* testCopy method
|
||||||
*
|
*
|
||||||
|
* Verify that subdirectories existing in both destination and source directory
|
||||||
|
* are merged recursively.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCopy() {
|
||||||
|
extract($this->_setupFilesystem());
|
||||||
|
|
||||||
|
$Folder = new Folder($folderOne);
|
||||||
|
$result = $Folder->copy($folderThree);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($folderTwo);
|
||||||
|
$result = $Folder->copy($folderThree);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($path);
|
||||||
|
$Folder->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testCopyWithMerge method
|
||||||
|
*
|
||||||
|
* Verify that subdirectories existing in both destination and source directory
|
||||||
|
* are merged recursively.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testCopyWithMerge() {
|
||||||
|
extract($this->_setupFilesystem());
|
||||||
|
|
||||||
|
$Folder = new Folder($folderOne);
|
||||||
|
$result = $Folder->copy($folderThree);
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($folderTwo);
|
||||||
|
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::MERGE));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($path);
|
||||||
|
$Folder->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testCopyWithSkip method
|
||||||
|
*
|
||||||
* Verify that directories and files are copied recursively
|
* Verify that directories and files are copied recursively
|
||||||
* even if the destination directory already exists.
|
* even if the destination directory already exists.
|
||||||
* Subdirectories existing in both destination and source directory
|
* Subdirectories existing in both destination and source directory
|
||||||
|
@ -810,125 +873,251 @@ class FolderTest extends CakeTestCase {
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testCopy() {
|
public function testCopyWithSkip() {
|
||||||
$path = TMP . 'folder_test';
|
extract($this->_setupFilesystem());
|
||||||
$folderOne = $path . DS . 'folder1';
|
|
||||||
$folderTwo = $folderOne . DS . 'folder2';
|
$Folder = new Folder($folderOne);
|
||||||
$folderThree = $path . DS . 'folder3';
|
$result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
|
||||||
$fileOne = $folderOne . DS . 'file1.php';
|
$this->assertTrue($result);
|
||||||
$fileTwo = $folderTwo . DS . 'file2.php';
|
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($folderTwo);
|
||||||
|
$Folder->delete();
|
||||||
|
|
||||||
|
$Folder = new Folder($folderOne);
|
||||||
|
$result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($folderTwo);
|
||||||
|
$Folder->delete();
|
||||||
|
|
||||||
new Folder($path, true);
|
|
||||||
new Folder($folderOne, true);
|
|
||||||
new Folder($folderTwo, true);
|
new Folder($folderTwo, true);
|
||||||
new Folder($folderThree, true);
|
new Folder($folderTwo . DS . 'folderB', true);
|
||||||
touch($fileOne);
|
file_put_contents($folderTwo . DS . 'file2.php', 'touched');
|
||||||
touch($fileTwo);
|
file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');
|
||||||
|
|
||||||
$Folder = new Folder($folderOne);
|
$Folder = new Folder($folderTwo);
|
||||||
$result = $Folder->copy($folderThree);
|
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::SKIP));
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'folder2' . DS . 'file2.php'));
|
$this->assertEquals('touched', file_get_contents($folderThree . DS . 'file2.php'));
|
||||||
|
$this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folderB' . DS . 'fileB.php'));
|
||||||
$Folder = new Folder($folderThree);
|
|
||||||
$Folder->delete();
|
|
||||||
|
|
||||||
$Folder = new Folder($folderOne);
|
|
||||||
$result = $Folder->copy($folderThree);
|
|
||||||
$this->assertTrue($result);
|
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'folder2' . DS . 'file2.php'));
|
|
||||||
|
|
||||||
$Folder = new Folder($folderThree);
|
|
||||||
$Folder->delete();
|
|
||||||
|
|
||||||
new Folder($folderThree, true);
|
|
||||||
new Folder($folderThree . DS . 'folder2', true);
|
|
||||||
file_put_contents($folderThree . DS . 'folder2' . DS . 'file2.php', 'untouched');
|
|
||||||
|
|
||||||
$Folder = new Folder($folderOne);
|
|
||||||
$result = $Folder->copy($folderThree);
|
|
||||||
$this->assertTrue($result);
|
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
|
||||||
$this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folder2' . DS . 'file2.php'));
|
|
||||||
|
|
||||||
$Folder = new Folder($path);
|
$Folder = new Folder($path);
|
||||||
$Folder->delete();
|
$Folder->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testCopyWithOverwrite
|
||||||
|
*
|
||||||
|
* Verify that subdirectories existing in both destination and source directory
|
||||||
|
* are overwritten/replaced recursively.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testCopyWithOverwrite() {
|
||||||
|
extract($this->_setupFilesystem());
|
||||||
|
|
||||||
|
$Folder = new Folder($folderOne);
|
||||||
|
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
|
||||||
|
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($folderTwo);
|
||||||
|
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($folderOne);
|
||||||
|
unlink($fileOneA);
|
||||||
|
$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
|
||||||
|
$this->assertTrue(!file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
|
||||||
|
|
||||||
|
$Folder = new Folder($path);
|
||||||
|
$Folder->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup filesystem for copy tests
|
||||||
|
* $path: folder_test/
|
||||||
|
* - folder1/file1.php
|
||||||
|
* - folder1/folderA/fileA.php
|
||||||
|
* - folder2/file2.php
|
||||||
|
* - folder2/folderB/fileB.php
|
||||||
|
* - folder3/
|
||||||
|
*
|
||||||
|
* @return array Filenames to extract in the test methods
|
||||||
|
*/
|
||||||
|
protected function _setupFilesystem() {
|
||||||
|
$path = TMP . 'folder_test';
|
||||||
|
|
||||||
|
$folderOne = $path . DS . 'folder1';
|
||||||
|
$folderOneA = $folderOne . DS . 'folderA';
|
||||||
|
$folderTwo = $path . DS . 'folder2';
|
||||||
|
$folderTwoB = $folderTwo . DS . 'folderB';
|
||||||
|
$folderThree = $path . DS . 'folder3';
|
||||||
|
|
||||||
|
$fileOne = $folderOne . DS . 'file1.php';
|
||||||
|
$fileTwo = $folderTwo . DS . 'file2.php';
|
||||||
|
$fileOneA = $folderOneA . DS . 'fileA.php';
|
||||||
|
$fileTwoB = $folderTwoB . DS . 'fileB.php';
|
||||||
|
|
||||||
|
new Folder($path, true);
|
||||||
|
new Folder($folderOne, true);
|
||||||
|
new Folder($folderOneA, true);
|
||||||
|
new Folder($folderTwo, true);
|
||||||
|
new Folder($folderTwoB, true);
|
||||||
|
new Folder($folderThree, true);
|
||||||
|
touch($fileOne);
|
||||||
|
touch($fileTwo);
|
||||||
|
touch($fileOneA);
|
||||||
|
touch($fileTwoB);
|
||||||
|
|
||||||
|
return compact(
|
||||||
|
'path',
|
||||||
|
'folderOne', 'folderOneA', 'folderTwo', 'folderTwoB', 'folderThree',
|
||||||
|
'fileOne', 'fileOneA', 'fileTwo', 'fileTwoB');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* testMove method
|
* testMove method
|
||||||
*
|
*
|
||||||
* Verify that directories and files are moved recursively
|
* Verify that directories and files are moved recursively
|
||||||
* even if the destination directory already exists.
|
* even if the destination directory already exists.
|
||||||
* Subdirectories existing in both destination and source directory
|
* Subdirectories existing in both destination and source directory
|
||||||
* are skipped and not merged or overwritten.
|
* are merged recursively.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testMove() {
|
public function testMove() {
|
||||||
$path = TMP . 'folder_test';
|
extract($this->_setupFilesystem());
|
||||||
$folderOne = $path . DS . 'folder1';
|
|
||||||
$folderTwo = $folderOne . DS . 'folder2';
|
|
||||||
$folderThree = $path . DS . 'folder3';
|
|
||||||
$fileOne = $folderOne . DS . 'file1.php';
|
|
||||||
$fileTwo = $folderTwo . DS . 'file2.php';
|
|
||||||
|
|
||||||
new Folder($path, true);
|
|
||||||
new Folder($folderOne, true);
|
|
||||||
new Folder($folderTwo, true);
|
|
||||||
new Folder($folderThree, true);
|
|
||||||
touch($fileOne);
|
|
||||||
touch($fileTwo);
|
|
||||||
|
|
||||||
$Folder = new Folder($folderOne);
|
$Folder = new Folder($folderOne);
|
||||||
$result = $Folder->move($folderThree);
|
$result = $Folder->move($folderTwo);
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
|
||||||
$this->assertTrue(is_dir($folderThree . DS . 'folder2'));
|
$this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'folder2' . DS . 'file2.php'));
|
$this->assertTrue(file_exists($folderTwo . DS . 'folderB' . DS . 'fileB.php'));
|
||||||
$this->assertFalse(file_exists($fileOne));
|
$this->assertFalse(file_exists($fileOne));
|
||||||
$this->assertFalse(file_exists($folderTwo));
|
$this->assertTrue(file_exists($folderTwo . DS . 'folderA'));
|
||||||
$this->assertFalse(file_exists($fileTwo));
|
$this->assertFalse(file_exists($folderOneA));
|
||||||
|
$this->assertFalse(file_exists($fileOneA));
|
||||||
|
|
||||||
$Folder = new Folder($folderThree);
|
$Folder = new Folder($folderTwo);
|
||||||
$Folder->delete();
|
$Folder->delete();
|
||||||
|
|
||||||
new Folder($folderOne, true);
|
new Folder($folderOne, true);
|
||||||
new Folder($folderTwo, true);
|
new Folder($folderOneA, true);
|
||||||
touch($fileOne);
|
touch($fileOne);
|
||||||
touch($fileTwo);
|
touch($fileOneA);
|
||||||
|
|
||||||
$Folder = new Folder($folderOne);
|
$Folder = new Folder($folderOne);
|
||||||
$result = $Folder->move($folderThree);
|
$result = $Folder->move($folderTwo);
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
|
||||||
$this->assertTrue(is_dir($folderThree . DS . 'folder2'));
|
$this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'folder2' . DS . 'file2.php'));
|
$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
$this->assertFalse(file_exists($fileOne));
|
$this->assertFalse(file_exists($fileOne));
|
||||||
$this->assertFalse(file_exists($folderTwo));
|
$this->assertFalse(file_exists($folderOneA));
|
||||||
$this->assertFalse(file_exists($fileTwo));
|
$this->assertFalse(file_exists($fileOneA));
|
||||||
|
|
||||||
$Folder = new Folder($folderThree);
|
$Folder = new Folder($folderTwo);
|
||||||
$Folder->delete();
|
$Folder->delete();
|
||||||
|
|
||||||
new Folder($folderOne, true);
|
new Folder($folderOne, true);
|
||||||
|
new Folder($folderOneA, true);
|
||||||
new Folder($folderTwo, true);
|
new Folder($folderTwo, true);
|
||||||
new Folder($folderThree, true);
|
new Folder($folderTwoB, true);
|
||||||
new Folder($folderThree . DS . 'folder2', true);
|
|
||||||
touch($fileOne);
|
touch($fileOne);
|
||||||
touch($fileTwo);
|
touch($fileOneA);
|
||||||
file_put_contents($folderThree . DS . 'folder2' . DS . 'file2.php', 'untouched');
|
new Folder($folderOne . DS . 'folderB', true);
|
||||||
|
touch($folderOne . DS . 'folderB' . DS . 'fileB.php');
|
||||||
|
file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');
|
||||||
|
|
||||||
$Folder = new Folder($folderOne);
|
$Folder = new Folder($folderOne);
|
||||||
$result = $Folder->move($folderThree);
|
$result = $Folder->move($folderTwo);
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
|
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
|
||||||
$this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folder2' . DS . 'file2.php'));
|
$this->assertEquals('', file_get_contents($folderTwoB . DS . 'fileB.php'));
|
||||||
$this->assertFalse(file_exists($fileOne));
|
$this->assertFalse(file_exists($fileOne));
|
||||||
$this->assertFalse(file_exists($folderTwo));
|
$this->assertFalse(file_exists($folderOneA));
|
||||||
$this->assertFalse(file_exists($fileTwo));
|
$this->assertFalse(file_exists($fileOneA));
|
||||||
|
|
||||||
|
$Folder = new Folder($path);
|
||||||
|
$Folder->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testMoveWithSkip method
|
||||||
|
*
|
||||||
|
* Verify that directories and files are moved recursively
|
||||||
|
* even if the destination directory already exists.
|
||||||
|
* Subdirectories existing in both destination and source directory
|
||||||
|
* are skipped and not merged or overwritten.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testMoveWithSkip() {
|
||||||
|
extract($this->_setupFilesystem());
|
||||||
|
|
||||||
|
$Folder = new Folder($folderOne);
|
||||||
|
$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
|
||||||
|
$this->assertTrue(file_exists($folderTwoB . DS . 'fileB.php'));
|
||||||
|
$this->assertFalse(file_exists($fileOne));
|
||||||
|
$this->assertFalse(file_exists($folderOneA));
|
||||||
|
$this->assertFalse(file_exists($fileOneA));
|
||||||
|
|
||||||
|
$Folder = new Folder($folderTwo);
|
||||||
|
$Folder->delete();
|
||||||
|
|
||||||
|
new Folder($folderOne, true);
|
||||||
|
new Folder($folderOneA, true);
|
||||||
|
new Folder($folderTwo, true);
|
||||||
|
touch($fileOne);
|
||||||
|
touch($fileOneA);
|
||||||
|
|
||||||
|
$Folder = new Folder($folderOne);
|
||||||
|
$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
|
||||||
|
$this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
|
||||||
|
$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
|
||||||
|
$this->assertFalse(file_exists($fileOne));
|
||||||
|
$this->assertFalse(file_exists($folderOneA));
|
||||||
|
$this->assertFalse(file_exists($fileOneA));
|
||||||
|
|
||||||
|
$Folder = new Folder($folderTwo);
|
||||||
|
$Folder->delete();
|
||||||
|
|
||||||
|
new Folder($folderOne, true);
|
||||||
|
new Folder($folderOneA, true);
|
||||||
|
new Folder($folderTwo, true);
|
||||||
|
new Folder($folderTwoB, true);
|
||||||
|
touch($fileOne);
|
||||||
|
touch($fileOneA);
|
||||||
|
file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');
|
||||||
|
|
||||||
|
$Folder = new Folder($folderOne);
|
||||||
|
$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
|
||||||
|
$this->assertEquals('untouched', file_get_contents($folderTwoB . DS . 'fileB.php'));
|
||||||
|
$this->assertFalse(file_exists($fileOne));
|
||||||
|
$this->assertFalse(file_exists($folderOneA));
|
||||||
|
$this->assertFalse(file_exists($fileOneA));
|
||||||
|
|
||||||
$Folder = new Folder($path);
|
$Folder = new Folder($path);
|
||||||
$Folder->delete();
|
$Folder->delete();
|
||||||
|
|
|
@ -21,6 +21,30 @@
|
||||||
*/
|
*/
|
||||||
class Folder {
|
class Folder {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default scheme for Folder::copy
|
||||||
|
* Recursively merges subfolders with the same name
|
||||||
|
*
|
||||||
|
* @constant MERGE
|
||||||
|
*/
|
||||||
|
const MERGE = 'merge';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overwrite scheme for Folder::copy
|
||||||
|
* subfolders with the same name will be replaced
|
||||||
|
*
|
||||||
|
* @constant OVERWRITE
|
||||||
|
*/
|
||||||
|
const OVERWRITE = 'overwrite';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skip scheme for Folder::copy
|
||||||
|
* if a subfolder with the same name exists it will be skipped
|
||||||
|
*
|
||||||
|
* @constant SKIP
|
||||||
|
*/
|
||||||
|
const SKIP = 'skip';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to Folder.
|
* Path to Folder.
|
||||||
*
|
*
|
||||||
|
@ -594,6 +618,7 @@ class Folder {
|
||||||
* - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
|
* - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
|
||||||
* - `mode` The mode to copy the files/directories with.
|
* - `mode` The mode to copy the files/directories with.
|
||||||
* - `skip` Files/directories to skip.
|
* - `skip` Files/directories to skip.
|
||||||
|
* - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
|
||||||
*
|
*
|
||||||
* @param array|string $options Either an array of options (see above) or a string of the destination directory.
|
* @param array|string $options Either an array of options (see above) or a string of the destination directory.
|
||||||
* @return boolean Success
|
* @return boolean Success
|
||||||
|
@ -608,7 +633,7 @@ class Folder {
|
||||||
$to = $options;
|
$to = $options;
|
||||||
$options = array();
|
$options = array();
|
||||||
}
|
}
|
||||||
$options = array_merge(array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array()), $options);
|
$options = array_merge(array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array(), 'scheme' => Folder::MERGE), $options);
|
||||||
|
|
||||||
$fromDir = $options['from'];
|
$fromDir = $options['from'];
|
||||||
$toDir = $options['to'];
|
$toDir = $options['to'];
|
||||||
|
@ -630,10 +655,10 @@ class Folder {
|
||||||
|
|
||||||
$exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
|
$exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
|
||||||
if ($handle = @opendir($fromDir)) {
|
if ($handle = @opendir($fromDir)) {
|
||||||
while (false !== ($item = readdir($handle))) {
|
while (($item = readdir($handle)) !== false) {
|
||||||
if (!in_array($item, $exceptions)) {
|
$to = Folder::addPathElement($toDir, $item);
|
||||||
$from = Folder::addPathElement($fromDir, $item);
|
if (($options['scheme'] != Folder::SKIP || !is_dir($to)) && !in_array($item, $exceptions)) {
|
||||||
$to = Folder::addPathElement($toDir, $item);
|
$from = Folder::addPathElement($fromDir, $item);
|
||||||
if (is_file($from)) {
|
if (is_file($from)) {
|
||||||
if (copy($from, $to)) {
|
if (copy($from, $to)) {
|
||||||
chmod($to, intval($mode, 8));
|
chmod($to, intval($mode, 8));
|
||||||
|
@ -643,6 +668,10 @@ class Folder {
|
||||||
$this->_errors[] = __d('cake_dev', '%s NOT copied to %s', $from, $to);
|
$this->_errors[] = __d('cake_dev', '%s NOT copied to %s', $from, $to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_dir($from) && file_exists($to) && $options['scheme'] == Folder::OVERWRITE) {
|
||||||
|
$this->delete($to);
|
||||||
|
}
|
||||||
|
|
||||||
if (is_dir($from) && !file_exists($to)) {
|
if (is_dir($from) && !file_exists($to)) {
|
||||||
$old = umask(0);
|
$old = umask(0);
|
||||||
|
@ -657,6 +686,9 @@ class Folder {
|
||||||
} else {
|
} else {
|
||||||
$this->_errors[] = __d('cake_dev', '%s not created', $to);
|
$this->_errors[] = __d('cake_dev', '%s not created', $to);
|
||||||
}
|
}
|
||||||
|
} elseif (is_dir($from) && $options['scheme'] == Folder::MERGE) {
|
||||||
|
$options = array_merge($options, array('to' => $to, 'from' => $from));
|
||||||
|
$this->copy($options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -680,8 +712,9 @@ class Folder {
|
||||||
* - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
|
* - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
|
||||||
* - `chmod` The mode to copy the files/directories with.
|
* - `chmod` The mode to copy the files/directories with.
|
||||||
* - `skip` Files/directories to skip.
|
* - `skip` Files/directories to skip.
|
||||||
|
* - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
|
||||||
*
|
*
|
||||||
* @param array $options (to, from, chmod, skip)
|
* @param array $options (to, from, chmod, skip, scheme)
|
||||||
* @return boolean Success
|
* @return boolean Success
|
||||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::move
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::move
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue