/jay/net/xp_framework/tools/vm/util/AllClassesIterator.class.php

https://github.com/thekid/xp-experiments · PHP · 79 lines · 30 code · 10 blank · 39 comment · 2 complexity · 1a08ce31ae9b9ef9e18a45364c36a109 MD5 · raw file

  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses('util.XPIterator');
  7. /**
  8. * Iterates on all classes using an IOCollectionIterator
  9. *
  10. * @purpose Iterator
  11. */
  12. class AllClassesIterator extends Object implements XPIterator {
  13. var
  14. $aggregate = NULL,
  15. $classpath = '',
  16. $root = NULL;
  17. /**
  18. * Constructor
  19. *
  20. * @access public
  21. * @param io.collections.iterate.IOCollectionIterator aggregate
  22. * @param string classpath
  23. */
  24. function __construct($aggregate, $classpath) {
  25. $this->aggregate= $aggregate;
  26. $this->classpath= array_flip(array_map('realpath', explode(PATH_SEPARATOR, $classpath)));
  27. }
  28. /**
  29. * Helper method. Infers class name from an IOElement
  30. *
  31. * @access protected
  32. * @param io.IOElement element
  33. * @return string
  34. * @throws lang.IllegalArgumentException in case class name cannot be inferred
  35. */
  36. function classNameForElement($element) {
  37. $uri= realpath($element->getURI());
  38. $path= dirname($uri);
  39. while (FALSE !== ($pos= strrpos($path, DIRECTORY_SEPARATOR))) {
  40. if (isset($this->classpath[$path])) {
  41. return strtr(substr($uri, strlen($path)+ 1, -10), DIRECTORY_SEPARATOR, '.');
  42. }
  43. $path= substr($path, 0, $pos);
  44. }
  45. throw new IllegalArgumentException('Cannot infer classname from '.$element->toString());
  46. }
  47. /**
  48. * Returns true if the iteration has more elements. (In other words,
  49. * returns true if next would return an element rather than throwing
  50. * an exception.)
  51. *
  52. * @access public
  53. * @return bool
  54. */
  55. function hasNext() {
  56. return $this->aggregate->hasNext();
  57. }
  58. /**
  59. * Returns the next element in the iteration.
  60. *
  61. * @access public
  62. * @return mixed
  63. * @throws util.NoSuchElementException when there are no more elements
  64. */
  65. function next() {
  66. return $this->root->classNamed($this->classNameForElement($this->aggregate->next()));
  67. }
  68. }
  69. ?>