Ver Mensaje Individual
  #9 (permalink)  
Antiguo 12/04/2011, 18:17
Avatar de masterpuppet
masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: mostrar arbol de archivos y carpetas

A ver, podrías hacer un decorator, algo así:

Código PHP:
Ver original
  1. interface Renderer
  2. {
  3.     public function render();
  4. }
  5.  
  6. class HTMLTreeDecorator implements Renderer
  7. {
  8.     protected $_iterator;
  9.  
  10.     public function __construct(RecursiveIteratorIterator $it)
  11.     {
  12.         $this->_iterator = $it;
  13.     }
  14.  
  15.     public function render()
  16.     {
  17.        // iterate container
  18.         $prevDepth = -1;
  19.         $ulClass   = 'list';
  20.         $html      = '';
  21.  
  22.         foreach ($this->_iterator as $file) {
  23.  
  24.             if($this->_iterator->isDot()) continue;
  25.  
  26.             $depth = $this->_iterator->getDepth();
  27.  
  28.             if ($depth > $prevDepth) {
  29.  
  30.                 if ($ulClass && $depth ==  0) {
  31.                     $ulClass = ' class="' . $ulClass . '"';
  32.                 } else {
  33.                     $ulClass = '';
  34.                 }
  35.                 $html .= '<ul' . $ulClass . '>' . PHP_EOL;
  36.  
  37.             } else if ($prevDepth > $depth) {
  38.  
  39.                 for ($i = $prevDepth; $i > $depth; $i--) {
  40.                     $html .=  '    </li>' . PHP_EOL;
  41.                     $html .=  '</ul>' . PHP_EOL;
  42.                 }
  43.                 // close previous li tag
  44.                 $html .= '    </li>' . PHP_EOL;
  45.             } else {
  46.                 // close previous li tag
  47.                 $html .= '    </li>' . PHP_EOL;
  48.             }
  49.  
  50.             $html .= '    <li>' . PHP_EOL
  51.                    . '        ' . $file . PHP_EOL;
  52.  
  53.             // store as previous depth for next iteration
  54.             $prevDepth = $depth;
  55.         }
  56.  
  57.         if ($html) {
  58.             // done iterating container; close open ul/li tags
  59.             for ($i = $prevDepth+1; $i > 0; $i--) {
  60.                 $html .= '    </li>' . PHP_EOL
  61.                        . '</ul>' . PHP_EOL;
  62.             }
  63.             $html = rtrim($html, PHP_EOL);
  64.         }
  65.  
  66.         return $html;
  67.  
  68.     }
  69.  
  70.     public function __toString()
  71.     {
  72.         try {
  73.             return $this->render();
  74.         } catch (Exception $e) {}
  75.     }
  76.  
  77. }

y para utilizarlo seria:

Código PHP:
Ver original
  1. $path = '/path/to/dir';
  2. $iterator = new RecursiveIteratorIterator(
  3.                 new RecursiveDirectoryIterator($path),
  4.                 RecursiveIteratorIterator::SELF_FIRST);
  5.  
  6. $decorator = new HTMLTreeDecorator($iterator);
  7. echo $decorator;

Nota: el render es parte de Zend_Navigation :)

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)