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

Source for file Filesystem.php

Documentation is available at Filesystem.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  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. // | Authors: Wolfram Kriesing <wolfram@kriesing.de>                      |
  17. // +----------------------------------------------------------------------+
  18. //
  19. //  $Id: Filesystem.php 320703 2011-12-08 22:08:40Z danielc $
  20.  
  21. require_once 'Tree/Error.php';
  22.  
  23. /**
  24.  * the Filesystem interface to the tree class
  25.  * this is a bit different, as id we use the entire path, since we know
  26.  * this is unique in a filesystem and an integer id could only be created
  27.  * virtually, it doesnt have a tight connection to the actual directory
  28.  * i.e. using 'add' with ids could fail since we dont know if we get the same
  29.  * id when we call 'add' with a parentId to create a new folder under since
  30.  * our id would be made up. So we use the complete path as id, which takes up
  31.  * a lot of memory, i know but since php is typeless its possible and it makes
  32.  * life easier when we want to create another dir, since we get the dir
  33.  * as parentId passed to the 'add' method, which makes it easy to create
  34.  * a new dir there :-)
  35.  * I also thought about hashing the path name but then the add method is
  36.  * not that easy to implement ... may be one day :-)
  37.  *
  38.  * @access     public
  39.  * @author     Wolfram Kriesing <wolfram@kriesing.de>
  40.  * @version    2001/06/27
  41.  * @package    Tree
  42.  */
  43.  {
  44.  
  45.     /**
  46.      * @access public
  47.      * @var    array   saves the options passed to the constructor
  48.      */
  49.     var $options =  array(
  50.                         'order'     => '',
  51.                         'columnNameMaps' => array(),
  52.                     );
  53.  
  54.     // {{{ Tree_Memory_Filesystem()
  55.  
  56.     /**
  57.      * set up this object
  58.      *
  59.      * @version    2002/08/23
  60.      * @access     public
  61.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  62.      * @param      string  $dsn    the path on the filesystem
  63.      * @param      array   $options  additional options you can set
  64.      */
  65.     function Tree_Memory_Filesystem ($path$options = array())
  66.     {
  67.         $this->_path $path;
  68.         // not in use currently
  69.         $this->_options $options;
  70.     }
  71.  
  72.     // }}}
  73.     // {{{ setup()
  74.  
  75.     /**
  76.      * retreive all the navigation data from the db and call build to build
  77.      * the tree in the array data and structure
  78.      *
  79.      * @version    2001/11/20
  80.      * @access     public
  81.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  82.      * @return     boolean     true on success
  83.      */
  84.     function setup()
  85.     {
  86.         // unset the data to be sure to get the real data again, no old data
  87.         unset($this->data);
  88.         if (is_dir($this->_path)) {
  89.             $this->data[$this->_path= array(
  90.                                           'id'       => $this->_path,
  91.                                           'name'     => $this->_path,
  92.                                           'parentId' => 0
  93.                                         );
  94.             $this->_setup($this->_path$this->_path);
  95.         }
  96.         return $this->data;
  97.     }
  98.  
  99.     // }}}
  100.     // {{{ _setup()
  101.  
  102.     function _setup($path$parentId = 0)
  103.     {
  104.         if ($handle opendir($path)) {
  105.             while (false !== ($file readdir($handle))) {
  106.                 if ($file != '.' && $file != '..'
  107.                     && is_dir("$path/$file")) {
  108.                     $this->data[= array(
  109.                                         'id'       => "$path/$file",
  110.                                         'name'     => $file,
  111.                                         'parentId' => $parentId
  112.                                     );
  113.                     $this->_setup("$path/$file""$path/$file");
  114.                 }
  115.             }
  116.             closedir($handle);
  117.         }
  118.     }
  119.  
  120.     // }}}
  121.     // {{{ add()
  122.  
  123.     /**
  124.      * this is tricky on a filesystem, since we are working with id's
  125.      * as identifiers and we need to be sure, the parentId to create
  126.      * a node under is the same as when the tree was last read.
  127.      * but this might be tricky.
  128.      */
  129.     function add($newValues$parent = 0$prevId = 0)
  130.     {
  131.         if (!$parent{
  132.             $parent $this->path;
  133.         }
  134.         # FIXXME do the mapping
  135.         if (!@mkdir("$parent/{$newValues['name']}"0700)) {
  136.             return $this->_raiseError(TREE_ERROR_CANNOT_CREATE_FOLDER,
  137.                                       $newValues['name'].' under '.$parent,
  138.                                       __LINE__
  139.                                      );
  140.         }
  141.         return "$parent/{$newValues['name']}";
  142.     }
  143.  
  144.     // }}}
  145.     // {{{ remove()
  146.  
  147.     function remove($id)
  148.     {
  149.         if (!@rmdir($id)) {
  150.             return $this->_throwError('couldnt remove dir '.$id__LINE__);
  151.         }
  152.         return true;
  153.     }
  154.  
  155.     // }}}
  156.     // {{{ copy()
  157.  
  158.     function copy($srcId$destId)
  159.     {
  160.         # if(!@copy($srcId, $destId)) this would only be for files :-)
  161.         # FIXXME loop through the directory to copy the children too !!!
  162.         $dest $destId.'/'.preg_replace('/^.*\//','',$srcId);
  163.         if (is_dir($dest)) {
  164.             return $this->_throwError(
  165.                     "couldnt copy, $destId already exists in $srcId "__LINE__
  166.                 );
  167.         }
  168.         if (!@mkdir($dest0700)) {
  169.             return $this->_throwError(
  170.                     "couldnt copy dir from $srcId to $destId "__LINE__
  171.                 );
  172.         }
  173.         return true;
  174.     }
  175.  
  176.     // }}}
  177.     // {{{ _prepareResults()
  178.  
  179.     /**
  180.      * prepare multiple results
  181.      *
  182.      * @see        _prepareResult()
  183.      * @access     private
  184.      * @version    2002/03/03
  185.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  186.      */
  187.     function _prepareResults($results)
  188.     {
  189.         $newResults = array();
  190.         foreach ($results as $aResult{
  191.             $newResults[$this->_prepareResult($aResult);
  192.         }
  193.         return $newResults;
  194.     }
  195.  
  196.     // }}}
  197.     // {{{ _prepareResult()
  198.  
  199.     /**
  200.      * map back the index names to get what is expected
  201.      *
  202.      * @access     private
  203.      * @version    2002/03/03
  204.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  205.      */
  206.     function _prepareResult($result)
  207.     {
  208.         $map $this->getOption('columnNameMaps');
  209.         if ($map{
  210.             foreach ($map as $key => $columnName{
  211.                 $result[$key$result[$columnName];
  212.                 unset($result[$columnName]);
  213.             }
  214.         }
  215.         return $result;
  216.     }
  217.  
  218.     // }}}
  219. }

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