[BUGFIX] Use recursive option of mkdir in t3lib_div::mkdir_deep
authorChristian Kuhn <lolli@schwarzbu.ch>
Sun, 24 Apr 2011 16:22:29 +0000 (18:22 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 28 Apr 2011 08:41:02 +0000 (10:41 +0200)
Additionally throw exceptions instead of a return string if directory could
not be created.

Change-Id: I90da96e1b62d28997573d821c62b7b24b3ef2ba3
Resolves: #26240
Depends: #26230
Releases: 4.6
Reviewed-on: http://review.typo3.org/1716
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
t3lib/class.t3lib_div.php
tests/t3lib/class.t3lib_divTest.php

index 1b42bc1..e484e4b 100644 (file)
@@ -3069,24 +3069,45 @@ final class t3lib_div {
 
        /**
         * Creates a directory - including parent directories if necessary and
-        * fixes permissions on newly created directories.
-        *
-        * @param string $baseDirectory Base directory. This must exist! Must have trailing slash!
-        *              Example: "/root/typo3site/"
-        * @param string $deepDirectory Directory to create.
-        *              Expample: "xx/yy/" which creates "/root/typo3site/xx/yy/" if $destination is "/root/typo3site/"
-        * @return void/string Error string if error occured
-        */
-       public static function mkdir_deep($baseDirectory, $deepDirectory) {
-               $directories = self::trimExplode('/', $deepDirectory, 1);
-               $currentPath = '';
-               foreach ($directories as $directory) {
-                       $currentPath .= $directory . '/';
-                       if (!is_dir($baseDirectory . $currentPath)) {
-                               $result = self::mkdir($baseDirectory . $currentPath);
-                               if (!$result) {
-                                       return 'Error: The directory "' . $baseDirectory . $currentPath . '" could not be created...';
-                               }
+        * sets permissions on newly created directories.
+        *
+        * @param string $directory Target directory to create. Must a have trailing slash
+        *              if second parameter is given!
+        *              Example: "/root/typo3site/typo3temp/foo/"
+        * @param string $deepDirectory Directory to create. This second parameter
+        *              is kept for backwards compatibility since 4.6 where this method
+        *              was split into a base directory and a deep directory to be created.
+        *              Example: "xx/yy/" which creates "/root/typo3site/xx/yy/" if $directory is "/root/typo3site/"
+        * @return void
+        * @throws \InvalidArgumentException If $directory or $deepDirectory are not strings
+        * @throws \RuntimeException If directory could not be created
+        */
+       public static function mkdir_deep($directory, $deepDirectory = '') {
+               if (!is_string($directory)) {
+                       throw new \InvalidArgumentException(
+                               'The specified directory is of type "' . gettype($directory) . '" but a string is expected.',
+                               1303662955
+                       );
+               }
+               if (!is_string($deepDirectory)) {
+                       throw new \InvalidArgumentException(
+                               'The specified directory is of type "' . gettype($deepDirectory) . '" but a string is expected.',
+                               1303662956
+                       );
+               }
+
+               $fullPath = $directory . $deepDirectory;
+               if (!is_dir($fullPath) && strlen($fullPath) > 0) {
+                       @mkdir(
+                               $fullPath,
+                               octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']),
+                               TRUE
+                       );
+                       if (!is_dir($fullPath)) {
+                               throw new \RuntimeException(
+                                       'Could not create directory!',
+                                       1170251400
+                               );
                        }
                }
        }
@@ -5915,4 +5936,4 @@ final class t3lib_div {
        }
 }
 
-?>
\ No newline at end of file
+?>
index 9f9a2ac..5044174 100644 (file)
@@ -2011,13 +2011,13 @@ class t3lib_divTest extends tx_phpunit_testcase {
                        $this->markTestSkipped('mkdirDeepFixesPermissionsOnNewDirectory() test not available on Windows.');
                }
 
-               $directory = uniqid('test_');
-               $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
+               $directory = uniqid('mkdirdeeptest_');
+               $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0750';
                t3lib_div::mkdir_deep(PATH_site . 'typo3temp/', $directory);
                clearstatcache();
-               $resultDirectoryPermissions = substr(decoct(fileperms(PATH_site . 'typo3temp/' . $directory)), 2);
+               $resultDirectoryPermissions = substr(decoct(fileperms(PATH_site . 'typo3temp/' . $directory)), -3, 3);
                @rmdir(PATH_site . 'typo3temp/' . $directory);
-               $this->assertEquals($resultDirectoryPermissions, '0770');
+               $this->assertEquals($resultDirectoryPermissions, '750');
        }
 
        /**
@@ -2055,6 +2055,30 @@ class t3lib_divTest extends tx_phpunit_testcase {
                $this->assertTrue(is_dir('vfs://' . $baseDirectory . '/sub'));
        }
 
+       /**
+        * @test
+        * @expectedException \RuntimeException
+        */
+       public function mkdirDeepThrowsExceptionIfDirectoryCreationFails() {
+               t3lib_div::mkdir_deep('http://localhost');
+       }
+
+       /**
+        * @test
+        * @expectedException \InvalidArgumentException
+        */
+       public function mkdirDeepThrowsExceptionIfBaseDirectoryIsNotOfTypeString() {
+               t3lib_div::mkdir_deep(array());
+       }
+
+       /**
+        * @test
+        * @expectedException \InvalidArgumentException
+        */
+       public function mkdirDeepThrowsExceptionIfDeepDirectoryIsNotOfTypeString() {
+               t3lib_div::mkdir_deep(PATH_site . 'typo3temp/foo', array());
+       }
+
 
        ///////////////////////////////
        // Tests concerning unQuoteFilenames