1<?php
2
3/** @file directorytreeiterator.inc
4 * @ingroup Examples
5 * @brief class DirectoryTreeIterator
6 * @author  Marcus Boerger
7 * @date    2003 - 2008
8 *
9 * SPL - Standard PHP Library
10 */
11
12/** @ingroup Examples
13 * @brief   DirectoryIterator to generate ASCII graphic directory trees
14 * @author  Marcus Boerger
15 * @version 1.1
16 */
17class DirectoryTreeIterator extends RecursiveIteratorIterator
18{
19    /** Construct from a path.
20     * @param $path directory to iterate
21     */
22    function __construct($path)
23    {
24        parent::__construct(
25            new RecursiveCachingIterator(
26                new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_FILENAME
27                ),
28                CachingIterator::CALL_TOSTRING|CachingIterator::CATCH_GET_CHILD
29            ),
30            parent::SELF_FIRST
31        );
32    }
33
34    /** @return the current element prefixed with ASCII graphics
35     */
36    function current(): mixed
37    {
38        $tree = '';
39        for ($l=0; $l < $this->getDepth(); $l++) {
40            $tree .= $this->getSubIterator($l)->hasNext() ? '| ' : '  ';
41        }
42        return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-')
43                     . $this->getSubIterator($l)->__toString();
44    }
45
46    /** Aggregates the inner iterator
47     */
48    function __call($func, $params)
49    {
50        return call_user_func_array(array($this->getSubIterator(), $func), $params);
51    }
52}
53
54?>
55