Case for a filtered directory iterator
#1
Posted 02 August 2012 - 11:59 AM
Ability to iterate directories using complex allow / filter.
Comment:
Iterating directories is not very "sexy" in the great scheme of things but I needed this myself for other projects so thought I'd put it forward.
Types: Return directories or files
Allow filter types:
allow - Returns all files exactly matching the filter ( e.g. file account_edit.php or directory admin )
allow_extn - Returns all files that have the extensions in the filters array ( e.g. array( '.png', '.gif' )
allow_pcre - Returns all files matching the filters array of patterns e.g. array( '^checkout([a-z0-9_]+)\.php$' ) )
Filter filter types:
filter - Returns all files which do not exactly match the filter.
filter_extn - Returns all files except those that have their extensions in the filters array.
filter_pcre - Returns all files which do not match the filters array of patterns
Public Interface API & examples of real tests:
www ( dot ) fwrdev ( dot ) co ( dot ) uk/cases/case_filtered_directory_iterator.html
KissMT Dynamic SEO Meta & Canonical Header Tags
KissER Error Handling and Debugging
KissIT Image Thumbnailer
Security Pro - Querystring protection against hackers ( a KISS contribution )
If you found my post useful please click the "Like This" button to the right.
Please only PM me for paid work.
#2 ONLINE
Posted 02 August 2012 - 01:20 PM
https://github.com/haraldpdl/oscommerce/blob/master/osCommerce/OM/Core/DirectoryListing.php
(for v3.x)
Edited by Harald Ponce de Leon, 02 August 2012 - 01:20 PM.
#3
Posted 02 August 2012 - 01:31 PM
KissMT Dynamic SEO Meta & Canonical Header Tags
KissER Error Handling and Debugging
KissIT Image Thumbnailer
Security Pro - Querystring protection against hackers ( a KISS contribution )
If you found my post useful please click the "Like This" button to the right.
Please only PM me for paid work.
#4
Posted 02 August 2012 - 06:16 PM
Spent a little time on this, some of it I still need to go through and understand exactly what you want from certain methods.
Having said that two needs were immediately obvious:
1) Recursive iteration
2) File stats
So I've added this functionality and would like your thoughts if you get a sec ( keeps me motivated in my spare time
Same location for the updated API
www ( dot ) fwrdev ( dot ) co ( dot ) uk/cases/case_filtered_directory_iterator.html
Edited by FWR Media, 02 August 2012 - 06:17 PM.
KissMT Dynamic SEO Meta & Canonical Header Tags
KissER Error Handling and Debugging
KissIT Image Thumbnailer
Security Pro - Querystring protection against hackers ( a KISS contribution )
If you found my post useful please click the "Like This" button to the right.
Please only PM me for paid work.
#5
Posted 02 August 2012 - 11:10 PM
Updated the API file.
The chaining of methods was getting too verbose with the number of settings required so I have separated out the core class and made some extensions.
FilteredCoreIterator is now the core class - not intended for use any more but still can be.
Extensions are ( rationale behind class naming strategy explained in the API ): -
- FileIterator
- DirectoryIterator
- FileRecursiveIterator
- DirectoryRecursiveIterator
- FileRecursiveStatsIterator
- DirectoryRecursiveStatsIterator
Edited by FWR Media, 02 August 2012 - 11:11 PM.
KissMT Dynamic SEO Meta & Canonical Header Tags
KissER Error Handling and Debugging
KissIT Image Thumbnailer
Security Pro - Querystring protection against hackers ( a KISS contribution )
If you found my post useful please click the "Like This" button to the right.
Please only PM me for paid work.
#6
Posted 03 August 2012 - 11:24 AM
- DirectoryStatsIterator - ( returns directories from the dir path and returns an array of SplFileInfo results )
- FileStatsIterator - ( returns files from the dir path and returns an array of SplFileInfo results )
- DirectoryFileIterator - ( iterates the dir path for both files and directories
- setPcreCaseSensitive() - ( patterns are case insensitive as default )
- setAddDirectoryToFilename() - ( adds the directory path to directories and files returned )
Edited by FWR Media, 03 August 2012 - 11:25 AM.
KissMT Dynamic SEO Meta & Canonical Header Tags
KissER Error Handling and Debugging
KissIT Image Thumbnailer
Security Pro - Querystring protection against hackers ( a KISS contribution )
If you found my post useful please click the "Like This" button to the right.
Please only PM me for paid work.
#7
Posted 05 August 2012 - 08:40 AM
Main Changes/Additions:
- Much faster due to better use of SPL.
- All returns from CoreIterator::iterate() are now instances of SplFileInfo offering a lot of stats info
- Recursive iteration
- API public methods just 10 - ( reset(), setPath(), setType(), setFilters(), setFileFilters(), setDirFilters(), setFilterMode(), setRecursive(), setPcreCaseSensitive(), iterate() )
- Extended classes reduced to 6 - these remove use of public methods down to just a few and handle all needs. ( FileIterator, DirectoryIterator, DirectoryFileIterator, FileRecursiveIterator, DirectoryRecursiveIterator, DirectoryFileRecursiveIterator )
One example of a fully recursive filtered iteration of includes/modules ( 2.3.2 ) which returns all directories and files with .php extension.
$Iterator = new DirectoryFileRecursiveIterator( array( '_path' => PATH_TO_ITERATE . 'includes/modules/' ) );
foreach ( $Iterator->iterate() as $SplFileInfo ) {
// Do something with $SplFileInfo->getRealpath() ( or other SplFileInfo methods )
}
Can make the files available for beta testing along with tests and a blank file for creating new tests if wanted.
Edited by FWR Media, 05 August 2012 - 08:45 AM.
KissMT Dynamic SEO Meta & Canonical Header Tags
KissER Error Handling and Debugging
KissIT Image Thumbnailer
Security Pro - Querystring protection against hackers ( a KISS contribution )
If you found my post useful please click the "Like This" button to the right.
Please only PM me for paid work.
#8
Posted 07 August 2012 - 06:45 PM
Working example: -
namespace osCommerce\OM\Core\Site\Admin\Application\PaymentModules\Model;
use osCommerce\OM\Core\Registry,
osCommerce\OM\Core\Site\Admin\Application\PaymentModules\PaymentModules,
osCommerce\OM\Core\DirectoryListing\Extensions\FileIterator,
osCommerce\OM\Core\OSCOM;
class getUninstalled {
public static function execute() {
$OSCOM_Language = Registry::get('Language');
$installed_modules = PaymentModules::getInstalled();
$installed = array();
foreach ( $installed_modules['entries'] as $module ) {
$installed[] = $module['code'];
}
$result = array('entries' => array());
foreach ( ( new FileIterator( array( '_path' => OSCOM::BASE_DIRECTORY . 'Core/Site/Admin/Module/Payment' ) ) )->iterate() as $file ) {
$filename = $file->getBasename();
$module = substr($filename, 0, strrpos($filename, '.'));
if ( !in_array($module, $installed) ) {
$class = 'osCommerce\\OM\\Core\\Site\\Admin\\Module\\Payment\\' . $module;
$OSCOM_Language->injectDefinitions('modules/payment/' . $module . '.xml');
$OSCOM_PM = new $class();
$result['entries'][] = array('code' => $OSCOM_PM->getCode(),
'title' => $OSCOM_PM->getTitle(),
'sort_order' => $OSCOM_PM->getSortOrder(),
'status' => $OSCOM_PM->isEnabled());
}
}
$result['total'] = count($result['entries']);
return $result;
}
}
Let me know please if there is any interest in this before I take it further.
KissMT Dynamic SEO Meta & Canonical Header Tags
KissER Error Handling and Debugging
KissIT Image Thumbnailer
Security Pro - Querystring protection against hackers ( a KISS contribution )
If you found my post useful please click the "Like This" button to the right.
Please only PM me for paid work.
#9
Posted 08 August 2012 - 06:09 PM
KissMT Dynamic SEO Meta & Canonical Header Tags
KissER Error Handling and Debugging
KissIT Image Thumbnailer
Security Pro - Querystring protection against hackers ( a KISS contribution )
If you found my post useful please click the "Like This" button to the right.
Please only PM me for paid work.









