Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    public function tree($path) {
        $folder = new Folder();
        $tree = $folder->tree($path);
        $offset = strlen($path . DS);
        $folders = $this->generateTree($tree[0], $offset);
        $files = $this->generateTree($tree[1], $offset, true);
        return array_merge_recursive($folders, $files);
    }
   
    private function generateTree($tree, $offset, $files = false) {
        $t = array();
        foreach ($tree as $current_path) {
            $current_path = substr($current_path, $offset);
            $_t = $this->nest(explode(DS, $current_path), $files);
            $t = array_merge_recursive($_t, $t);
        }
        return $t;
    }
   
    private function nest($paths, $files) {
        $count = count($paths);
        if ($count > 1) {
            return array(current($paths) => $this->nest(array_slice($paths, 1, $count), $files));
        } else {
            return $files ? array(current($paths)) : array(current($paths) => array());
        }
    }