File
[ class tree: File ] [ index: File ] [ all elements ]

Source for file Find.php

Documentation is available at Find.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2005 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Sterling Hughes <sterling@php.net>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Find.php,v 1.19 2005/09/11 10:03:30 techtonik Exp $
  20. //
  21.  
  22. require_once 'PEAR.php';
  23.  
  24. define('FILE_FIND_VERSION''@package_version@');
  25.  
  26. /**
  27. *  Commonly needed functions searching directory trees
  28. *
  29. @access public
  30. @version $Id: Find.php,v 1.19 2005/09/11 10:03:30 techtonik Exp $
  31. @package File
  32. @author Sterling Hughes <sterling@php.net>
  33. */
  34. class File_Find
  35. {
  36.     /**
  37.      * internal dir-list
  38.      * @var array 
  39.      */
  40.     var $_dirs = array();
  41.  
  42.     /**
  43.      * found files
  44.      * @var array 
  45.      */
  46.     var $files = array();
  47.  
  48.     /**
  49.      * found dirs
  50.      * @var array 
  51.      */
  52.     var $directories = array();
  53.  
  54.     /**
  55.      * Search the current directory to find matches for the
  56.      * the specified pattern.
  57.      *
  58.      * @param string $pattern a string containing the pattern to search
  59.      *  the directory for.
  60.      *
  61.      * @param string $dirpath a string containing the directory path
  62.      *  to search.
  63.      *
  64.      * @param string $pattern_type a string containing the type of
  65.      *  pattern matching functions to use (can either be 'php' or
  66.      *  'perl').
  67.      *
  68.      * @return array containing all of the files and directories
  69.      *  matching the pattern or null if no matches
  70.      *
  71.      * @author Sterling Hughes <sterling@php.net>
  72.      * @access public
  73.      * @static
  74.      */
  75.     function &glob($pattern$dirpath$pattern_type 'php')
  76.     {
  77.         $dh @opendir($dirpath);
  78.  
  79.         if (!$dh{
  80.             $pe = PEAR::raiseError("Cannot open directory");
  81.             return $pe;
  82.         }
  83.  
  84.         $match_function File_Find::_determineRegex($pattern$pattern_type);
  85.         $matches = array();
  86.         while (false !== ($entry @readdir($dh))) {
  87.             if ($match_function($pattern$entry&&
  88.                 $entry != '.' && $entry != '..'{
  89.                 $matches[$entry;
  90.             }
  91.         }
  92.  
  93.         @closedir($dh);
  94.  
  95.         return (count($matches> 0$matches : null;
  96.     }
  97.  
  98.     /**
  99.      * Map the directory tree given by the directory_path parameter.
  100.      *
  101.      * @param string $directory contains the directory path that you
  102.      *  want to map.
  103.      *
  104.      * @return array a two element array, the first element containing a list
  105.      *  of all the directories, the second element containing a list of all the
  106.      *  files.
  107.      *
  108.      * @author Sterling Hughes <sterling@php.net>
  109.      * @access public
  110.      */
  111.     function &maptree($directory)
  112.     {
  113.  
  114.         /* if called statically */
  115.         if (!isset($this)  || !is_a($this"File_Find")) {
  116.             $obj &new File_Find();
  117.             return $obj->maptree($directory);
  118.         }
  119.       
  120.         /* clear the results just in case */
  121.         $this->files       = array();
  122.         $this->directories = array();
  123.  
  124.         /* consistency rules - strip out trailing slashes */
  125.         $directory preg_replace('![\\/]+$!'''$directory);
  126.         /* use only native system directory delimiters */
  127.         $directory preg_replace("![\\/]+!"DIRECTORY_SEPARATOR$directory);
  128.  
  129.         $this->_dirs = array($directory);
  130.  
  131.         while (count($this->_dirs)) {
  132.             $dir array_pop($this->_dirs);
  133.             File_Find::_build($dir);
  134.             array_push($this->directories$dir);
  135.         }
  136.  
  137.         return array($this->directories$this->files);
  138.     }
  139.  
  140.     /**
  141.      * Map the directory tree given by the directory parameter.
  142.      *
  143.      * @param string $directory contains the directory path that you
  144.      *  want to map.
  145.      * @param integer $maxrecursion maximun number of folders to recursive
  146.      *  map
  147.      *
  148.      * @return array a multidimensional array containing all subdirectories
  149.      *  and their files. For example:
  150.      *
  151.      *  Array
  152.      *  (
  153.      *     [0] => file_1.php
  154.      *     [1] => file_2.php
  155.      *     [subdirname] => Array
  156.      *        (
  157.      *           [0] => file_1.php
  158.      *        )
  159.      *  )
  160.      *
  161.      * @author Mika Tuupola <tuupola@appelsiini.net>
  162.      * @access public
  163.      * @static
  164.      */
  165.     function &mapTreeMultiple($directory$maxrecursion = 0$count = 0)
  166.     {   
  167.         $retval = array();
  168.  
  169.         $count++;
  170.  
  171.         $directory .= DIRECTORY_SEPARATOR;
  172.         $dh opendir($directory);
  173.         while (false !== ($entry @readdir($dh))) {
  174.             if ($entry != '.' && $entry != '..'{
  175.                  array_push($retval$entry);
  176.             }
  177.         }
  178.  
  179.         closedir($dh);
  180.      
  181.         while (list($key$valeach($retval)) {
  182.             $path $directory $val;
  183.             $path str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,
  184.                                 DIRECTORY_SEPARATOR$path);
  185.       
  186.             if (!is_array($val&& is_dir($path)) {
  187.                 unset($retval[$key]);
  188.                 if ($maxrecursion == 0 || $count $maxrecursion{
  189.                     $retval[$val&File_Find::mapTreeMultiple($path
  190.                                     $maxrecursion$count);
  191.                 }
  192.             }
  193.         }
  194.  
  195.         return $retval;
  196.     }
  197.  
  198.     /**
  199.      * Search the specified directory tree with the specified pattern.  Return
  200.      * an array containing all matching files (no directories included).
  201.      *
  202.      * @param string $pattern the pattern to match every file with.
  203.      *
  204.      * @param string $directory the directory tree to search in.
  205.      *
  206.      * @param string $type the type of regular expression support to use, either
  207.      *  'php' or 'perl'.
  208.      *
  209.      * @param bool $fullpath whether the regex should be matched against the
  210.      *  full path or only against the filename
  211.      *
  212.      * @param string $match can be either 'files', 'dirs' or 'both' to specify
  213.      *  the kind of list to return
  214.      *
  215.      * @return array a list of files matching the pattern parameter in the the
  216.      *  directory path specified by the directory parameter
  217.      *
  218.      * @author Sterling Hughes <sterling@php.net>
  219.      * @access public
  220.      * @static
  221.      */
  222.     function &search($pattern$directory$type 'php'$fullpath = true$match 'files')
  223.     {
  224.  
  225.         $matches = array();
  226.         list ($directories,$files)  File_Find::maptree($directory);
  227.         switch($match{
  228.             case 'directories'$data $directories; break;
  229.             case 'both'$data array_merge($directories$files); break;
  230.             case 'files':
  231.             default:
  232.                 $data $files;
  233.         }
  234.         unset($files$directories);
  235.  
  236.         $match_function File_Find::_determineRegex($pattern$type);
  237.  
  238.         reset($data);
  239.         while (list(,$entryeach($data)) {
  240.             if ($match_function($pattern
  241.                                 $fullpath $entry basename($entry))) {
  242.                 $matches[$entry;
  243.             }
  244.         }
  245.  
  246.         return ($matches);
  247.     }
  248.  
  249.     /**
  250.      * Determine whether or not a variable is a PEAR error
  251.      *
  252.      * @param object PEAR_Error $var the variable to test.
  253.      *
  254.      * @return boolean returns true if the variable is a PEAR error, otherwise
  255.      *  it returns false.
  256.      * @access public
  257.      */
  258.     function isError(&$var)
  259.     {
  260.         return PEAR::isError($var);
  261.     }
  262.  
  263.     /**
  264.      * internal function to build singular directory trees, used by
  265.      * File_Find::maptree()
  266.      *
  267.      * @param string $directory name of the directory to read
  268.      * @return void 
  269.      */
  270.     function _build($directory)
  271.     {
  272.  
  273.         $dh @opendir($directory);
  274.  
  275.         if (!$dh{
  276.             $pe = PEAR::raiseError("Cannot open directory");
  277.             return $pe;
  278.         }
  279.  
  280.         while (false !== ($entry @readdir($dh))) {
  281.             if ($entry != '.' && $entry != '..'{
  282.  
  283.                 $entry $directory.DIRECTORY_SEPARATOR.$entry;
  284.                 $entry str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,
  285.                                      DIRECTORY_SEPARATOR$entry);
  286.  
  287.                 if (is_dir($entry)) {
  288.                     array_push($this->_dirs$entry);
  289.                 else {
  290.                     array_push($this->files$entry);
  291.                 }
  292.             }
  293.         }
  294.  
  295.         @closedir($dh);
  296.     }
  297.  
  298.     /**
  299.      * internal function to determine the type of regular expression to
  300.      * use, implemented by File_Find::glob() and File_Find::search()
  301.      *
  302.      * @param string $type given RegExp type
  303.      * @return string kind of function ( "eregi", "ereg" or "preg_match") ;
  304.      *
  305.      */
  306.     function _determineRegex($pattern$type)
  307.     {
  308.         if (!strcasecmp($type'perl')) {
  309.             $match_function 'preg_match';
  310.         else if (!strcasecmp(substr($pattern-2)'/i')) {
  311.             $match_function 'eregi';
  312.         else {
  313.             $match_function 'ereg';
  314.         }
  315.  
  316.         return $match_function;
  317.     }
  318. }
  319.  
  320. /*
  321.  * Local variables:
  322.  * tab-width: 4
  323.  * c-basic-offset: 4
  324.  * End:
  325.  */
  326.  
  327. ?>

Documentation generated on Mon, 11 Mar 2019 14:14:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.