Tree.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // | Authors: Wolfram Kriesing <wolfram@kriesing.de> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Tree.php,v 1.4 2003/01/04 11:56:27 mj Exp $
  20. require_once('PEAR.php');
  21. /**
  22. * the DB interface to the tree class
  23. *
  24. * @access public
  25. * @author Wolfram Kriesing <wolfram@kriesing.de>
  26. * @version 2001/06/27
  27. * @package Tree
  28. */
  29. class Tree extends PEAR
  30. {
  31. /**
  32. * setup an object which works on trees that are temporarily saved in memory
  33. * dont use with huge trees, suggested is a maximum size of tree of
  34. * about 1000-5000 elements since the entire tree is read at once from the data source.
  35. * use this to instanciate a class of a tree if you i.e.
  36. * - need the entire tree at once
  37. * - want to work on the tree w/o db-access for every call
  38. * since this set of classes loads the entire tree into the memory, you should
  39. * be aware about the size of the tree you work on using this class
  40. * for one you should know how efficient this kind of tree class is on
  41. * your data source (i.e. db) and what effect it has reading the entire tree at once.
  42. * on small trees, like upto about 1000 elements an instance of this class
  43. * will give you very powerful means to manage/modify the tree, no matter from which
  44. * data source it comes, either from a nested-DB, simple-DB, XML-File/String or
  45. * whatever is implemented
  46. *
  47. * @version 2002/02/05
  48. * @access public
  49. * @author Wolfram Kriesing <wolfram@kriesing.de>
  50. * @param string $type the kind of data source this class shall work on initially,
  51. * you can still switch later, by using "setDataSource"
  52. * to i.e. export data from a DB to XML, or whatever implementation might exist some day
  53. * currently available types are: 'DBsimple', 'XML'
  54. * TODO: DBnested (which i think should be implemented after Dynamic/DBnested, since it would only need
  55. * to use it's methods to manage the tree)
  56. * @param $dsn $dsn the dsn, or filename, etc., empty i.e. for XML if you use setupByRawData
  57. */
  58. function &setupMemory( $type , $dsn='' , $options=array() )
  59. # if anyone knows a better name it would be great to change it, since "setupMemory" kind of reflects it
  60. # but i think it's not obvious if you dont know what is meant
  61. {
  62. require_once('Tree/Memory.php');
  63. return new Tree_Memory( $type , $dsn , $options );
  64. } // end of function
  65. /**
  66. * setup an object that works on trees where each element(s) are read on demand from the given data source
  67. * actually this was intended to serve for nested trees which are read from
  68. * the db up on demand, since it doesnt make sense to read a huge tree into
  69. * the memory when you only want to access one level of this tree
  70. *
  71. * in short: an instance returned by this method works on a tree by mapping
  72. * every request (such as getChild, getParent ...) to the data source defined to work on
  73. *
  74. * @version 2002/02/05
  75. * @access public
  76. * @author Wolfram Kriesing <wolfram@kriesing.de>
  77. * @param
  78. */
  79. function &setupDynamic( $type , $dsn , $options=array() )
  80. # "dynamic" stands for retreiving a tree(chunk) dynamically when needed,
  81. # better name would be great :-)
  82. {
  83. require_once("Tree/Dynamic/$type.php");
  84. $className = 'Tree_Dynamic_'.$type;
  85. $obj = & new $className( $dsn , $options );
  86. return $obj;
  87. } // end of function
  88. /**
  89. * this is just a wrapper around the two setup methods above
  90. * some example calls:
  91. * <code>
  92. * $tree = Tree::setup( 'Dynamic_DBnested' , 'mysql://root@localhost/test' , array('table'=>'nestedTree') );
  93. * $tree = Tree::setup( 'Memory_DBsimple' , 'mysql://root@localhost/test' , array('table'=>'simpleTree') );
  94. * $tree = Tree::setup( 'Memory_XML' , '/path/to/some/xml/file.xml' );
  95. * </code>
  96. *
  97. * you can call the following too, but the functions/classes are not implemented yet
  98. * or not finished
  99. * <code>
  100. * $tree = Tree::setup( 'Memory_DBnested' , 'mysql://root@localhost/test' , array('table'=>'nestedTree') );
  101. * $tree = Tree::setup( 'Dynamic_XML' , '/path/to/some/xml/file.xml' );
  102. * </code>
  103. *
  104. * and those would be really cool to have one day:
  105. * LDAP, Filesystem, WSDL, ...
  106. *
  107. * @access private
  108. * @version 2002/03/07
  109. * @author Wolfram Kriesing <wolfram@kriesing.de>
  110. * @param
  111. * @return
  112. */
  113. function setup( $type , $dsn , $options=array() )
  114. {
  115. $type = explode( '_' , $type );
  116. $method = 'setup'.$type[0];
  117. return Tree::$method( $type[1] , $dsn , $options );
  118. }
  119. }
  120. ?>