Skip to content

Instantly share code, notes, and snippets.

@hirak
Created January 18, 2012 17:04
Show Gist options
  • Save hirak/1634083 to your computer and use it in GitHub Desktop.
Save hirak/1634083 to your computer and use it in GitHub Desktop.
RecursiveTreeIteratorでtreeコマンドを実装 ref: http://qiita.com/Hiraku/items/4da9fe08ea83773f9631
#!/usr/bin/env php
<?php
/**
* treeコマンドのPHP5.3による実装例。
*/
namespace Tree;
//iterator類は名前が長いので短縮
use RecursiveDirectoryIterator as RDI,
RecursiveFilterIterator as RFI,
RecursiveTreeIterator as RTI;
$opt = getopt('adL:');
$di = new RDI('.');
// -a で"."から始まるファイルも表示。 --------------
// 通常は"."をフィルタする
if (!isset($opt['a'])) {
$di = new DotFilter($di);
}
class DotFilter extends RFI {
function accept() {
$fname = $this->current()->getFilename();
return $fname[0] !== '.';
}
}
// -d で ディレクトリのみにフィルタ -----------------
if (isset($opt['d'])) {
$di = new \ParentIterator($di);
}
$rti = new RTI($di, RTI::BYPASS_CURRENT);
// デザインを少し変更。Unix風
$rti->setPrefixPart(RTI::PREFIX_END_LAST, '`-');
$rti->setPrefixPart(RTI::PREFIX_RIGHT, '- ');
// -L n で深さを指定
if (isset($opt['L'])) {
$rti->setMaxDepth((int)$opt['L'] - 1);
}
foreach ($rti as $file) {
echo $rti->getPrefix();
echo $file->getFilename();
echo PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment