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

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