Created
August 17, 2011 15:53
-
-
Save thinkt4nk/1151847 to your computer and use it in GitHub Desktop.
Directory Enumerator
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 | |
/* | |
* Directory enumeration and presentation for jQuery TreeView Widget | |
* Author: Ryan Bales, 2011 | |
*/ | |
class DirectoryEnumerator | |
{ | |
private $enumeration_list = array(); | |
private $directory; | |
private $public_directory; | |
/** | |
* __construct | |
* The constructor | |
* @param string $directory | |
* @access public | |
* @return void | |
*/ | |
public function __construct($directory=null,$public_directory=null) | |
{ | |
if( $directory === null ) | |
$this->directory = SITE_LOC; | |
else | |
$this->directory = $directory; | |
if( $public_directory === null ) | |
$this->public_directory = ''; | |
else | |
$this->public_directory = $public_directory; | |
} | |
/** | |
* enumerate | |
* Bootstraps recursive enumeration | |
* @access public | |
* @return void | |
*/ | |
public function enumerate() | |
{ | |
return $this->recursively_enumerate($this->directory); | |
} | |
/** | |
* getRecursiveTree | |
* Method outputs the directory tree for jQuery's TreeView plugin | |
* @param array $directory | |
* @access public | |
* @return void | |
*/ | |
public function getRecursiveTree($directory,$parent=null) | |
{ | |
if( $parent === null ) | |
$parent = $this->public_directory; | |
// create the output buffer | |
$ob = ""; | |
foreach( $directory as $child_name => $child ) | |
{ | |
if( is_array($child) && !empty($child) ) | |
{ | |
$ob .= '<li class="closed">'; | |
} else { | |
$ob .= '<li>'; | |
} | |
if( is_array($child) ) | |
{ | |
$ob .= '<span class="folder">' . $child_name . '</span>'; | |
// recurse | |
$ob .= "<ul>" . $this->getRecursiveTree($child,implode('/',array($parent,$child_name))) . "</ul>"; | |
} | |
else { | |
// push file object | |
$ob .= '<li><span class="file"><a href="' . implode('/',array($parent,$child)) . '">' . $child . '</a></span></li>'; | |
} | |
$ob .= "</li>"; | |
} | |
return $ob; | |
} | |
/** | |
* recursively_enumerate | |
* Recursively enumerates a directory structure, returning a dictionary | |
* Directories are arrays, empty or containing their files, and files are simple strings | |
* @param string $directory | |
* @access private | |
* @return array | |
*/ | |
private function recursively_enumerate($directory) | |
{ | |
$directory_name_parts = explode('/',$directory); | |
$directory_basename = (!empty($directory_name_parts)) ? $directory_name_parts[(count($directory_name_parts)-1)] : null; | |
if( $directory_basename !== null ) | |
{ | |
// create the return object | |
$directory_list = array(); | |
// scan the directory | |
$children = scandir($directory); | |
foreach($children as $child) { | |
// don't recurse up the directory tree or stall or search DS_Store | |
if( !in_array($child,array('.','..','.DS_Store')) ) | |
{ | |
$child_pathname = sprintf("%s/%s",$directory,$child); | |
if( is_dir($child_pathname) ) | |
{ | |
// recurse | |
$directory_list[$child] = $this->recursively_enumerate($child_pathname); | |
} | |
elseif( is_file($child_pathname) ) | |
{ | |
// push | |
array_push($directory_list,$child); | |
} | |
} | |
} | |
return $directory_list; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment