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.14 2005/06/03 08:11:18 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.14 2005/06/03 08:11:18 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 = PEAR::raiseError("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.         /* TODO: make mapTree() statically callable */
  111.  
  112.         /* clear the results just in case */
  113.         $this->files       = array();
  114.         $this->directories = array();
  115.  
  116.         /* strip out tailing / to be consistent */
  117.         $directory ereg_replace(DIRECTORY_SEPARATOR.'$'''$directory);
  118.  
  119.         $this->_dirs = array($directory);
  120.  
  121.         while (count($this->_dirs)) {
  122.             $dir array_pop($this->_dirs);
  123.             File_Find::_build($dir);
  124.             array_push($this->directories$dir);
  125.         }
  126.  
  127.         return array($this->directories$this->files);
  128.     }
  129.  
  130.     /**
  131.      * Map the directory tree given by the directory parameter.
  132.      *
  133.      * @param string $directory contains the directory path that you
  134.      *  want to map.
  135.      * @param integer $maxrecursion maximun number of folders to recursive
  136.      *  map
  137.      *
  138.      * @return array a multidimensional array containing all subdirectories
  139.      *  and their files. For example:
  140.      *
  141.      *  Array
  142.      *  (
  143.      *     [0] => file_1.php
  144.      *     [1] => file_2.php
  145.      *     [subdirname] => Array
  146.      *        (
  147.      *           [0] => file_1.php
  148.      *        )
  149.      *  )
  150.      *
  151.      * @author Mika Tuupola <tuupola@appelsiini.net>
  152.      * @access public
  153.      */
  154.     function &mapTreeMultiple($directory$maxrecursion = 0$count = 0)
  155.     {   
  156.         $retval = array();
  157.  
  158.         $count++;
  159.  
  160.         $directory .= DIRECTORY_SEPARATOR;
  161.         $dh opendir($directory);
  162.         while (false !== ($entry @readdir($dh))) {
  163.             if ($entry != '.' && $entry != '..'{
  164.                  array_push($retval$entry);
  165.             }
  166.         }
  167.  
  168.         closedir($dh);
  169.      
  170.         while (list($key$valeach($retval)) {
  171.             $path $directory $val;
  172.             $path str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,
  173.                                 DIRECTORY_SEPARATOR$path);
  174.       
  175.             if (!is_array($val&& is_dir($path)) {
  176.                 unset($retval[$key]);
  177.                 if ($maxrecursion == 0 || $count $maxrecursion{
  178.                     $retval[$valFile_Find::mapTreeMultiple($path
  179.                                     $maxrecursion$count);
  180.                 }
  181.             }
  182.         }
  183.  
  184.         return $retval;
  185.     }
  186.  
  187.     /**
  188.      * Search the specified directory tree with the specified pattern.  Return
  189.      * an array containing all matching files (no directories included).
  190.      *
  191.      * @param string $pattern the pattern to match every file with.
  192.      *
  193.      * @param string $directory the directory tree to search in.
  194.      *
  195.      * @param string $type the type of regular expression support to use, either
  196.      *  'php' or 'perl'.
  197.      *
  198.      * @param bool $fullpath whether the regex should be matched against the
  199.      *  full path or only against the filename
  200.      *
  201.      * @return array a list of files matching the pattern parameter in the the
  202.      *  directory path specified by the directory parameter
  203.      *
  204.      * @author Sterling Hughes <sterling@php.net>
  205.      * @access public
  206.      */
  207.     function &search($pattern$directory$type 'php'$fullpath = true)
  208.     {
  209.  
  210.         /* if called statically */
  211.         if (!isset($this)  || !is_a($this"File_Find")) {
  212.             $obj &new File_Find();
  213.             return $obj->search($pattern$directory$type$fullpath);
  214.         else {
  215.  
  216.             $matches = array();
  217.             list (,$files)  File_Find::maptree($directory);
  218.             $match_function File_Find::_determineRegex($pattern$type);
  219.  
  220.             reset($files);
  221.             while (list(,$entryeach($files)) {
  222.                 if ($match_function($pattern
  223.                                     $fullpath $entry basename($entry))) {
  224.                     $matches[$entry;
  225.                 }
  226.             }
  227.         }
  228.  
  229.         return ($matches);
  230.     }
  231.  
  232.     /**
  233.      * Determine whether or not a variable is a PEAR error
  234.      *
  235.      * @param object PEAR_Error $var the variable to test.
  236.      *
  237.      * @return boolean returns true if the variable is a PEAR error, otherwise
  238.      *  it returns false.
  239.      * @access public
  240.      */
  241.     function isError(&$var)
  242.     {
  243.         return PEAR::isError($var);
  244.     }
  245.  
  246.     /**
  247.      * internal function to build singular directory trees, used by
  248.      * File_Find::maptree()
  249.      *
  250.      * @param string $directory name of the directory to read
  251.      * @return void 
  252.      */
  253.     function _build($directory)
  254.     {
  255.  
  256.         $dh @opendir($directory);
  257.  
  258.         if (!$dh{
  259.             $pe = PEAR::raiseError("Cannot open directory");
  260.             return $pe;
  261.         }
  262.  
  263.         while (false !== ($entry @readdir($dh))) {
  264.             if ($entry != '.' && $entry != '..'{
  265.  
  266.                 $entry $directory.DIRECTORY_SEPARATOR.$entry;
  267.                 $entry str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,
  268.                                      DIRECTORY_SEPARATOR$entry);
  269.  
  270.                 if (is_dir($entry)) {
  271.                     array_push($this->_dirs$entry);
  272.                 else {
  273.                     array_push($this->files$entry);
  274.                 }
  275.             }
  276.         }
  277.  
  278.         @closedir($dh);
  279.     }
  280.  
  281.     /**
  282.      * internal function to determine the type of regular expression to
  283.      * use, implemented by File_Find::glob() and File_Find::search()
  284.      *
  285.      * @param string $type given RegExp type
  286.      * @return string kind of function ( "eregi", "ereg" or "preg_match") ;
  287.      *
  288.      */
  289.     function _determineRegex($pattern$type)
  290.     {
  291.         if (!strcasecmp($type'perl')) {
  292.             $match_function 'preg_match';
  293.         else if (!strcasecmp(substr($pattern-2)'/i')) {
  294.             $match_function 'eregi';
  295.         else {
  296.             $match_function 'ereg';
  297.         }
  298.  
  299.         return $match_function;
  300.     }
  301. }
  302.  
  303. /*
  304.  * Local variables:
  305.  * tab-width: 4
  306.  * c-basic-offset: 4
  307.  * End:
  308.  */
  309.  
  310. ?>

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