Jump to content



Latest News: (loading..)

- - - - -

Case for a filtered directory iterator


  • Please log in to reply
8 replies to this topic

#1   FWR Media

FWR Media
  • Community Sponsor
  • 6,839 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 02 August 2012 - 11:59 AM

Concept:

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

#2 ONLINE   Harald Ponce de Leon

Harald Ponce de Leon

    Healthy Giraffe

  • Core Team
  • 3,962 posts
  • Real Name:Harald Ponce de Leon
  • Gender:Male
  • Location:Solingen, Germany

Posted 02 August 2012 - 01:20 PM

That needs to be merged with:

https://github.com/haraldpdl/oscommerce/blob/master/osCommerce/OM/Core/DirectoryListing.php

(for v3.x)

:thumbsup:

Edited by Harald Ponce de Leon, 02 August 2012 - 01:20 PM.

Harald Ponce de Leon

#3   FWR Media

FWR Media
  • Community Sponsor
  • 6,839 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 02 August 2012 - 01:31 PM

Interesting, I'll look through that and identify all the diffs ( like perms etc ).

#4   FWR Media

FWR Media
  • Community Sponsor
  • 6,839 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 02 August 2012 - 06:16 PM

@Harald Ponce de Leon

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.


#5   FWR Media

FWR Media
  • Community Sponsor
  • 6,839 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 02 August 2012 - 11:10 PM

The classes now operate in the osCommerce\OM\Core namespace

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
API html file at same location.

Edited by FWR Media, 02 August 2012 - 11:11 PM.


#6   FWR Media

FWR Media
  • Community Sponsor
  • 6,839 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 03 August 2012 - 11:24 AM

Added to public API
  • 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
Added to the core iterator interface API
  • 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.


#7   FWR Media

FWR Media
  • Community Sponsor
  • 6,839 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 05 August 2012 - 08:40 AM

I think it is pretty much done now, new API at the usual address.

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 )
This seems to cover all needs of OM/Core/DirectoryListing.php although I wasn't sure of the sorting and didn't want to hardcode in anything osCommerce specific at this stage.

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.


#8   FWR Media

FWR Media
  • Community Sponsor
  • 6,839 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 07 August 2012 - 06:45 PM

Now working in osC 3.0

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.

#9   FWR Media

FWR Media
  • Community Sponsor
  • 6,839 posts
  • Real Name:Robert Fisher
  • Gender:Male
  • Location:Stowmarket - Suffolk - UK

Posted 08 August 2012 - 06:09 PM

Point taken :blink: