Browse Source

refactored FileHelper::copyDirectory()

tags/2.0.0-alpha
Qiang Xue 12 years ago
parent
commit
a3d058574e
  1. 33
      framework/helpers/base/FileHelper.php

33
framework/helpers/base/FileHelper.php

@ -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
* *
* - 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
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);
} }
} }
} }

Loading…
Cancel
Save