Iterating over life with SPL Iterators I: Directories

In the past i have already talked about SPL and how it makes PHP Developers’ life a breeze, since then i have felt a lack of SPL recipes around the web, if you are getting into SPL now, using some of the available classes can be a real mystery, so I decided to add more posts to google’s list of SPL articles. So this is the first in a line that i will be adding as i come by the examples.

Wouldn’t it be nice if you could go by life just applying a foreach to each year and life day by day? Ok, that was an awful joke, but using iterators does make life a lot easier and fun, and that’s without mentioning cleaner code. SPL’s iterator classes are really awesome and helpful, replacing multiple lines of code and a handful functions with a simple new this and a foreach can really help cleaning up code. Ok, i did get into an argument that this might make the code less legible to “beginner"programmers or programmers that are not familiar with iterators and such, but hey, if you can’t understand it, read this post and learn it.

In this article i want to go over some of SPL’s Directory Iteration options, following up with more details the code i posted in the original SPL article. So i will now dive into the infinity of iterators and iterate (sic) over them, showing how they “go together"and where to get them to solve things for you.

Native in SPL

Native SPL classes have been converted to C, so they perform much faster and are available in any PHP install, especially since in PHP 5.3 you cannot disable SPL anymore.

DirectoryIterator (doc )(doxygen )

This is a simple iterator, as in its not a recursive iterator but leave that for later so you don’t end up as dizzy as we endedup after the “Iteratah drinking game” in Tek'09. It basically replaces what you can do with the scandir function, but gives you a few more advantages on the way out. Basically you can pass it the directory you wish to iterate and it will return an object that you can foreach over as if it were an array. This is a simple task that can be done using scandir as well, so let’s compare advantages, first some code:

 <?php

echo '- Iterate diretory using scandir' . PHP\_EOL; echo '- Avoid DOT directories' . PHP\_EOL; echo '- Show full path' . PHP\_EOL; $dir = 'samples' . DIRECTORY\_SEPARATOR . 'sampledirtree'; $files = scandir( $dir ); foreach($files as $file){ if ($file != '.' || $file != '..'){ echo $dir . DIRECTORY\_SEPARATOR . $file . PHP\_EOL; } } ?> 

And same thing with DirectoryIterator

 <?php

echo '- Iterate directory using DirectoryIterator' . PHP\_EOL; echo '- Avoid DOT directories' . PHP\_EOL; echo '- Show full path' . PHP\_EOL; $files = new DirectoryIterator('samples' . DIRECTORY\_SEPARATOR . 'sampledirtree'); foreach($files as $file){ if (!$file->isDot()){ echo $file->getRealPath() . PHP\_EOL; } }

?> 

Output for both:

 - Iterate directory using (scandir|DirectoryIterator) - Avoid DOT directories - Show full path samples/sampledirtree/file1.txt samples/sampledirtree/folder1 samples/sampledirtree/folder2 

The code looks pretty much the same and we are basically performing a simple task, but one of the powerful built-in things about the DirectoryIterator is that instead of a plain string as scandir does, it returns a SplFileInfo Object, packed with a whole bunch of information goodness, thus it allows us to skip the “dot” files ( . and .. ) without testing for both and getting a file’s full real path without having to concatenate the actual directory and such, but it actually does more, check out the main methods list: (whole list )

  • getFilename ()
  • getOwner ()
  • getPath ()
  • getPathname ()
  • getPerms ()
  • getRealPath ()
  • getSize ()
  • getType ()
  • isDir ()
  • isExecutable ()
  • isFile ()
  • isLink ()
  • isReadable ()
  • isWritable ()
  • openFile ($mode= ‘r’, $use_include_path=false, $context=NULL)

Its arguable that these are all information you can get by calling a function, hey, this is OO, its cleaner and not procedural. So it makes for much cleaner code ad ease of use, you have a fully qualified object to handle a file right there, just a method call away. Its important to notice that this does come at a performance cost, but at less then 40% and measured in much less then microseconds, this is not a major thing to worry about.

RecursiveDirectoryIterator (doc )(doxygen )

This is where the fun begins, recursive goodness. So you noticed above that the script did not follow up on the folders it found, it stayed within the first level of the directory we chose, this is where recursiveness comes in. Basically this iterator will go into directories, executing DirectoryIterator on anything that is a directory. This is done by implementing the getChildren function which allows you to get a DirectoryIterator instance of the child directory.

Using regular scandir approach we would have to use a recursive function to obtain this behavior, but using this we only need to.. “wait, even with the getChildren function we still would need a recursive function to go through it, hey! someone lied to me!” .. This is where SPL composite magic comes in, we just need to use a RecursiveIteratorIterator (see how the drinking game begins to be fun?).

The RecursiveIteratorIterator is basically an object that implements the recursive function, but without the hassle and thinking needed, just pass a Recursive__Iterator to its construct and foreach away, it will automatically call the getChildren functions and manage that, and you can even tell it how to behave.

<?php

function recursiveScanDir($dir){ $files = scandir($dir); foreach($files as $file){ if ($file != '.' && $file != '..'){ if (is\_dir($dir . DIRECTORY\_SEPARATOR . $file)){ recursiveScanDir($dir . DIRECTORY\_SEPARATOR . $file); }else{ echo $dir . DIRECTORY\_SEPARATOR . $file . PHP\_EOL; } } } }

$dir = 'samples' . DIRECTORY\_SEPARATOR . 'sampledirtree'; recursiveScanDir($dir);

?>

Now using SPL stuff with 3.5 less lines of code:

<?php

$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator('samples' . DIRECTORY\_SEPARATOR . 'sampledirtree') ); foreach($files as $file){ echo $file->getPathname() . PHP\_EOL; }

?>

