directorytreeiterator.inc 1.2 KB

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