PHP Classes

PHP Sort Directory Iterator: Interface to sort and filter directory entries

Recommend this page to a friend!
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 92 This week: 2All time: 9,895 This week: 94Up
Version License PHP version Categories
directorywalker 1.0.0BSD License5PHP 5, Files and Folders, Language
Description 

Author

This package provides an interface to sort and filter directory entries.

It implements the recursive iterator interface to traverse directories recursively and sort the directory entries.

The interface takes a callback function that can filter files and directories according to custom rules.

Picture of David Tamas
  Performance   Level  
Name: David Tamas <contact>
Classes: 5 packages by
Country: Hungary Hungary
Age: 37
All time rank: 311028 in Hungary Hungary
Week rank: 109 Up2 in Hungary Hungary Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Example

<?php

function gAutoLoader($class) {
   
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class);

    if (
is_readable($class . '.php')) {
        include_once
$class . '.php';
        return
true;
    }

    return
false;
}

spl_autoload_register('gAutoLoader');


$path = realpath(__DIR__.'/../g-backup-2/test-dir/source/backup5');

$directoryIncludes = []; //All include
$directoryExcludes = ['~y~']; //contains "y" char
$fileIncludes = ['~\.php$~']; //extension need to be ".php'
$fileExcludes = ['~temp~']; //File name or path contains the string "temp"

$directorySelector = new \directoryWalker\selector($directoryIncludes, $directoryExcludes);
$fileSelector = new \directoryWalker\selector($fileIncludes, $fileExcludes);

$sortFunction = function($value) { return ($value->isDir() ? '2#' : '1#').$value->getPathName(); };
$filterFunction = function ($current, $key, $iterator) {
    global
$directorySelector, $fileSelector;

    if (
$current->isDir()) {
        return
$directorySelector->isGood($current->getPathName());
    }
    else {
        return
$fileSelector->isGood($current->getPathname());
    }
};

$directory = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
$sorted = new \directoryWalker\RecursiveSortingIterator($directory, $sortFunction );
$filtered = new \RecursiveCallbackFilterIterator($sorted, $filterFunction);

$tit = new RecursiveTreeIterator($filtered);
foreach(
$tit as $key => $value ){
    echo
$value . PHP_EOL;
}


Details

directoryWalker

Sort RecursiveDirectoryIterator elements level-by-level

Sorts the entries in the current subdirectory (based on custom defined criteria) and stores the entries desired->current position pairs. Because RecursiveDirectoryIterator is seekable this class can seek through the entries in the desired order. Minimal memory and performance overhead

Filtering uses include+exclude rulesets with regexp or bogus glob patterns

This is a simple matcher that can filter elements using include and exclude rulesets. An elemnt is matched if it matches any include rule, as long as it does not match an exclude rule.

Rules are simple regexp patterns or glob like patterns that converts to regexp patterns. This class joins up the include rules into one regexp and the exclude rules into an another regexp to improve the matching performance.


  Files folder image Files  
File Role Description
Files folder imagedirectoryWalker (2 files)
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file test-filtering-sorted.php Example Example script
Accessible without login Plain text file test-selector.php Example Example script
Accessible without login Plain text file test-sorting.php Example Example script

  Files folder image Files  /  directoryWalker  
File Role Description
  Plain text file RecursiveSortingIterator.php Class Class source
  Plain text file selector.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:92
This week:2
All time:9,895
This week:94Up