Created
March 5, 2012 21:53
-
-
Save erikfrerejean/1981387 to your computer and use it in GitHub Desktop.
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
<?php | |
function dirToArray($path, $dir = '') | |
{ | |
$path = (!empty($path)) ? rtrim($path, '/') . '/' : $path; | |
$dir = (!empty($dir)) ? rtrim($dir, '/') . '/' : $dir; | |
$path = $path . $dir; | |
$list = array(); | |
$d = dir($path); | |
while (false !== ($entry = $d->read())) | |
{ | |
$file = new SplFileInfo($path . $entry); | |
if (substr($file->getFilename(), 0, 1) == '.') | |
{ | |
continue; | |
} | |
if ($file->isDir()) | |
{ | |
if (empty($list[$file->getFilename()])) | |
{ | |
$list[$file->getFilename()] = array(); | |
} | |
$list[$file->getFilename()] = &dirToArray($path, $file->getFilename()); | |
} | |
else | |
{ | |
$list[] = $file->getFilename(); | |
} | |
} | |
return $list; | |
} |
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
<?php | |
class tree | |
{ | |
private $t; | |
public function __construct($path) | |
{ | |
$this->t = new dirNode(new SplFileInfo($path)); | |
$this->build($path); | |
} | |
public function build($path = '', $dir = '', node $node = null) | |
{ | |
$path = (!empty($path)) ? rtrim($path, '/') . '/' : $path; | |
$dir = (!empty($dir)) ? rtrim($dir, '/') . '/' : $dir; | |
$node = (is_null($node)) ? $this->t : $node; | |
$path = $path . $dir; | |
$d = dir($path); | |
while (false !== ($entry = $d->read())) | |
{ | |
$file = new SplFileInfo($path . $entry); | |
if (substr($file->getFilename(), 0, 1) == '.') | |
{ | |
continue; | |
} | |
if ($file->isDir()) | |
{ | |
$n = new dirNode($file); | |
$node->addNode($this->build($path, $file->getFilename(), $n)); | |
} | |
else | |
{ | |
$node->addNode(new fileNode($file)); | |
} | |
} | |
return $node; | |
} | |
} | |
interface node | |
{ | |
} | |
class nodeBase implements node | |
{ | |
} | |
class fileNode extends nodeBase | |
{ | |
protected $file; | |
public function __construct(SplFileInfo $file) | |
{ | |
$this->file = $file; | |
} | |
} | |
class dirNode extends nodeBase | |
{ | |
protected $dir; | |
protected $children; | |
public function __construct(SplFileInfo $dir) | |
{ | |
$this->dir = $dir; | |
} | |
public function addNode(node $node) | |
{ | |
$this->children[] = $node; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment