OOP structure extends the PHP \Iterator class

dev-master 2018-03-26 14:14 UTC

This package is auto-updated.

Last update: 2024-04-29 03:41:25 UTC


Iterators magic is a way to iterate everything (folder - files - pdo vs.) with OOP structure.

Gives you, the separation of concerns and extendable class structure while you have an iterator with a renderer class.

Please don't hesitate to contribute if you have different opinion that you want to share.


You can use any renderer class to render result of the Iteration class ( SPL class )

There is an example in project that called HtmlDirectoryRenderer to print the results to a page with html design.

$data = new HtmlDirectoryRenderer(
    new LocalDirectoryIterator('/var/www')


Iterator Class use IteratorAggregate interface and getIterator() method for iteration.

You can change easily DirectoryIterator and Renderer Type.

For example if you want to iterate a remote directory folder with a ftp connection,

You can create a class FtpDirectoryIterator :

You can pass configuration with AbstractDirectoryIterator to your main Iterator class.

$data = new HtmlDirectoryRenderer(
    new FtpDirectoryIterator('/var/www',$config)



TreeIterator example of view folder structure as tree view.

$data = new TreeDirectoryRenderer(
    new TreeDirectoryIterator('/var/www/playground')


├ /var/www/playground/app
├ /var/www/playground/package.json
├ /var/www/playground/gulpfile.js
├ /var/www/playground/.git
├ /var/www/playground/config.js
├ /var/www/playground/.babelrc
├ /var/www/playground/
├ /var/www/playground/.gitignore
├ /var/www/playground/merger
├ /var/www/playground/public
└ /var/www/playground/.idea


You can define filters to filter your iteration. You can create custom filter easily in your Filter class and you can just define with an array.
$filters = [
        'extensions'=>['php', 'html', 'js', 'tpl']

$data = new HtmlDirectoryRenderer(
    new LocalDirectoryIterator('/var/www/playground'),


Filters must return SPL directory File Info ( $this->directory ) or false (break out filter queue)

protected function ExtensionFilter($filter)
    $extensions = $filter['extensions'];

    return in_array($this->directory->getExtension(), $extensions) === true?$this->directory:false;