[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/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 __construct( $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 BB_Dir_Map( $root, $args = '' ) {
  33          $this->__construct( $root, $args );
  34      }
  35  
  36  	function parse_args( $args ) {
  37          // callback: should be callable
  38          // callback_args: additional args to pass to callback
  39          // apply_to: all, files, dirs
  40          // keep_empty: (bool)
  41          // recurse: (int) depth, -1 = infinite
  42          // dots: true (everything), false (nothing), nosvn
  43          $defaults = array( 'callback' => false, 'callback_args' => false, 'keep_empty' => false, 'apply_to' => 'files', 'recurse' => -1, 'dots' => false );
  44          $this->callback = is_array($args) && isset($args['callback']) ? $args['callback'] : false;
  45          $args = wp_parse_args( $args, $defaults );
  46  
  47          foreach ( array('callback', 'keep_empty', 'dots') as $a )
  48              if ( 'false' == $args[$a] )
  49                  $args[$a] = false;
  50              elseif ( 'true' == $args[$a] )
  51                  $args[$a] = true;
  52  
  53          if ( !isset($this->callback) )
  54              $this->callback = $args['callback'];
  55          if ( !is_callable($this->callback) )
  56              $this->callback = false;
  57          $this->callback_args = is_array($args['callback_args']) ? $args['callback_args'] : array();
  58  
  59          $this->keep_empty = (bool) $args['keep_empty'];
  60  
  61          $_apply_to = array( 'files' => 1, 'dirs' => 2, 'all' => 3 ); // This begs to be bitwise
  62          $this->apply_to = @$_apply_to[$args['apply_to']];
  63  
  64          $this->recurse = (int) $args['recurse'];
  65  
  66          $_dots = array( 1 => 3, 0 => 0, 'nosvn' => 1 ); // bitwise here is a little silly
  67          $this->dots = @$_dots[$args['dots']];
  68      }
  69  
  70  	function map( $root = false ) {
  71          $return = array();
  72          $_dir = dir($root ? $root : $this->_current_root);
  73          while ( $_dir && false !== ( $this->_current_file = $_dir->read() ) ) {
  74              if ( in_array($this->_current_file, array('.', '..')) )
  75                  continue;
  76              if ( !$this->dots && '.' == $this->_current_file{0} )
  77                  continue;
  78  
  79              $item = $_dir->path . DIRECTORY_SEPARATOR . $this->_current_file;
  80              $_item = substr( $item, strlen($this->root) + 1 );
  81              $_callback_args = $this->callback_args;
  82              array_push( $_callback_args, $item, $_item ); // $item, $_item will be last two args
  83              if ( is_dir($item) )  { // dir stuff
  84                  if ( 1 & $this->dots && in_array($this->_current_file, array('.svn', 'CVS')) )
  85                      continue;
  86                  if ( 2 & $this->apply_to ) {
  87                      $result = $this->callback ? call_user_func_array($this->callback, $_callback_args) : true;
  88                      if ( $result || $this->keep_empty )
  89                          $this->flat[$_item] = $result;
  90                  }
  91                  if ( 0 > $this->recurse || $this->recurse ) {
  92                      $this->recurse--;
  93                      $this->map( $item );
  94                      $this->recurse++;
  95                  }
  96              } else { // file stuff
  97                  if ( !(1 & $this->apply_to) )
  98                      continue;
  99                  $result = $this->callback ? call_user_func_array($this->callback, $_callback_args) : true;
 100                  if ( $result || $this->keep_empty )
 101                      $this->flat[$_item] = $result;
 102              }
 103          }
 104      }
 105  
 106  	function get_results() {
 107          return is_wp_error( $this->error ) ? $this->error : $this->flat;
 108      }
 109  }


Generated: Thu Dec 7 01:01:35 2017 Cross-referenced by PHPXref 0.7.1