Development

Changeset 8256

You must first sign up to be able to contribute.

Changeset 8256

Show
Ignore:
Timestamp:
04/04/08 10:43:19 (1 year ago)
Author:
noel
Message:

fixed relative file search with sfFinder (backported from 1.1)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.0/lib/util/sfFinder.class.php

    r7767 r8256  
    44 * This file is part of the symfony package. 
    55 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
    6  *  
     6 * 
    77 * For the full copyright and license information, please view the LICENSE 
    88 * file that was distributed with this source code. 
    99 */ 
    1010 
    11 /** 
    12  * 
    13  * @package    symfony 
    14  * @subpackage util 
    15  * @author     Fabien Potencier <fabien.potencier@symfony-project.com> 
    16  * @version    SVN: $Id$ 
    17  */ 
    1811 
    1912/** 
     
    3831class sfFinder 
    3932{ 
    40   protected $type        = 'file'; 
    41   protected $names       = array(); 
    42   protected $prunes      = array(); 
    43   protected $discards    = array(); 
    44   protected $execs       = array(); 
    45   protected $mindepth    = 0; 
    46   protected $sizes       = array(); 
    47   protected $maxdepth    = 1000000; 
    48   protected $relative    = false; 
    49   protected $follow_link = false; 
     33  protected $type                   = 'file'; 
     34  protected $names                  = array(); 
     35  protected $prunes                 = array(); 
     36  protected $discards               = array(); 
     37  protected $execs                  = array(); 
     38  protected $mindepth               = 0; 
     39  protected $sizes                  = array(); 
     40  protected $maxdepth               = 1000000; 
     41  protected $relative               = false; 
     42  protected $follow_link            = false; 
     43  protected $sort                   = false; 
     44  protected $ignore_version_control = true; 
    5045 
    5146  /** 
     
    9388  { 
    9489    $finder = new sfFinder(); 
    95  
     90    return $finder->setType($name); 
     91  } 
     92 
     93  public function setType($name) 
     94  { 
    9695    if (strtolower(substr($name, 0, 3)) == 'dir') 
    9796    { 
    98       $finder->type = 'directory'; 
     97      $this->type = 'directory'; 
    9998    } 
    10099    else if (strtolower($name) == 'any') 
    101100    { 
    102       $finder->type = 'any'; 
     101      $this->type = 'any'; 
    103102    } 
    104103    else 
    105104    { 
    106       $finder->type = 'file'; 
    107     } 
    108  
    109     return $finder
     105      $this->type = 'file'; 
     106    } 
     107 
     108    return $this
    110109  } 
    111110 
     
    236235   * Currently supports Subversion, CVS, DARCS, Gnu Arch, Monotone, Bazaar-NG, GIT, Mercurial 
    237236   * 
    238    * @return object current pakeFinder object 
    239    */ 
    240   public function ignore_version_control() 
    241   { 
    242     $ignores = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'); 
    243  
    244     return $this->discard($ignores)->prune($ignores); 
     237   * @return object current sfFinder object 
     238   */ 
     239  public function ignore_version_control($ignore = true) 
     240  { 
     241    $this->ignore_version_control = $ignore; 
     242 
     243    return $this; 
     244  } 
     245 
     246  /** 
     247   * Returns files and directories ordered by name 
     248   * 
     249   * @return object current sfFinder object 
     250   */ 
     251  public function sort_by_name() 
     252  { 
     253    $this->sort = 'name'; 
     254 
     255    return $this; 
     256  } 
     257 
     258  /** 
     259   * Returns files and directories ordered by type (directories before files), then by name 
     260   * 
     261   * @return object current sfFinder object 
     262   */ 
     263  public function sort_by_type() 
     264  { 
     265    $this->sort = 'type'; 
     266 
     267    return $this; 
    245268  } 
    246269 
     
    263286      if (is_array($args[$i]) && !method_exists($args[$i][0], $args[$i][1])) 
    264287      { 
    265         throw new sfException("method {$args[$i][1]} does not exist for object {$args[$i][0]}"); 
     288        throw new sfException(sprintf('method "%s" does not exist for object "%s".', $args[$i][1], $args[$i][0])); 
    266289      } 
    267290      else if (!is_array($args[$i]) && !function_exists($args[$i])) 
    268291      { 
    269         throw new sfException("function {$args[$i]} does not exist"); 
     292        throw new sfException(sprintf('function "%s" does not exist.', $args[$i])); 
    270293      } 
    271294 
     
    310333    $here_dir = getcwd(); 
    311334    $numargs  = func_num_args(); 
    312     $arg_list = func_get_args();  
     335    $arg_list = func_get_args(); 
     336 
     337    $finder = clone $this; 
     338 
     339    if ($this->ignore_version_control) 
     340    { 
     341      $ignores = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'); 
     342 
     343      $finder->discard($ignores)->prune($ignores); 
     344    } 
    313345 
    314346    // first argument is an array? 
     
    338370      } 
    339371 
     372      $dir = str_replace(array('/', '\\'), '/', $dir); 
     373 
    340374      if ($this->relative) 
    341375      { 
    342         $files = array_merge($files, str_replace($dir.DIRECTORY_SEPARATOR, '', $this->search_in($dir))); 
     376        $dir   = rtrim($dir, '/'); 
     377        $files = array_merge($files, str_replace($dir.'/', '', str_replace(array('/', '\\'), '/', $finder->search_in($dir)))); 
    343378      } 
    344379      else 
    345380      { 
    346         $files = array_merge($files, $this->search_in($dir)); 
    347       } 
     381        $files = array_merge($files, $finder->search_in($dir)); 
     382      } 
     383    } 
     384 
     385    if ($this->sort == 'name') 
     386    { 
     387      sort($files); 
    348388    } 
    349389 
     
    364404 
    365405    $files = array(); 
    366  
     406    $temp_files = array(); 
     407    $temp_folders = array(); 
    367408    if (is_dir($dir)) 
    368409    { 
     
    379420 
    380421        if (is_dir($current_entry)) 
     422        { 
     423          if ($this->sort == 'type') 
     424          { 
     425            $temp_folders[$entryname] = $current_entry; 
     426          } 
     427          else 
     428          { 
     429            if (($this->type == 'directory' || $this->type == 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->exec_ok($dir, $entryname)) 
     430            { 
     431              $files[] = realpath($current_entry); 
     432            } 
     433 
     434            if (!$this->is_pruned($dir, $entryname)) 
     435            { 
     436              $files = array_merge($files, $this->search_in($current_entry, $depth + 1)); 
     437            } 
     438          } 
     439        } 
     440        else 
     441        { 
     442          if (($this->type != 'directory' || $this->type == 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->size_ok($dir, $entryname) && $this->exec_ok($dir, $entryname)) 
     443          { 
     444            if ($this->sort == 'type') 
     445            { 
     446              $temp_files[] = realpath($current_entry); 
     447            } 
     448            else 
     449            { 
     450              $files[] = realpath($current_entry); 
     451            } 
     452          } 
     453        } 
     454      } 
     455 
     456      if ($this->sort == 'type') 
     457      { 
     458        ksort($temp_folders); 
     459        foreach($temp_folders as $entryname => $current_entry) 
    381460        { 
    382461          if (($this->type == 'directory' || $this->type == 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->exec_ok($dir, $entryname)) 
     
    390469          } 
    391470        } 
    392         else 
    393         { 
    394           if (($this->type != 'directory' || $this->type == 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->size_ok($dir, $entryname) && $this->exec_ok($dir, $entryname)) 
    395           { 
    396             $files[] = realpath($current_entry); 
    397           } 
    398         } 
    399       } 
     471 
     472        sort($temp_files); 
     473        $files = array_merge($files, $temp_files); 
     474      } 
     475 
    400476      closedir($current_dir); 
    401477    } 
     
    685761    if (!preg_match('{^([<>]=?)?(.*?)([kmg]i?)?$}i', $this->test, $matches)) 
    686762    { 
    687       throw new sfException('don\'t understand "'.$this->test.'" as a test'); 
     763      throw new sfException(sprintf('don\'t understand "%s" as a test.', $this->test)); 
    688764    } 
    689765 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.