Permalink
Browse files

BackwardsCompatibilityBreak - fixed ticket #376 - fDirectory::scan() …

…and fDirectory::scanRecursive() to strip the current directory's path before matching the `$filter`. Also added support for glob style matching.
  • Loading branch information...
wbond committed Mar 8, 2010
1 parent a432862 commit 72da3349210e99382f86455b364d479cff357e68
Showing with 65 additions and 31 deletions.
  1. +65 −31 classes/fDirectory.php
View
@@ -10,16 +10,17 @@
* @package Flourish
* @link http://flourishlib.com/fDirectory
*
* @version 1.0.0b9
* @changes 1.0.0b9 Changed the way directories deleted in a filesystem transaction are handled, including improvements to the exception that is thrown [wb+wb-imarc, 2010-03-05]
* @changes 1.0.0b8 Backwards Compatibility Break - renamed ::getFilesize() to ::getSize(), added ::move() [wb, 2009-12-16]
* @changes 1.0.0b7 Fixed ::__construct() to throw an fValidationException when the directory does not exist [wb, 2009-08-21]
* @changes 1.0.0b6 Fixed a bug where deleting a directory would prevent any future operations in the same script execution on a file or directory with the same path [wb, 2009-08-20]
* @changes 1.0.0b5 Added the ability to skip checks in ::__construct() for better performance in conjunction with fFilesystem::createObject() [wb, 2009-08-06]
* @changes 1.0.0b4 Refactored ::scan() to use the new fFilesystem::createObject() method [wb, 2009-01-21]
* @changes 1.0.0b3 Added the $regex_filter parameter to ::scan() and ::scanRecursive(), fixed bug in ::scanRecursive() [wb, 2009-01-05]
* @changes 1.0.0b2 Removed some unnecessary error suppresion operators [wb, 2008-12-11]
* @changes 1.0.0b The initial implementation [wb, 2007-12-21]
* @version 1.0.0b10
* @changes 1.0.0b10 BackwardsCompatibilityBreak - Fixed ::scan() and ::scanRecursive() to strip the current directory's path before matching, added support for glob style matching [wb, 2010-03-05]
* @changes 1.0.0b9 Changed the way directories deleted in a filesystem transaction are handled, including improvements to the exception that is thrown [wb+wb-imarc, 2010-03-05]
* @changes 1.0.0b8 Backwards Compatibility Break - renamed ::getFilesize() to ::getSize(), added ::move() [wb, 2009-12-16]
* @changes 1.0.0b7 Fixed ::__construct() to throw an fValidationException when the directory does not exist [wb, 2009-08-21]
* @changes 1.0.0b6 Fixed a bug where deleting a directory would prevent any future operations in the same script execution on a file or directory with the same path [wb, 2009-08-20]
* @changes 1.0.0b5 Added the ability to skip checks in ::__construct() for better performance in conjunction with fFilesystem::createObject() [wb, 2009-08-06]
* @changes 1.0.0b4 Refactored ::scan() to use the new fFilesystem::createObject() method [wb, 2009-01-21]
* @changes 1.0.0b3 Added the $regex_filter parameter to ::scan() and ::scanRecursive(), fixed bug in ::scanRecursive() [wb, 2009-01-05]
* @changes 1.0.0b2 Removed some unnecessary error suppresion operators [wb, 2008-12-11]
* @changes 1.0.0b The initial implementation [wb, 2007-12-21]
*/
class fDirectory
{
@@ -414,27 +415,49 @@ public function rename($new_dirname, $overwrite)
/**
* Performs a [http://php.net/scandir scandir()] on a directory, removing the `.` and `..` entries
*
* @param string $regex_filter A PCRE to filter files/directories by path, directories can be detected by checking for a trailing / (even on Windows)
* If the `$filter` looks like a valid PCRE pattern - matching delimeters
* (a delimeter can be any non-alphanumeric, non-backslash, non-whitespace
* character) followed by zero or more of the flags `i`, `m`, `s`, `x`,
* `e`, `A`, `D`, `S`, `U`, `X`, `J`, `u` - then
* [http://php.net/preg_match `preg_match()`] will be used.
*
* Otherwise the `$filter` will do a case-sensitive match with `*` matching
* zero or more characters and `?` matching a single character.
*
* On all OSes (even Windows), directories will be separated by `/`s when
* comparing with the `$filter`.
*
* @param string $filter A PCRE or glob pattern to filter files/directories by path - directories can be detected by checking for a trailing / (even on Windows)
* @return array The fFile (or fImage) and fDirectory objects for the files/directories in this directory
*/
public function scan($regex_filter=NULL)
public function scan($filter=NULL)
{
$this->tossIfDeleted();
$files = array_diff(scandir($this->directory), array('.', '..'));
$files = array_diff(scandir($this->directory), array('.', '..'));
$objects = array();
if ($filter && !preg_match('#^([^a-zA-Z0-9\\\\\s]).*\1[imsxeADSUXJu]*$#D', $filter)) {
$filter = '#^' . strtr(
preg_quote($filter, '#'),
array(
'\\*' => '.*',
'\\?' => '.'
)
) . '$#D';
}
natcasesort($files);
foreach ($files as $file) {
$file = $this->directory . $file;
if ($regex_filter) {
if ($filter) {
$test_path = (is_dir($file)) ? $file . '/' : $file;
if (!preg_match($regex_filter, $test_path)) {
continue;
if (!preg_match($filter, $test_path)) {
continue;
}
}
$objects[] = fFilesystem::createObject($file);
$objects[] = fFilesystem::createObject($this->directory . $file);
}
return $objects;
@@ -444,28 +467,39 @@ public function scan($regex_filter=NULL)
/**
* Performs a **recursive** [http://php.net/scandir scandir()] on a directory, removing the `.` and `..` entries
*
* @param string $regex_filter A PCRE to filter files/directories by path, directories can be detected by checking for a trailing / (even on Windows)
* @return array The fFile and fDirectory objects for the files/directory (listed recursively) in this directory
* @param string $filter A PCRE or glob pattern to filter files/directories by path - see ::scan() for details
* @return array The fFile (or fImage) and fDirectory objects for the files/directories (listed recursively) in this directory
*/
public function scanRecursive($regex_filter=NULL)
public function scanRecursive($filter=NULL)
{
$this->tossIfDeleted();
$files = $this->scan();
$objects = $files;
$objects = $this->scan();
$total_files = sizeof($files);
$total_files = sizeof($objects);
for ($i=0; $i < $total_files; $i++) {
if ($files[$i] instanceof fDirectory) {
array_splice($objects, $i+1, 0, $files[$i]->scanRecursive());
if ($objects[$i] instanceof fDirectory) {
array_splice($objects, $i+1, 0, $objects[$i]->scanRecursive());
}
}
if ($regex_filter) {
$new_objects = array();
if ($filter) {
if (!preg_match('#^([^a-zA-Z0-9\\\\\s*?^$]).*\1[imsxeADSUXJu]*$#D', $filter)) {
$filter = '#^' . strtr(
preg_quote($filter, '#'),
array(
'\\*' => '.*',
'\\?' => '.'
)
) . '$#D';
}
$new_objects = array();
$strip_length = strlen($this->getPath());
foreach ($objects as $object) {
$test_path = ($object instanceof fDirectory) ? substr($object->getPath(), 0, -1) . '/' : $object->getPath();
if (!preg_match($regex_filter, $test_path)) {
$test_path = substr($object->getPath(), $strip_length);
$test_path = str_replace(DIRECTORY_SEPARATOR, '/', $test_path);
if (!preg_match($filter, $test_path)) {
continue;
}
$new_objects[] = $object;

0 comments on commit 72da334

Please sign in to comment.