Commit a3d05857 by Qiang Xue

refactored FileHelper::copyDirectory()

parent db2392cd
...@@ -124,7 +124,6 @@ class FileHelper ...@@ -124,7 +124,6 @@ class FileHelper
return null; return null;
} }
/** /**
* Copies a whole directory as another one. * Copies a whole directory as another one.
* The files and sub-directories will also be copied over. * The files and sub-directories will also be copied over.
...@@ -134,15 +133,12 @@ class FileHelper ...@@ -134,15 +133,12 @@ class FileHelper
* *
* - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0777. * - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0777.
* - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting. * - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting.
* - filter: callback, a PHP callback that is called for every sub-directory and file to * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file.
* determine if it should be copied. The signature of the callback should be: * If the callback returns false, the copy operation for the sub-directory or file will be cancelled.
* * The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or
* ~~~ * file to be copied from, while `$to` is the copy target.
* // $path is the file/directory path to be copied * - afterCopy: callback, a PHP callback that is called after a sub-directory or file is successfully copied.
* function ($path) { * The signature of the callback is similar to that of `beforeCopy`.
* // return a boolean indicating if $path should be copied
* }
* ~~~
*/ */
public static function copyDirectory($src, $dst, $options = array()) public static function copyDirectory($src, $dst, $options = array())
{ {
...@@ -155,16 +151,19 @@ class FileHelper ...@@ -155,16 +151,19 @@ class FileHelper
if ($file === '.' || $file === '..') { if ($file === '.' || $file === '..') {
continue; continue;
} }
$srcPath = $src . DIRECTORY_SEPARATOR . $file; $from = $src . DIRECTORY_SEPARATOR . $file;
if (!isset($options['filter']) || call_user_func($options['filter'], $srcPath)) { $to = $dst . DIRECTORY_SEPARATOR . $file;
$dstPath = $dst . DIRECTORY_SEPARATOR . $file; if (!isset($options['beforeCopy']) || call_user_func($options['beforeCopy'], $from, $to)) {
if (is_file($srcPath)) { if (is_file($from)) {
copy($srcPath, $dstPath); copy($from, $to);
if (isset($options['fileMode'])) { if (isset($options['fileMode'])) {
chmod($dstPath, $options['fileMode']); chmod($to, $options['fileMode']);
} }
} else { } else {
static::copyDirectory($srcPath, $dstPath, $options); static::copyDirectory($from, $to, $options);
}
if (isset($options['afterCopy'])) {
call_user_func($options['afterCopy'], $from, $to);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment