title: Combining recursive iterator results: children with parents
body: <p>I'm trying to iterate over a directory which contains loads of PHP files, and detect what classes are defined in each file.</p>
  
  <p>Consider the following:</p>
  
  <pre><code>$php_files_and_content = new PhpFileAndContentIterator($dir);
  foreach($php_files_and_content as $filepath =&gt; $sourceCode) {
      // echo $filepath, $sourceCode
  }
  </code></pre>
  
  <p>The above <code>$php_files_and_content</code> variable represents an iterator where the key is the filepath, and the content is the source code of the file (as if that wasn't obvious from the example).</p>
  
  <p>This is then supplied into another iterator which will match all the defined classes in the source code, ala:</p>
  
  <pre><code>class DefinedClassDetector extends FilterIterator implements RecursiveIterator {
      public function accept() {
          return $this-&gt;hasChildren();
      }
  
      public function hasChildren() {
          $classes = getDefinedClasses($this-&gt;current());
          return !empty($classes);
      }
  
      public function getChildren() {
          return new RecursiveArrayIterator(getDefinedClasses($this-&gt;current()));
      }
  }
  
  $defined_classes = new RecursiveIteratorIterator(new DefinedClassDetector($php_files_and_content));
  
  foreach($defined_classes as $index =&gt; $class) {
      // print "$index =&gt; $class"; outputs:
      // 0 =&gt; Class A
      // 1 =&gt; Class B
      // 0 =&gt; Class C
  }
  </code></pre>
  
  <p>The reason the <code>$index</code> isn't sequential numerically is because 'Class C' was defined in the second source code file, and thus the array returned starts from index 0 again. This is preserved in the RecursiveIteratorIterator because each set of results represents a separate Iterator (and thus key/value pairs).</p>
  
  <p>Anyway, what I am trying to do now is find the best way to combine these, such that when I iterate over the new iterator, I can get the key is the class name (from the <code>$defined_classes</code> iterator) and the value is the original file path, ala:</p>
  
  <pre><code>foreach($classes_and_paths as $filepath =&gt; $class) {
      // print "$class =&gt; $filepath"; outputs
      // Class A =&gt; file1.php
      // Class B =&gt; file1.php
      // Class C =&gt; file2.php
  }
  </code></pre>
  
  <p>And that's where I'm stuck thus far.</p>
  
  <p>At the moment, the only solution that is coming to mind is to create a new RecursiveIterator, that overrides the current() method to return the outer iterator key() (which would be the original filepath), and key() method to return the current iterator() value. But I'm not favouring this solution because:</p>
  
  <ul>
  <li>It sounds complex (which means the code will look hideous and it won't be intuitive</li>
  <li>The business rules are hard-coded inside the class, whereas I would like to define some generic Iterators and be able to combine them in such a way to produce the required result.</li>
  </ul>
  
  <p>Any ideas or suggestions gratefully recieved.</p>
  
  <p>I also realise there are far faster, more efficient ways of doing this, but this is also an exercise in using Iterators for myselfm and also an exercise in promoting code reuse, so any new Iterators that have to be written should be as minimal as possible and try to leverage existing functionality.</p>
  
  <p>Thanks</p>