Browse Source

add a method to get all subdirectories of a folder

frankfoerster 9 years ago
parent
commit
a74d0567e4
2 changed files with 67 additions and 1 deletions
  1. 29 0
      src/Filesystem/Folder.php
  2. 38 1
      tests/TestCase/Filesystem/FolderTest.php

+ 29 - 0
src/Filesystem/Folder.php

@@ -484,6 +484,35 @@ class Folder
     }
 
     /**
+     * Returns an array of subdirectories for the provided or current path.
+     *
+     * @param string|null $path The directory path to get subdirectories for.
+     * @param bool $fullPath Wheter to return the full path or only the directory name.
+     * @return array Array of subdirectories for the provided or current path.
+     */
+    public function subdirectories($path = null, $fullPath = true)
+    {
+        if (!$path) {
+            $path = $this->path;
+        }
+        $subdirectories = [];
+
+        try {
+            $iterator = new DirectoryIterator($path);
+        } catch (Exception $e) {
+            return [];
+        }
+
+        foreach ($iterator as $item) {
+            if (!$item->isDir() || $item->isDot()) {
+                continue;
+            }
+            $subdirectories[] = $fullPath ? $item->getRealPath() : $item->getFilename();
+        }
+        return $subdirectories;
+    }
+
+    /**
      * Returns an array of nested directories and files in each directory
      *
      * @param string|null $path the directory path to build the tree from

+ 38 - 1
tests/TestCase/Filesystem/FolderTest.php

@@ -428,6 +428,43 @@ class FolderTest extends TestCase
     }
 
     /**
+     * testFolderSubdirectories method
+     *
+     * @return void
+     */
+    public function testFolderSubdirectories()
+    {
+        $path = CAKE . 'Network';
+        $folder = new Folder($path);
+
+        $expected = [
+            $path . DS . 'Exception',
+            $path . DS . 'Http',
+            $path . DS . 'Session'
+        ];
+        $result = $folder->subdirectories();
+        $this->assertEquals($expected, $result);
+        $result = $folder->subdirectories($path);
+        $this->assertEquals($expected, $result);
+
+        $expected = [
+            'Exception',
+            'Http',
+            'Session'
+        ];
+        $result = $folder->subdirectories(null, false);
+        $this->assertEquals($expected, $result);
+        $result = $folder->subdirectories($path, false);
+        $this->assertEquals($expected, $result);
+
+        $expected = [];
+        $result = $folder->subdirectories('NonExistantPath');
+        $this->assertEquals($expected, $result);
+        $result = $folder->subdirectories($path . DS . 'Exception');
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
      * testFolderTree method
      *
      * @return void
@@ -1235,7 +1272,7 @@ class FolderTest extends TestCase
         $Folder = new Folder($path);
         $Folder->delete();
     }
-    
+
     public function testMoveWithoutRecursive()
     {
         extract($this->_setupFilesystem());