-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CFileHelper::copyDirectory recursive mkdir. #1146
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ public static function getExtension($path) | |
|
||
/** | ||
* Copies a directory recursively as another. | ||
* If the destination directory does not exist, it will be created. | ||
* If the destination directory does not exist, it will be created recursively. | ||
* @param string $src the source directory | ||
* @param string $dst the destination directory | ||
* @param array $options options for directory copy. Valid options are: | ||
|
@@ -55,6 +55,9 @@ public static function copyDirectory($src,$dst,$options=array()) | |
$exclude=array(); | ||
$level=-1; | ||
extract($options); | ||
if(!is_dir($dst)) | ||
self::mkdir($dst, $options, true); | ||
|
||
self::copyDirectoryRecursive($src,$dst,'',$fileTypes,$exclude,$level,$options); | ||
} | ||
|
||
|
@@ -110,11 +113,8 @@ public static function findFiles($dir,$options=array()) | |
protected static function copyDirectoryRecursive($src,$dst,$base,$fileTypes,$exclude,$level,$options) | ||
{ | ||
if(!is_dir($dst)) | ||
mkdir($dst); | ||
if(isset($options['newDirMode'])) | ||
@chmod($dst,$options['newDirMode']); | ||
else | ||
@chmod($dst,0777); | ||
self::mkdir($dst, $options, false); | ||
|
||
$folder=opendir($src); | ||
while(($file=readdir($folder))!==false) | ||
{ | ||
|
@@ -128,7 +128,7 @@ protected static function copyDirectoryRecursive($src,$dst,$base,$fileTypes,$exc | |
{ | ||
copy($path,$dst.DIRECTORY_SEPARATOR.$file); | ||
if(isset($options['newFileMode'])) | ||
@chmod($dst.DIRECTORY_SEPARATOR.$file, $options['newFileMode']); | ||
chmod($dst.DIRECTORY_SEPARATOR.$file, $options['newFileMode']); | ||
} | ||
elseif($level) | ||
self::copyDirectoryRecursive($path,$dst.DIRECTORY_SEPARATOR.$file,$base.'/'.$file,$fileTypes,$exclude,$level-1,$options); | ||
|
@@ -263,4 +263,27 @@ public static function getMimeTypeByExtension($file,$magicFile=null) | |
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Shared environment safe version of mkdir. Supports recursive creation. | ||
* For avoidance of umask side-effects chmod is used. | ||
* | ||
* @static | ||
* @param string $dst path to be created | ||
* @param array $options newDirMode element used, must contain access bitmask. | ||
* @param boolean $recursive | ||
* @return boolean result of mkdir | ||
* @see mkdir | ||
*/ | ||
private static function mkdir($dst, array $options, $recursive) | ||
{ | ||
$prevDir = dirname($dst); | ||
if ($recursive && !is_dir($dst) && !is_dir($prevDir)) self::mkdir(dirname($dst), $options, true); | ||
|
||
$mode = isset($options['newDirMode']) ? $options['newDirMode'] : 0777; | ||
$res = mkdir($dst, $mode); | ||
chmod($dst, $mode); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've read on PHP.net that using mkdir mode can cause troubles with fpm. If I'm understanding your question right. Alexander Makarov notifications@github.com wrote:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What kind of troubles exactly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, troubles was with umask: http://www.php.net/manual/en/function.umask.php (ive used it in other revisions). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, chmod is not affected with umask. so there are no side effects |
||
return $res; | ||
} | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In lines 266-268,282 please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the new methods mkdir() and getModeFromOptions(). They make simple code harder to read. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not agree, it removes code duplication, and we have a constant here. It must be in one place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I totally agree big chunk of duplicated code or code that are repeated many times should should be refactored with methods. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reader dont have to jump if method named right (exposes its intent). I can rename it to getAccessMode() for more readability and shortness. mkdir() and getAccessMode() I think must be in separate methods because its more error prone to duplicate that code (you can assume that this algorithm used once, and forget to look for copy of it) and makes class code shorter (more readable). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you argue? This piece of code is not required to provide the method.
Enough already to justify a bad refactoring, covering everything forever oblivious programmers. It is a myth. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Friendly. He argues because he believes strongly in his opinion. He does have some valid points, it's just that people value them and other aspects differently. Either case it is the core dev team that has the last word, naturally. Either you like what he did or reject it or pick it up and refactor it the way you want it to be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @creocoder please tell me more about "good refactoring". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @senz Good books about Refactoring will tell you more. Refactoring its not about idiotic duplicates eliminating. Its smart process. You have get:
You say:
You get more explain:
You say:
What do you expect on that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @creocoder I dont want to continue this arguing. You are not being constructive. Just giving insults. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
documentation for mode options is missing in this method, please copy them from protected method
copyDirectoryRecursive
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just noticed that all the options described in the docs of
copyDirectory
are completely ignored...!? Has nobody noticed that until now, or am I overlooking something? @yiisoft/core-developersThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ignored?
they get extracted on the line 57
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh damn, thanks for opening my eyes @mdomba :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are working fine as @mdomba mentioned. Used this method just recently.