I am trying to iterate over a directory containing many PHP files and determine which classes are defined in each file.
Consider the following:
$php_files_and_content = new PhpFileAndContentIterator($dir);
foreach($php_files_and_content as $filepath => $sourceCode) {
}
The above variable $php_files_and_contentis an iterator, where the key is the path to the file, and the contents are the source code of the file (as if it were not obvious from the example).
Then it is passed to another iterator that will match all the defined classes in the source code, ala:
class DefinedClassDetector extends FilterIterator implements RecursiveIterator {
public function accept() {
return $this->hasChildren();
}
public function hasChildren() {
$classes = getDefinedClasses($this->current());
return !empty($classes);
}
public function getChildren() {
return new RecursiveArrayIterator(getDefinedClasses($this->current()));
}
}
$defined_classes = new RecursiveIteratorIterator(new DefinedClassDetector($php_files_and_content));
foreach($defined_classes as $index => $class) {
}
, $index , , "Class C" , , , 0. RecursiveIteratorIterator, Iterator (, , /).
, , , , , , - ( $defined_classes iterator) value - , ala:
foreach($classes_and_paths as $filepath => $class) {
}
, .
, , RecursiveIterator, current(), (), ) key() iterator(). , :
.
, , Iterators , , , , .