root

Changeset 466

Show
Ignore:
Timestamp:
01/05/09 23:46:28 (2 years ago)
Author:
wbond
Message:

Added the $regex_filter parameter to fDirectory::scan() and fDirectory::scanRecursive(), fixed a bug in fDirectory::scanRecursive()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fDirectory.php

    r448 r466 Hide Line Numbers
    33 * Represents a directory on the filesystem, also provides static directory-related methods 
    44 *  
    5  * @copyright  Copyright (c) 2007-2008 Will Bond 
     5 * @copyright  Copyright (c) 2007-2009 Will Bond 
    66 * @author     Will Bond [wb] <will@flourishlib.com> 
    77 * @license    http://flourishlib.com/license 
     
    1010 * @link       http://flourishlib.com/fDirectory 
    1111 *  
    12  * @version    1.0.0b2 
     12 * @version    1.0.0b3 
     13 * @changes    1.0.0b3  Added the $regex_filter parameter to ::scan() and ::scanRecursive(), fixed bug in ::scanRecursive() [wb, 2009-01-05] 
    1314 * @changes    1.0.0b2  Removed some unnecessary error suppresion operators [wb, 2008-12-11] 
    1415 * @changes    1.0.0b   The initial implementation [wb, 2007-12-21] 
     
    353354     * Performs a [http://php.net/scandir scandir()] on a directory, removing the `.` and `..` entries 
    354355     *  
     356     * @param  string $regex_filter  A PCRE to filter files/directories by path, directories can be detected by checking for a trailing / (even on Windows) 
    355357     * @return array  The fFile (or fImage) and fDirectory objects for the files/directories in this directory 
    356358     */ 
    357     public function scan(
     359    public function scan($regex_filter=NULL
    358360    { 
    359361        $this->tossIfException(); 
     
    364366        foreach ($files as $file) { 
    365367            $file = $this->directory . $file; 
     368             
     369            if ($regex_filter) { 
     370                $test_path = (is_dir($file)) ? $file . '/' : $file; 
     371                if (!preg_match($regex_filter, $test_path)) { 
     372                    continue;    
     373                } 
     374            } 
     375             
    366376            if (is_dir($file)) { 
    367377                $objects[] = new fDirectory($file); 
     
    380390     * Performs a **recursive** [http://php.net/scandir scandir()] on a directory, removing the `.` and `..` entries 
    381391     *  
     392     * @param  string $regex_filter  A PCRE to filter files/directories by path, directories can be detected by checking for a trailing / (even on Windows) 
    382393     * @return array  The fFile and fDirectory objects for the files/directory (listed recursively) in this directory 
    383394     */ 
    384     public function scanRecursive(
    385     { 
    386         $this->tossIfException(); 
    387          
    388         $files  = $this->scan(); 
     395    public function scanRecursive($regex_filter=NULL
     396    { 
     397        $this->tossIfException(); 
     398         
     399        $files  = $this->scan(); 
    389400        $objects = $files; 
    390401         
     
    392403        for ($i=0; $i < $total_files; $i++) { 
    393404            if ($files[$i] instanceof fDirectory) { 
    394                 $objects = array_splice($objects, $i, 0, $files[$i]->scanRecursive()); 
    395             } 
     405                array_splice($objects, $i+1, 0, $files[$i]->scanRecursive()); 
     406            } 
     407        } 
     408         
     409        if ($regex_filter) { 
     410            $new_objects = array(); 
     411            foreach ($objects as $object) { 
     412                $test_path = ($object instanceof fDirectory) ? substr($object->getPath(), 0, -1) . '/' : $object->getPath(); 
     413                if (!preg_match($regex_filter, $test_path)) { 
     414                    continue;    
     415                }    
     416                $new_objects[] = $object; 
     417            } 
     418            $objects = $new_objects; 
    396419        } 
    397420         
     
    416439 
    417440/** 
    418  * Copyright (c) 2007-2008 Will Bond <will@flourishlib.com> 
     441 * Copyright (c) 2007-2009 Will Bond <will@flourishlib.com> 
    419442 *  
    420443 * Permission is hereby granted, free of charge, to any person obtaining a copy