From 8f7a783757f41b4ae1ea555a7502ebedbb4a83c3 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Mon, 10 Jun 2013 22:15:03 +0300 Subject: [PATCH] Method "yii\helpers\base\FileHelper::mkdir()" has been added. --- framework/yii/helpers/base/FileHelper.php | 22 ++++++++++++++++++++++ tests/unit/framework/helpers/FileHelperTest.php | 12 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/framework/yii/helpers/base/FileHelper.php b/framework/yii/helpers/base/FileHelper.php index 27088a6..7fa87d5 100644 --- a/framework/yii/helpers/base/FileHelper.php +++ b/framework/yii/helpers/base/FileHelper.php @@ -288,4 +288,26 @@ class FileHelper return false; } } + + /** + * Shared environment safe version of mkdir. Supports recursive creation. + * For avoidance of umask side-effects chmod is used. + * + * @param string $path path to be created. + * @param integer $mode the permission to be set for created directory. If not set 0777 will be used. + * @param boolean $recursive whether to create directory structure recursive if parent dirs do not exist. + * @return boolean result of mkdir. + * @see mkdir + */ + public static function mkdir($path, $mode = null, $recursive = false) + { + $prevDir = dirname($path); + if ($recursive && !is_dir($path) && !is_dir($prevDir)) { + static::mkdir(dirname($path), $mode, true); + } + $mode = isset($mode) ? $mode : 0777; + $result = mkdir($path, $mode); + chmod($path, $mode); + return $result; + } } diff --git a/tests/unit/framework/helpers/FileHelperTest.php b/tests/unit/framework/helpers/FileHelperTest.php index 8de45c4..e3bd343 100644 --- a/tests/unit/framework/helpers/FileHelperTest.php +++ b/tests/unit/framework/helpers/FileHelperTest.php @@ -266,4 +266,16 @@ class FileHelperTest extends TestCase $foundFiles = FileHelper::findFiles($dirName, $options); $this->assertEquals(array($dirName . DIRECTORY_SEPARATOR . $fileName), $foundFiles); } + + public function testMkdir() { + $basePath = $this->testFilePath; + + $dirName = $basePath . DIRECTORY_SEPARATOR . 'test_dir'; + FileHelper::mkdir($dirName); + $this->assertTrue(file_exists($dirName), 'Unable to create directory!'); + + $dirName = $basePath . DIRECTORY_SEPARATOR . 'test_dir_level_1' . DIRECTORY_SEPARATOR . 'test_dir_level_2'; + FileHelper::mkdir($dirName, null, true); + $this->assertTrue(file_exists($dirName), 'Unable to create directory recursively!'); + } } \ No newline at end of file