php-src/ext/spl/examples/directorytreeiterator.inc

54 lines
1.2 KiB
PHP
Raw Normal View History

2003-11-18 22:18:38 +00:00
<?php
/** @file directorytreeiterator.inc
* @ingroup Examples
* @brief class DirectoryTreeIterator
* @author Marcus Boerger
2005-02-08 19:10:06 +00:00
* @date 2003 - 2005
*
* SPL - Standard PHP Library
*/
/** @ingroup Examples
* @brief DirectoryIterator to generate ASCII graphic directory trees
* @author Marcus Boerger
2006-01-04 15:34:39 +00:00
* @version 1.1
*/
2003-11-18 22:18:38 +00:00
class DirectoryTreeIterator extends RecursiveIteratorIterator
{
/** Construct from a path.
* @param $path directory to iterate
*/
2004-03-16 10:16:52 +00:00
function __construct($path)
2003-11-18 22:18:38 +00:00
{
2006-03-30 21:57:28 +00:00
parent::__construct(
new RecursiveCachingIterator(
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_FILENAME
),
CachingIterator::CALL_TOSTRING|CachingIterator::CATCH_GET_CHILD
),
parent::SELF_FIRST
);
2003-11-18 22:18:38 +00:00
}
/** @return the current element prefixed with ASCII graphics
*/
2003-11-18 22:18:38 +00:00
function current()
{
$tree = '';
2003-11-22 20:51:15 +00:00
for ($l=0; $l < $this->getDepth(); $l++) {
2003-11-18 22:34:51 +00:00
$tree .= $this->getSubIterator($l)->hasNext() ? '| ' : ' ';
2003-11-18 22:18:38 +00:00
}
return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-')
. $this->getSubIterator($l)->__toString();
2003-11-18 22:18:38 +00:00
}
/** Aggregates the inner iterator
*/
2003-11-22 20:51:15 +00:00
function __call($func, $params)
{
return call_user_func_array(array($this->getSubIterator(), $func), $params);
}
2003-11-18 22:18:38 +00:00
}
?>