ClassList.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace ClassPreloader;
  3. /**
  4. * Maintains a list of classes using a sort of doubly-linked list
  5. */
  6. class ClassList
  7. {
  8. /**
  9. * @var ClassNode The head node of the list
  10. */
  11. protected $head;
  12. /**
  13. * @var ClassNode The current node of the list
  14. */
  15. protected $current;
  16. public function __construct()
  17. {
  18. $this->clear();
  19. }
  20. /**
  21. * Clear the contents of the list and reset the head node and current node
  22. */
  23. public function clear()
  24. {
  25. $this->head = new ClassNode(null);
  26. $this->current = $this->head;
  27. }
  28. /**
  29. * Traverse to the next node in the list
  30. */
  31. public function next()
  32. {
  33. if (isset($this->current->next)) {
  34. $this->current = $this->current->next;
  35. } else {
  36. $this->current->next = new ClassNode(null, $this->current);
  37. $this->current = $this->current->next;
  38. }
  39. }
  40. /**
  41. * Insert a value at the current position in the list. Any currently set
  42. * value at this position will be pushed back in the list after the new
  43. * value
  44. *
  45. * @param mixed $value Value to insert
  46. */
  47. public function push($value)
  48. {
  49. if (!$this->current->value) {
  50. $this->current->value = $value;
  51. } else {
  52. $temp = $this->current;
  53. $this->current = new ClassNode($value, $temp->prev);
  54. $this->current->next = $temp;
  55. $temp->prev = $this->current;
  56. if ($temp === $this->head) {
  57. $this->head = $this->current;
  58. } else {
  59. $this->current->prev->next = $this->current;
  60. }
  61. }
  62. }
  63. /**
  64. * Traverse the ClassList and return a list of classes
  65. *
  66. * @return array
  67. */
  68. public function getClasses()
  69. {
  70. $classes = array();
  71. $current = $this->head;
  72. while ($current && $current->value) {
  73. $classes[] = $current->value;
  74. $current = $current->next;
  75. }
  76. return array_filter($classes);
  77. }
  78. }