Config.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace ClassPreloader;
  3. use Parser\AbstractNodeVisitor;
  4. /**
  5. * Class loader configuration object
  6. */
  7. class Config implements \IteratorAggregate
  8. {
  9. /**
  10. * @var array Array of AbstractNodeVisitor objects that visit nodes
  11. */
  12. protected $visitors = array();
  13. /**
  14. * @var array Array of file names
  15. */
  16. protected $filenames = array();
  17. /**
  18. * @var array Array of exclusive filters
  19. */
  20. protected $exclusiveFilters = array();
  21. /**
  22. * @var array Array of inclusive filters
  23. */
  24. protected $inclusiveFilters = array();
  25. /**
  26. * Set the filenames owned by the config
  27. *
  28. * @param array $filenames File name
  29. *
  30. * @return self
  31. */
  32. public function addFile($filename)
  33. {
  34. $this->filenames[] = $filename;
  35. return $this;
  36. }
  37. /**
  38. * Get an array of file names that satisfy any added filters
  39. *
  40. * @return array
  41. */
  42. public function getFilenames()
  43. {
  44. $filenames = array();
  45. foreach ($this->filenames as $f) {
  46. foreach ($this->inclusiveFilters as $filter) {
  47. if (!preg_match($filter, $f)) {
  48. continue 2;
  49. }
  50. }
  51. foreach ($this->exclusiveFilters as $filter) {
  52. if (preg_match($filter, $f)) {
  53. continue 2;
  54. }
  55. }
  56. $filenames[] = $f;
  57. }
  58. return $filenames;
  59. }
  60. /**
  61. * Get an iterator for the filenames
  62. *
  63. * @return \ArrayIterator
  64. */
  65. public function getIterator()
  66. {
  67. return new \ArrayIterator($this->getFilenames());
  68. }
  69. /**
  70. * Add a filter used to filter out classes matching a specific pattern
  71. *
  72. * @param string $pattern Regular expression pattern
  73. *
  74. * @return self
  75. */
  76. public function addExclusiveFilter($pattern)
  77. {
  78. $this->exclusiveFilters[] = $pattern;
  79. return $this;
  80. }
  81. /**
  82. * Add a filter used to grab only file names matching the pattern
  83. *
  84. * @param string $pattern Regular expression pattern
  85. *
  86. * @return self
  87. */
  88. public function addInclusiveFilter($pattern)
  89. {
  90. $this->inclusiveFilters[] = $pattern;
  91. return $this;
  92. }
  93. /**
  94. * Add a visitor that will visit each node when traversing the node list
  95. * of each file.
  96. *
  97. * @param AbstractNodeVisitor $visitor Node visitor
  98. *
  99. * @return self
  100. */
  101. public function addVisitor(AbstractNodeVisitor $visitor)
  102. {
  103. $this->visitors[] = $visitor;
  104. return $this;
  105. }
  106. /**
  107. * Get an array of node visitors
  108. *
  109. * @return array
  110. */
  111. public function getVisitors()
  112. {
  113. return $this->visitors;
  114. }
  115. }