6.7 Implementing the RecursiveIterator Interface

 <  Day Day Up  >  

The RecursiveIterator interface extends the existing Iterator interface to include two additional methods : hasChildren( ) and getChildren( ) . These methods are used to navigate through sublists.

If you have an existing Iterator , it's natural to extend it to include the additional methods. SPL actually comes with a RecursiveDirectoryIterator , so there's no need to add these methods to DirectoryIterator , but it's a useful exercise to see how these methods should behave.

Here's how it's done:

 class MyRecursiveDirectoryIterator extends MyDirectoryIterator                                     implements RecursiveIterator {     protected function getPath( ) {         return $this->path . DIRECTORY_SEPARATOR . $this->current( );     }     public function hasChildren( ) {         return is_dir($this->getPath( ));     }          public function getChildren( ) {         return new MyRecursiveDirectoryIterator($this->getPath( ));     } } 

Wow! This is much shorter than the code that was needed for iterate_dir( ) . Since you can inherit methods from your earlier iterator, the implementation is three one-line methods.

The first method, getPath( ) , constructs a complete pathname from the original path and the current filename. It uses the $path property that was stored in the constructor for MyDirectoryIterator in Example 6-1 because I knew you'd need it now.

The other two methods required by the interface also turn out to be easy to code. A file has children if it's a directory, so hasChildren( ) only needs to check the return value of is_dir( ) .

Likewise, you can create a child iterator for a directory by instantiating a new instance of MyRecursiveDirectoryIterator and using the subdirectory's path in the constructor. This is analogous to calling iterate_dir( ) inside of iterate_dir( ) .

Notice how there's no need to keep track of where you've been, what files you've seen, and when a directory needs to be processed . This is all taken care of by the RecursiveIteratorIterator and is another major advantage of iterators.

If you actually run this code, however, it doesn't work:

 $dir = new DirectoryTreeIterator(        new MyRecursiveDirectoryIterator('/www/www.example.com/'), true); foreach ($dir as $file) {     print "$file\n"; } 

You end up with an amusing series of warnings, including this:

  PHP Warning:  opendir(././././././././././././././././././././././././   ./././././././././././././././././././././././././././././././././././   ./././././././././././././././././././././././././././././././././././   ./././././././././././././././././././././././././././././././././././   ./././././././././././././././././././././././././././././././././././   ./././././././././././././././././././././././././././././././././././   ./././././././././././././././././././././././././././././././././././   ./././././././././././././././.): failed to open dir: Too many open   files  

PHP just spirals off into an endless cycle. The directory files . and .. cause an infinite loop as the same directory is opened repeatedly, again and again.

It turns out the best solution is eliminating those two files completely. This requires using one new method and redefining two more:

 public function isDot( ) {         return ($this->current( ) =  = '.'                  $this->current( ) =  = '..');          }     public function rewind( ) {         rewinddir($this->dir);         $this->key = 0;         while ($this->valid = $this->readDir( ) and                 $this->isDot( )) {  }     }     public function next( ) {         $this->key++;         while ($this->valid = $this->readDir( ) and                 $this->isDot( )) {  }     } 

The isDot( ) method returns true if a file is one of the two nasty directory links. The method is used in rewind( ) and next( ) to detect those files and cause the iterator to skip over them. These redefined methods no longer retrieve just a single file using readDir( ) , but use a while loop to keep retrieving files until they get one that isn't a dot.

Everything now works as expected:

 $dir = new DirectoryTreeIterator(     new MyRecursiveDirectoryIterator('/www/www.example.com/'), true); foreach ($dir as $file) {     print "$file\n"; }  -email.html   -images   -logo.gif   -php.gif   -includes   -auth.inc   -user.inc   -index.html   -search.html  

Most recursive iterators don't contain these special self-referential aliases, so they don't need to go through the complicated steps necessary in this example. Directories are a unique case.

 <  Day Day Up  >  


Upgrading to PHP 5
Upgrading to PHP 5
ISBN: 0596006365
EAN: 2147483647
Year: 2004
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net