BB_Dir_Map

The BuddyPress BB Dir Map class.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bbpress/bb-includes/class.bb-dir-map.php  
  1. class BB_Dir_Map { 
  2. var $root; 
  3. var $callback; 
  4. var $callback_args; 
  5. var $keep_empty; 
  6. var $apply_to; 
  7. var $recurse; 
  8. var $dots; 
  9. var $flat = array(); 
  10. var $error = false; 
  11.  
  12. var $_current_root; 
  13. var $_current_file; 
  14.  
  15. function __construct( $root, $args = '' ) { 
  16. if ( !is_dir( $root ) ) { 
  17. $this->error = new WP_Error( 'bb_dir_map', __('Not a valid directory') ); 
  18. return; 
  19.  
  20. $this->parse_args( $args ); 
  21. if ( is_null($this->apply_to) || is_null($this->dots) ) { 
  22. $this->error = new WP_Error( 'bb_dir_map', __('Invalid arguments') ); 
  23. return; 
  24. $this->_current_root = $this->root = rtrim($root, '/\\'); 
  25. $this->map(); 
  26.  
  27. function BB_Dir_Map( $root, $args = '' ) { 
  28. $this->__construct( $root, $args ); 
  29.  
  30. function parse_args( $args ) { 
  31. // callback: should be callable 
  32. // callback_args: additional args to pass to callback 
  33. // apply_to: all, files, dirs 
  34. // keep_empty: (bool) 
  35. // recurse: (int) depth, -1 = infinite 
  36. // dots: true (everything), false (nothing), nosvn 
  37. $defaults = array( 'callback' => false, 'callback_args' => false, 'keep_empty' => false, 'apply_to' => 'files', 'recurse' => -1, 'dots' => false ); 
  38. $this->callback = is_array($args) && isset($args['callback']) ? $args['callback'] : false; 
  39. $args = wp_parse_args( $args, $defaults ); 
  40.  
  41. foreach ( array('callback', 'keep_empty', 'dots') as $a ) 
  42. if ( 'false' == $args[$a] ) 
  43. $args[$a] = false; 
  44. elseif ( 'true' == $args[$a] ) 
  45. $args[$a] = true; 
  46.  
  47. if ( !isset($this->callback) ) 
  48. $this->callback = $args['callback']; 
  49. if ( !is_callable($this->callback) ) 
  50. $this->callback = false; 
  51. $this->callback_args = is_array($args['callback_args']) ? $args['callback_args'] : array(); 
  52.  
  53. $this->keep_empty = (bool) $args['keep_empty']; 
  54.  
  55. $_apply_to = array( 'files' => 1, 'dirs' => 2, 'all' => 3 ); // This begs to be bitwise 
  56. $this->apply_to = @$_apply_to[$args['apply_to']]; 
  57.  
  58. $this->recurse = (int) $args['recurse']; 
  59.  
  60. $_dots = array( 1 => 3, 0 => 0, 'nosvn' => 1 ); // bitwise here is a little silly 
  61. $this->dots = @$_dots[$args['dots']]; 
  62.  
  63. function map( $root = false ) { 
  64. $return = array(); 
  65. $_dir = dir($root ? $root : $this->_current_root); 
  66. while ( $_dir && false !== ( $this->_current_file = $_dir->read() ) ) { 
  67. if ( in_array($this->_current_file, array('.', '..')) ) 
  68. continue; 
  69. if ( !$this->dots && '.' == $this->_current_file{0} ) 
  70. continue; 
  71.  
  72. $item = $_dir->path . DIRECTORY_SEPARATOR . $this->_current_file; 
  73. $_item = substr( $item, strlen($this->root) + 1 ); 
  74. $_callback_args = $this->callback_args; 
  75. array_push( $_callback_args, $item, $_item ); // $item, $_item will be last two args 
  76. if ( is_dir($item) ) { // dir stuff 
  77. if ( 1 & $this->dots && in_array($this->_current_file, array('.svn', 'CVS')) ) 
  78. continue; 
  79. if ( 2 & $this->apply_to ) { 
  80. $result = $this->callback ? call_user_func_array($this->callback, $_callback_args) : true; 
  81. if ( $result || $this->keep_empty ) 
  82. $this->flat[$_item] = $result; 
  83. if ( 0 > $this->recurse || $this->recurse ) { 
  84. $this->recurse--; 
  85. $this->map( $item ); 
  86. $this->recurse++; 
  87. } else { // file stuff 
  88. if ( !(1 & $this->apply_to) ) 
  89. continue; 
  90. $result = $this->callback ? call_user_func_array($this->callback, $_callback_args) : true; 
  91. if ( $result || $this->keep_empty ) 
  92. $this->flat[$_item] = $result; 
  93.  
  94. function get_results() { 
  95. return is_wp_error( $this->error ) ? $this->error : $this->flat;