Skip to content

Instantly share code, notes, and snippets.

@jbdelhommeau
Last active August 29, 2015 14:22
Show Gist options
  • Save jbdelhommeau/736880a36962887624cb to your computer and use it in GitHub Desktop.
Save jbdelhommeau/736880a36962887624cb to your computer and use it in GitHub Desktop.
Show files likes tree command
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