Last active
August 29, 2015 14:22
-
-
Save jbdelhommeau/736880a36962887624cb to your computer and use it in GitHub Desktop.
Show files likes tree command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function tree() | |
{ | |
$nbFiles = iterator_count($this->getInnerIterator()); | |
if ($this->io && $nbFiles > 0) { | |
$this->io->write('.'); | |
$currentDir = array(); | |
$depth = $countDir = 0; | |
foreach ($this->getInnerIterator() as $key => $file) { | |
$relativePath = $file->getRelativePath(); | |
if (empty($relativePath)) { | |
$depth = 0; | |
$currentDir = array(); | |
} else { | |
$listDir = explode(DIRECTORY_SEPARATOR, $relativePath); | |
if ($dirDiff = array_diff_assoc($listDir, $currentDir)) { | |
$depth = count($listDir) - count($dirDiff); | |
foreach ($dirDiff as $dirName) { | |
$this->io->write( | |
sprintf( | |
'%s├── <info>%s</info>', | |
str_repeat(($depth > 0) ? '│ ': ' ', $depth), | |
$dirName | |
) | |
); | |
$depth++; | |
$countDir++; | |
} | |
} | |
$currentDir = $listDir; | |
} | |
$this->io->write( | |
sprintf( | |
'%s├── %s', | |
str_repeat(($depth > 0) ? '│ ': ' ', $depth), | |
$file->getFileName() | |
) | |
); | |
} | |
//Show total message | |
$dir = ''; | |
if ($countDir > 0) { | |
$dir = sprintf('%d %s, ', $countDir, ($countDir > 1) ? 'directories' : 'directory'); | |
} | |
$file = sprintf('%d %s', $nbFiles, ($nbFiles > 1) ? 'files' : 'file'); | |
$this->io->write(''); | |
$this->io->write($dir.$file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment