function testSyncDir() { if (file_exists(__DIR__ . "/synctest_copy")) { ZipDeploy::delTree(__DIR__ . "/synctest_copy"); } $dirSync = new DirSync(__DIR__ . "/synctest", __DIR__ . "/synctest_copy"); $dirSync->sync(); $this->assertTrue(file_exists(__DIR__ . "/synctest_copy")); $this->assertTrue(file_exists(__DIR__ . "/synctest_copy/file.txt")); $this->assertTrue(file_exists(__DIR__ . "/synctest_copy/another_file.txt")); file_put_contents(__DIR__ . "/synctest_copy/removeme.txt", "dummy"); file_put_contents(__DIR__ . "/synctest_copy/some_dir/removeme_too.txt", "dummy"); file_put_contents(__DIR__ . "/synctest_copy/some_dir/keepme.txt", "dummy"); $dirSync = new DirSync(__DIR__ . "/synctest", __DIR__ . "/synctest_copy"); $dirSync->addKeep("some_dir/keepme.txt"); $dirSync->sync(); $this->assertFalse(file_exists(__DIR__ . "/synctest_copy/removeme.txt")); $this->assertFalse(file_exists(__DIR__ . "/synctest_copy/some_dir/removeme_too.txt")); $this->assertTrue(file_exists(__DIR__ . "/synctest_copy/some_dir/keepme.txt")); if (file_exists(__DIR__ . "/synctest_copy")) { ZipDeploy::delTree(__DIR__ . "/synctest_copy"); } }
/** * Perform sync. */ public function sync($subdir = "", $keep = FALSE) { if ($this->isKeep($subdir)) { $keep = TRUE; } $targetPath = $this->to . "/" . $subdir; if (!file_exists($targetPath)) { $res = mkdir($targetPath); if (!$res) { throw new Exception("Unable to create: " . $targetPath); } } if (!is_dir($targetPath)) { throw new Exception("Incomming directory, but there is a file there: " . $targetPath); } $files = array_diff(scandir($this->from . "/" . $subdir), array('.', '..')); foreach ($files as $file) { if (is_dir($this->from . "/" . $subdir . "/" . $file)) { $this->sync($subdir . "/" . $file, $keep); } else { $res = copy($this->from . "/" . $subdir . "/" . $file, $this->to . "/" . $subdir . "/" . $file); if (!$res) { throw new Exception("Unable to copy: " . $this->from . "/" . $subdir . "/" . $file); } } } $existingsourcefiles = array_diff(scandir($this->from . "/" . $subdir), array('.', '..')); $existingdestfiles = array_diff(scandir($this->to . "/" . $subdir), array('.', '..')); $removefiles = array_diff($existingdestfiles, $existingsourcefiles); //print_r($removefiles); foreach ($removefiles as $removefile) { if (!$this->isKeep($subdir . "/" . $removefile) && !$keep) { ZipDeploy::delTree($this->to . "/" . $subdir . "/" . $removefile); } } }