public function syncFiles($options = array())
{
$options = array_merge($this->options, $options);
// We're syncing and potentially deleting folders, so make sure
// we were passed something - make it a little harder to accidentally
// trash your site
if (!strlen(trim($options['file_base_path']))) {
throw new \Exception("file_base_path option looks empty, bailing out");
}
if (!strlen(trim($options['from_folder']))) {
throw new \Exception("from_folder option looks empty, bailing out");
}
if (!strlen(trim($options['to_folder']))) {
throw new \Exception("to_folder option looks empty, bailing out");
}
$from = $options['file_base_path'] . '/' . $options['from_folder'];
$to = $options['file_base_path'] . '/' . $options['to_folder'];
if (file_exists($from)) {
if (isset($options['create_to_folder']) && $options['create_to_folder']) {
@mkdir($to, 0777, true);
} elseif (!file_exists($to)) {
throw new \Exception("to_folder does not exist");
}
$result = null;
system("rsync -a --delete " . escapeshellarg($from . '/') . " " . escapeshellarg($to), $result);
if ($result !== 0) {
throw new \Exception("Sync failed with errorcode '{$result}'!");
}
if (isset($options['remove_from_folder']) && $options['remove_from_folder']) {
system("rm -rf " . escapeshellarg($from));
}
} else {
// A missing from_folder is not an error. This is commonly the case
// when syncing from something that has nothing attached to it yet, etc.
}
}