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

Source for file FilesystemTree.class.php

Documentation is available at FilesystemTree.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package filesystem
  9.  */
  10.  
  11. if!defined"CLASSPATH" ) )
  12. {
  13.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  14.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  15.   exit();
  16. }
  17.  
  18. /**
  19.  *
  20.  */
  21. require_onceCLASSPATH."filesystem/FilesystemToolkit.class.php" );
  22. /**
  23.  *
  24.  */
  25. require_onceCLASSPATH."filesystem/Folder.class.php" );
  26. /**
  27.  *
  28.  */
  29. include_onceCLASSPATH."filesystem/ImageFiles.class.php" );
  30. /**
  31.  * Class to generate Vector object.
  32.  */
  33. include_once CLASSPATH."data_structures/Vector.class.php";
  34.  
  35. /**
  36.  * A class to managing a filesystemtree.
  37.  *
  38.  * @package   filesystem
  39.  * @version   0.1.65
  40.  * @author    Daniel Plücken <daniel@debakel.net>
  41.  * @license   http://www.gnu.org/copyleft/lesser.html
  42.  *             GNU Lesser General Public License
  43.  * @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
  44.  *
  45.  *  This library is free software; you can redistribute it and/or
  46.  *  modify it under the terms of the GNU Lesser General Public
  47.  *  License as published by the Free Software Foundation; either
  48.  *  version 2.1 of the License.
  49.  *
  50.  *  This library is distributed in the hope that it will be useful,
  51.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  52.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  53.  *  GNU Lesser General Public License for more details.
  54.  *
  55.  *  You should have received a copy of the GNU Lesser General
  56.  *  Public License along with this library; if not, write to the
  57.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  58.  *  Boston, MA 02111-1307 USA
  59.  */
  60. {
  61.    /**
  62.     * @var    string 
  63.     * @access private
  64.     */
  65.    var $path;
  66.    /**
  67.     * @access public
  68.     * @var    string $name 
  69.     */
  70.    var $name;
  71.    /**
  72.     * @var    array 
  73.     * @access private
  74.     */
  75.    var $folder;
  76.    /**
  77.     * @var    array 
  78.     * @access private
  79.     */
  80.    var $files;
  81.  
  82.  
  83.  
  84.  
  85.    /**
  86.     * Constructor
  87.     *
  88.     * @version 1.03
  89.     * @since   0.1.0
  90.     * @author  Daniel Plücken <daniel@debakel.net>
  91.     * @access  public
  92.     * @param   string  $path 
  93.     * @param   string  $regExpMatchFiles 
  94.     * @param   string  $regExpMatchFolder 
  95.     * @param   boolean $filesize 
  96.     * @param   boolean $handle_known_filetypes For example, images will then put
  97.     *                                             into a ImageFiles object, instead
  98.     *                                             of Files object.
  99.     */
  100.    function FilesystemTree(
  101.                             $path,
  102.                             $regExpMatchFiles  "",
  103.                             $regExpMatchFolder "",
  104.                             $filesize          false,
  105.                              $handle_known_filetypes false
  106.                           )
  107.    {
  108.       $this->path = FilesystemToolkit::getCleanPath$path );
  109.       $this->name = basename$this->path );
  110.       $this->setTree(
  111.                       $path,
  112.                       $this,
  113.                       $regExpMatchFiles,
  114.                       $regExpMatchFolder,
  115.                       $filesize,
  116.                       $handle_known_filetypes
  117.                     );
  118.    }
  119.  
  120.  
  121.  
  122.  
  123.    /**
  124.     * Returns the names of folder that this->path contains by an array.
  125.     *
  126.     * @version 1.0
  127.     * @since   0.1.0
  128.     * @author  Daniel Plücken <daniel@debakel.net>
  129.     * @access  public
  130.     * @return  array 
  131.     */
  132.    function getFoldernames()
  133.    {
  134.       for$i 0$i count$this->folder )$i++ )
  135.          $outputArr[$this->folder[$i]->getName();
  136.  
  137.       return $outputArr;
  138.    }
  139.  
  140.  
  141.  
  142.  
  143.    /**
  144.     * Returns the names of files that this->path contains by an array.
  145.     *
  146.     * @version 1.0
  147.     * @since   0.1.0
  148.     * @author  Daniel Plücken <daniel@debakel.net>
  149.     * @access  public
  150.     * @return  array 
  151.     */
  152.    function getFilenames()
  153.    {
  154.       for$i 0$i count$this->files )$i++ )
  155.          $outputArr[$this->files[$i]->getName();
  156.  
  157.       return $outputArr;
  158.    }
  159.  
  160.  
  161.  
  162.  
  163.    /**
  164.     * Returns the path to the folder of this filesystem object.
  165.     *
  166.     * @version 1.0
  167.     * @since   0.1.0
  168.     * @author  Daniel Plücken <daniel@debakel.net>
  169.     * @access  public
  170.     * @return  string 
  171.     */
  172.    function getPath()
  173.    return $this->path}
  174.  
  175.  
  176.  
  177.  
  178.   /**
  179.     * Returns the foldername of this filsystem tree object.
  180.     *
  181.     * @version 1.0
  182.     * @since   0.1.5
  183.     * @author  Daniel Plücken <daniel@debakel.net>
  184.     * @access  public
  185.     * @return  string 
  186.     */
  187.    function getName()
  188.    return $this->name}
  189.  
  190.  
  191.  
  192.  
  193.   /**
  194.     * Returns the file objects of this filesystem tree object in pre-order.
  195.     *
  196.     * @version 1.0
  197.     * @since   0.1.65
  198.     * @author  Daniel Plücken <daniel@debakel.net>
  199.     * @access  public
  200.     * @return  array 
  201.     */
  202.    function &getFilesArray()
  203.    {
  204.        $out_arr array();
  205.        foreach $this->files as $key => $value )
  206.            $out_arr[=$this->files[$key];
  207.  
  208.        foreach $this->folder as $key => $value )
  209.        {
  210.            $tmp_arr =$this->folder[$key]->getFilesArray();
  211.  
  212.            // array_merge doesn't keep references alive!
  213.            foreach $tmp_arr as $key2 => $value2 )
  214.                $out_arr[=$tmp_arr[$key2];
  215.  
  216.            unset$tmp_arr );
  217.        }
  218.  
  219.        return $out_arr;
  220.    }
  221.  
  222.  
  223.  
  224.  
  225.    /**
  226.     * Builds a tree of the filesystem beginning at $dir.
  227.     *
  228.     * @version 1.65
  229.     * @since   0.1.0
  230.     * @author  Daniel Plücken <daniel@debakel.net>
  231.     * @access  private
  232.     * @static
  233.     * @param   string  $dir      The base directory from which the filesystem
  234.     *                             should be build.
  235.     * @param   object  $objRef   The root object of the tree. It will be this
  236.     *                             object in most cases.
  237.     *
  238.     * @param   string  $regExpMatchFiles 
  239.     * @param   string  $regExpMatchFolder 
  240.     *
  241.     * @param   boolean $filesize With this flag you were able to assign whether
  242.     *                             the filesizes should also stored in the tree.
  243.     *
  244.     * @param   boolean $handle_known_filetypes For example, images will then put
  245.     *                                             into a ImageFiles object, instead
  246.     *                                             of Files object.
  247.     * @return  string 
  248.     */
  249.    function setTree(
  250.                      $dir,
  251.                      &$objRef,
  252.                      $regExpMatchFiles       "",
  253.                      $regExpMatchFolder      "",
  254.                      $filesize               false,
  255.                      $handle_known_filetypes false
  256.                    )
  257.    {
  258.       $dir    FilesystemToolkit::getCleanPath$dir );
  259.       $resArr FilesystemToolkit::loadFolderContent(
  260.                                                        $dir,
  261.                                                        $regExpMatchFiles,
  262.                                                        $regExpMatchFolder,
  263.                                                        $filesize
  264.                                                     );
  265.  
  266.       if is_array$resArr ) )
  267.       {
  268.          for $i 0$i count$resArr )$i++ )
  269.           if $resArr[$i]["is_dir")
  270.           {
  271.               $objRef->folder[=new Folder(
  272.                                                  $resArr[$i]["name"],
  273.                                                  $objRef
  274.                                              );
  275.  
  276.               $tmp_curr count$objRef->folder 1;
  277.               $objRef->folder[$tmp_curr]->setParentFolder$objRef );
  278.  
  279.               $this->setTree(
  280.                                $dir."/".$resArr[$i]["name"],
  281.                                $objRef->folder[$tmp_curr],
  282.                                $regExpMatchFiles,
  283.                                $regExpMatchFolder,
  284.                                $filesize,
  285.                                $handle_known_filetypes
  286.                             );
  287.           }
  288.           else
  289.           {
  290.               if !$handle_known_filetypes )
  291.                     $objRef->files[=new Files$dir$resArr[$i]["name");
  292.               else
  293.               {
  294.                   switch (
  295.                               strtolower(
  296.                                   FilesystemToolkit::getFileSuffix(
  297.                                                           $resArr[$i]["name"]
  298.                                                                   )
  299.                                         )
  300.                            )
  301.                   {
  302.                     case "jpg":
  303.                     case "jpeg":
  304.                     case "gif":
  305.                     case "png":
  306.                            $objRef->files[=new ImageFiles(
  307.                                                        $dir."/".$resArr[$i]["name"]
  308.                                                                 );
  309.                          break;
  310.                     default:
  311.                            $objRef->files[=new Files(
  312.                                                            $dir,
  313.                                                            $resArr[$i]["name"]
  314.                                                          );
  315.                            break;
  316.                   }
  317.               }
  318.  
  319.               $tmp_curr count$objRef->files 1;
  320.               if $filesize )
  321.                  $objRef->files[$tmp_curr]->setFilesize$resArr[$i]["size");
  322.  
  323.               $objRef->files[$tmp_curr]->setParentFolder$objRef );
  324.           }
  325.       }
  326.    }
  327.  
  328.  
  329.  
  330.     /**
  331.      * Search an Files object in the tree that is equal to the given one. The
  332.      * two File objects will be accounted as equal, if the the filenames and
  333.      * pathes match each other.
  334.      *
  335.      * @version 1.0
  336.      * @since   0.1.47
  337.      * @author  Daniel Plücken <daniel@debakel.net>
  338.      * @access  public
  339.      * @param   string $f_obj      The object to find.
  340.      * @param   string $folder_obj The reference used to dig in the filesystem
  341.      *                                 tree. This variable has to be initialized
  342.      *                                 with $this object.
  343.      * @return  null|Files
  344.      */
  345.     function &findFile&$f_obj&$folder_obj /* = $this */ )
  346.     {
  347.        for $i 0$i count$folder_obj->files )$i++ )
  348.            if $f_obj->equals$folder_obj->files[$i) )
  349.               # ImageFiles object found!
  350.               return $folder_obj->files[$i];
  351.  
  352.        for $i 0$i count$folder_obj->folder )$i++ )
  353.        {
  354.            $return_obj =$this->findFile$f_obj$folder_obj->folder[$i);
  355.  
  356.            # ImageFiles object found in depth!
  357.            if is_object$return_obj ) )
  358.               return $return_obj;
  359.        }
  360.  
  361.        return null;
  362.     }
  363.  
  364.  
  365.  
  366.     /**
  367.      * Builds and returns a Vector from the filesystem trees. That means that
  368.      * all files will be in a list, so for example you can operate with next()-
  369.      * and prev()-function to navigate in that list.
  370.      *
  371.      * @version 1.0
  372.      * @since   0.1.17
  373.      * @author  Daniel Plücken <daniel@debakel.net>
  374.      * @access  public
  375.      * @param   string $folder_obj The reference used to dig in the filesystem
  376.      *                                 tree. This variable has to be initialized
  377.      *                                 with $this object.
  378.      * @return  Vector 
  379.      */
  380.     function getVectorFromTree&$folder_obj )
  381.     {
  382.         $vec_obj =new Vector();
  383.         for $i 0$i count$folder_obj->files )$i++ )
  384.             $vec_obj->add$folder_obj->files[$i);
  385.  
  386.         for $i 0$i count$folder_obj->folder )$i++ )
  387.         {
  388.             $return_obj =$this->getVectorFromTree$folder_obj->folder[$i);
  389.  
  390.             $vec_obj->mixed array_merge(
  391.                                             $vec_obj->mixed,
  392.                                             $return_obj->mixed
  393.                                          );
  394.         }
  395.         return $vec_obj;
  396.     }
  397.  
  398.  
  399.  
  400.    /**
  401.     * Sends a dirty formatted list of this filesystem to the browser.
  402.     *
  403.     * @version 1.0
  404.     * @since   0.1.0
  405.     * @author  Daniel Plücken <daniel@debakel.net>
  406.     * @access  public
  407.     * @return  void 
  408.     */
  409.    function echoTree()
  410.    {
  411.       for$i 0$i count$this->folder )$i++ )
  412.       {
  413.          echo $this->folder[$i]->getName()."<br>\n";
  414.          $this->folder[$i]->echoNameQueque();
  415.       }
  416.       for$i 0$i count$this->files )$i++ )
  417.          echo $this->files[$i]->getName()."<br>\n";
  418.    }
  419. // END of class FilesystemTree
  420. ?>

Documentation generated on Thu, 05 Jun 2008 19:11:38 +0200 by phpDocumentor 1.4.1