Skip to content
Browse files

fixed formatting, added missing docs

  • Loading branch information...
1 parent e6892c6 commit 4545516540d3d1dac3e75322135c5991e16110f7 @samdark samdark committed
Showing with 51 additions and 55 deletions.
  1. +16 −14 framework/utils/CFileHelper.php
  2. +35 −41 tests/framework/utils/CFileHelperTest.php
View
30 framework/utils/CFileHelper.php
@@ -26,7 +26,7 @@ class CFileHelper
*/
public static function getExtension($path)
{
- return pathinfo($path, PATHINFO_EXTENSION);
+ return pathinfo($path,PATHINFO_EXTENSION);
}
/**
@@ -47,6 +47,8 @@ public static function getExtension($path)
* Level 0 means copying only the files DIRECTLY under the directory;
* level N means copying those directories that are within N levels.
* </li>
+ * <li>newDirMode - the permission to be set for newly copied directories (defaults to 0777);</li>
+ * <li>newFileMode - the permission to be set for newly copied files (defaults to the current environment setting).</li>
* </ul>
*/
public static function copyDirectory($src,$dst,$options=array())
@@ -56,7 +58,7 @@ public static function copyDirectory($src,$dst,$options=array())
$level=-1;
extract($options);
if(!is_dir($dst))
- self::mkdir($dst, $options, true);
+ self::mkdir($dst,$options,true);
self::copyDirectoryRecursive($src,$dst,'',$fileTypes,$exclude,$level,$options);
}
@@ -113,7 +115,7 @@ public static function findFiles($dir,$options=array())
protected static function copyDirectoryRecursive($src,$dst,$base,$fileTypes,$exclude,$level,$options)
{
if(!is_dir($dst))
- self::mkdir($dst, $options, false);
+ self::mkdir($dst,$options,false);
$folder=opendir($src);
while(($file=readdir($folder))!==false)
@@ -128,7 +130,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);
@@ -196,7 +198,7 @@ protected static function validatePath($base,$file,$isFile,$fileTypes,$exclude)
}
if(!$isFile || empty($fileTypes))
return true;
- if(($type=pathinfo($file, PATHINFO_EXTENSION))!=='')
+ if(($type=pathinfo($file,PATHINFO_EXTENSION))!=='')
return in_array($type,$fileTypes);
else
return false;
@@ -248,12 +250,12 @@ public static function getMimeType($file,$magicFile=null,$checkExtension=true)
*/
public static function getMimeTypeByExtension($file,$magicFile=null)
{
- static $extensions, $customExtensions=array();
+ static $extensions,$customExtensions=array();
if($magicFile===null && $extensions===null)
$extensions=require(Yii::getPathOfAlias('system.utils.mimeTypes').'.php');
elseif($magicFile!==null && !isset($customExtensions[$magicFile]))
$customExtensions[$magicFile]=require($magicFile);
- if(($ext=pathinfo($file, PATHINFO_EXTENSION))!=='')
+ if(($ext=pathinfo($file,PATHINFO_EXTENSION))!=='')
{
$ext=strtolower($ext);
if($magicFile===null && isset($extensions[$ext]))
@@ -275,15 +277,15 @@ public static function getMimeTypeByExtension($file,$magicFile=null)
* @return boolean result of mkdir
* @see mkdir
*/
- private static function mkdir($dst, array $options, $recursive)
+ 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);
+ $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);
+ $mode=isset($options['newDirMode']) ? $options['newDirMode'] : 0777;
+ $res=mkdir($dst, $mode);
+ chmod($dst,$mode);
return $res;
}
-
}
View
76 tests/framework/utils/CFileHelperTest.php
@@ -2,17 +2,16 @@
class CFileHelperTest extends CTestCase
{
private $testDir;
- private $testMode = 0770;
- private $rootDir1 = "test1";
- private $rootDir2 = "test2";
- private $subDir = 'sub';
- private $file = 'testfile';
+ private $testMode=0770;
+ private $rootDir1="test1";
+ private $rootDir2="test2";
+ private $subDir='sub';
+ private $file='testfile';
protected function setUp()
{
- $this->testDir = Yii::getPathOfAlias('application.runtime.CFileHelper');
- if(!is_dir($this->testDir) &&
- !(@mkdir($this->testDir)))
+ $this->testDir=Yii::getPathOfAlias('application.runtime.CFileHelper');
+ if(!is_dir($this->testDir) && !(@mkdir($this->testDir)))
$this->markTestIncomplete('Unit tests runtime directory should have writable permissions!');
// create temporary testing data files
@@ -27,14 +26,15 @@ protected function setUp()
protected function tearDown()
{
- if (is_dir($this->testDir)) $this->rrmdir($this->testDir);
+ if (is_dir($this->testDir))
+ $this->rrmdir($this->testDir);
}
public function testGetMimeTypeByExtension()
{
// run everything ten times in one test action to be sure that caching inside
// CFileHelper::getMimeTypeByExtension() is working the right way
- for($i=0; $i<10; $i++)
+ for($i=0;$i<10;$i++)
{
$this->assertNull(CFileHelper::getMimeTypeByExtension('test.txa'));
$this->assertNull(CFileHelper::getMimeTypeByExtension('test.txb'));
@@ -52,69 +52,63 @@ public function testGetMimeTypeByExtension()
public function testCopyDirectory_subDir_modeShoudBe0775()
{
- if (substr(PHP_OS, 0, 3) == 'WIN') {
+ if (substr(PHP_OS,0,3)=='WIN')
$this->markTestSkipped("Can't reliably test it on Windows because fileperms() always return 0777.");
- }
$this->createTestStruct($this->testDir);
- $src = $this->testDir . DIRECTORY_SEPARATOR . $this->rootDir1;
- $dst = $this->testDir . DIRECTORY_SEPARATOR . $this->rootDir2;
- CFileHelper::copyDirectory($src, $dst, array('newDirMode' => $this->testMode));
+ $src=$this->testDir.DIRECTORY_SEPARATOR.$this->rootDir1;
+ $dst=$this->testDir.DIRECTORY_SEPARATOR.$this->rootDir2;
+ CFileHelper::copyDirectory($src,$dst,array('newDirMode'=>$this->testMode));
- $subDir2Mode = $this->getMode($dst . DIRECTORY_SEPARATOR . $this->subDir );
- $expectedMode = sprintf('%o', $this->testMode);
- $this->assertEquals($expectedMode, $subDir2Mode, "Subdir mode is not {$expectedMode}");
+ $subDir2Mode=$this->getMode($dst.DIRECTORY_SEPARATOR.$this->subDir);
+ $expectedMode=sprintf('%o',$this->testMode);
+ $this->assertEquals($expectedMode,$subDir2Mode,"Subdir mode is not {$expectedMode}");
}
public function testCopyDirectory_subDir_modeShoudBe0777()
{
- if (substr(PHP_OS, 0, 3) == 'WIN') {
+ if (substr(PHP_OS,0,3)=='WIN')
$this->markTestSkipped("Can't reliably test it on Windows because fileperms() always return 0777.");
- }
$this->createTestStruct($this->testDir);
- $src = $this->testDir . DIRECTORY_SEPARATOR . $this->rootDir1;
- $dst = $this->testDir . DIRECTORY_SEPARATOR . $this->rootDir2;
- CFileHelper::copyDirectory($src, $dst);
+ $src=$this->testDir.DIRECTORY_SEPARATOR.$this->rootDir1;
+ $dst=$this->testDir.DIRECTORY_SEPARATOR.$this->rootDir2;
+ CFileHelper::copyDirectory($src,$dst);
- $subDir2Mode = $this->getMode($dst . DIRECTORY_SEPARATOR . $this->subDir );
- $expectedMode = sprintf('%o', 0777);
- $this->assertEquals($expectedMode, $subDir2Mode, "Subdir mode is not {$expectedMode}");
+ $subDir2Mode=$this->getMode($dst.DIRECTORY_SEPARATOR.$this->subDir);
+ $expectedMode=sprintf('%o',0777);
+ $this->assertEquals($expectedMode,$subDir2Mode,"Subdir mode is not {$expectedMode}");
}
private function createTestStruct($testDir)
{
- $rootDir = $testDir . DIRECTORY_SEPARATOR . $this->rootDir1;
+ $rootDir=$testDir.DIRECTORY_SEPARATOR.$this->rootDir1;
mkdir($rootDir);
- $subDir = $testDir . DIRECTORY_SEPARATOR . $this->rootDir1 . DIRECTORY_SEPARATOR . $this->subDir;
+ $subDir=$testDir.DIRECTORY_SEPARATOR.$this->rootDir1.DIRECTORY_SEPARATOR.$this->subDir;
mkdir($subDir);
- $file = $testDir . DIRECTORY_SEPARATOR . $this->rootDir1 . DIRECTORY_SEPARATOR . $this->subDir . DIRECTORY_SEPARATOR . $this->file;
- file_put_contents($file, '12321312');
+ $file=$testDir.DIRECTORY_SEPARATOR.$this->rootDir1.DIRECTORY_SEPARATOR.$this->subDir.DIRECTORY_SEPARATOR.$this->file;
+ file_put_contents($file,'12321312');
}
private function getMode($file)
{
- return substr(sprintf('%o', fileperms($file)), -4);
+ return substr(sprintf('%o',fileperms($file)),-4);
}
private function rrmdir($dir)
{
- if ($handle = opendir($dir))
+ if($handle=opendir($dir))
{
- while (false !== ($entry = readdir($handle)))
+ while(false!==($entry=readdir($handle)))
{
- if ($entry != "." && $entry != "..")
+ if($entry!="." && $entry!="..")
{
- if (is_dir($dir . "/" . $entry) === true)
- {
- $this->rrmdir($dir . "/" . $entry);
- }
+ if(is_dir($dir."/".$entry)===true)
+ $this->rrmdir($dir."/".$entry);
else
- {
- unlink($dir . "/" . $entry);
- }
+ unlink($dir."/".$entry);
}
}
closedir($handle);

0 comments on commit 4545516

Please sign in to comment.
Something went wrong with that request. Please try again.