[ Root ] [ Search ] [ Index ]

PHP Cross Reference of bbPress Trunk

Provided by Yoast

title

Body

[close]

/bb-includes/ -> class.bb-dir-map.php (source)

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


Generated: Mon Nov 15 04:45:27 2010 Cross-referenced by PHPXref 0.7