Output:

samples/sampledirtree/file1.txt samples/sampledirtree/folder1/file1.txt samples/sampledirtree/folder1/file2.html samples/sampledirtree/folder2/file1.html samples/sampledirtree/folder2/file2.txt

We used default settings here, but in case we manipulate the $mode property of the contract (2nd parameter), we can order it to for example, show children first, or “leaves” only, this is very useful. If you are not seeing it yet, imagine you want to remove a directory structure, you can’t just rmdir it cause it will fail due to files existing inside the folder, so you need to delete one by one following hierarchy. So if you use this iterator combination and ask it to show children first, you can then delete all children and afterward remove the parents, like in this code:

<?php //Recursively delete tree structure $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('samples' . DIRECTORY\_SEPARATOR . 'sampledirtree'), RecursiveIteratorIterator::CHILD\_FIRST); foreach($files as $file){ if ($file->isDir()){ rmdir($file->getRealPath()); }else{ unlink($file->getRealPath()); } } ?>

Obviously you might not see advantages between the SPL stuff and scandir in the basic stuff, but once you start adding operations to your iteration and begin needing specific behavior, like the delete example, you begin to realize it let’s you have much simpler and easily readable code, plus its OO! (i’m a big OO fan BTW)

Non-native in SPL

Non-native SPL clases are available currently as examples and some will be converted to C and integrated in the native part of SPL. Some are useful as examples and you can then implement them locally for your use, or you can load these examples into your code by one of two choices:

  • Add ext/spl/examples/autoload.inc to you php.ini in auto_prepend_file (or add it to the file already set in auto_prepend_file)
  • Include ext/spl/examples/autoload.inc in your application

The autoload.inc file is available in the folder above which should be in your PHP install or in the source code you can download from PHP.net. I would recommend downloading this and adding it into your application tree if you wish to use it.

Personal Recommendation: Use everything in the examples folder as inspiration to what you can do with SPL and implement it locally

DirectoryTreeIterator (doxygen )

The DirectoryTreeIterator is more interesting as an example of what you can do with the iterators as to actually be something you might use on a daily basis. It basically does what the RecursiveDirectoryIterator does but diplays the result as a ASCII directory tree, so using this code:

<?php set\_include\_path( get\_include\_path() . PATH\_SEPARATOR . 'spl' . DIRECTORY\_SEPARATOR . 'examples' ); include('spl' . DIRECTORY\_SEPARATOR . 'examples' . DIRECTORY\_SEPARATOR . 'autoload.inc');

$files = new DirectoryTreeIterator('samples' . DIRECTORY\_SEPARATOR . 'sampledirtree');

foreach($files as $file){ echo $file . PHP\_EOL; }

?>

We get this result:

|-samples/sampledirtree/file1.txt |-samples/sampledirtree/folder1 | |-samples/sampledirtree/folder1/file1.txt | \\-samples/sampledirtree/folder1/file2.html \\-samples/sampledirtree/folder2 |-samples/sampledirtree/folder2/file1.html \\-samples/sampledirtree/folder2/file2.txt

Since i said its more interesting as an example, let’s look at the actual source code of the class that does the printing:

 function current() { $tree = ''; for ($l=0; $l < $this->getDepth(); $l++) { $tree .= $this->getSubIterator($l)->hasNext() ? '| ' : ' '; } return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\\-') . $this->getSubIterator($l)->\_\_toString(); }

As you can see, its just a matter of working the ASCII to images and css and you can very easily have a directory tree anywhere on your site, just taking advantage of the RecursiveDirectoryIterator.

End of Part I…

This is a brief overview of what you can do with all the Directory Iterators available in SPL. Combining these directory iterators with other navigation iterators you can do a lot more, this will be the topic of another post soon, where I will talk about all the different iterators you can use to iterate over iterators (say that 3x fast!) all the way from the FilterIterator to the InfinityIterator. I hope this helps you to get an idea of how to make your code better with SPL code.

comments powered by Disqus

Related Posts

Screencast: Utilizando o dmsAutocomplete com banco de dados

Screencast: Utilizando o dmsAutocomplete com banco de dados

  • January 10, 2008

Uma das perguntas que mais respondo pelo blog é “Como faço para utilizar o dmsAutocomplete com registros da base de dados?”. Além disso, a muito tempo procuro uma “desculpa” para fazer um screencast. Decidi então juntar estas duas situações e fazer o primeiro screencast da DMS Systems.

No video apresentado neste post vou demonstrar como fazer uma simples adaptação ao script PHP que vem incluído no dmsAutocomplete para que o mesmo busque dentro de uma base de dados e não mais dentro do texto como o exemplo demonstra. Usarei uma base de dados mysql simples e farei as conexões e busca de resultados utilizando as funções básicas do PDO, portanto se não conhecem esta nova camada OO de abstração de conexão com a base de dados, sugiro que procurem sobre, pois ela facilita e organiza bastante o trabalho com base de dados no PHP.

UPDATE: No momento o serviço que hospeda o screencast esta offline, o screencast deve voltar na quinta ou sexta

UPDATE: Screencast no ar novamente

Read More
Deploying a Symfony2 and Composer app on PagodaBox

Deploying a Symfony2 and Composer app on PagodaBox

  • April 16, 2012

I have been working on a little pet project and wanted to put it up somewhere to show to a few people how it was going.

Read More
OpenX 2.5 beta - Primeiras impressões

OpenX 2.5 beta - Primeiras impressões

  • March 28, 2008

Foi anunciado hoje o primeiro beta público do OpenX 2.5, a primeira versão puramente OpenX a ser lançada, digo isso por ser a primeira versão que incorpora as milhares de linhas que forma re-escritas, criando quase que praticamente uma nova aplicação.

Read More