Location: PHPKode > scripts > Display code > display-code/class.displayCode.php
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * *
 * Author 		: Anthony Huebers			
 * Email	   	: hide@address.com
 * Name			: display code
 * Version 		: 1.0
 * Since		: 22-06-2010					
 * Last updated : 22-06-2010					
 * * * * * * * */
class displayCode {
/* *
 * Store settings
 */	
	private static $path, $recursive, $displayHidden;	
/* *
 * Store allowed extensions
 */	
	private static $allowedExtensions = array();
/* *
 * Store files
 */
	private static $files = array();
/* *
 * Display script by path
 */
	public function __construct($allowedExtensions = 'php, ini, js') {
	/* *
	 * Set allowed extensions
	 */
		return self::$allowedExtensions = explode(',', str_replace(' ', '', $allowedExtensions));
	}
	public function displayCodeByPath($path, $recursive = true, $displayHidden = false) {
	/* *
	 * Set start load time
	 */	
		$microtime = explode(' ', microtime());
		$startLoadTime = $microtime[1] + $microtime[0];
	/* *
	 * Store settings
	 */
		self::$path = $path;
		self::$recursive = $recursive;
		self::$displayHidden = $displayHidden;
	/* *
	 * Get files
	 */
		self::getFiles($path);
	/* *
	 * Create and print table of content
	 */	
		print self::createTableOfContent();
	/* *
	 * Highlight and print files
	 */	
		print self::highlightFiles();
	/* *
	 * Footer with load time
	 */	
		$microtime = explode(' ', microtime());
		$loadTime = $microtime[0] + $microtime[1] - $startLoadTime;
		print 'Developed by Anthony Huebers - Generated in '.substr($loadTime, 0, 10).' seconds';
	}
	private function getFiles($dir) {
	/* *
	 * Open directory
	 */	
		$dirHandler = opendir($dir);
		while ($file = readdir($dirHandler)) {
		/* *
		 * No hidden files (start with .)
		 */	
			if(self::$displayHidden === true || stripos($file, '.') !== 0) {
			/* *
			 * Check if it's a directory
			 */	
			    if(is_dir(($dir.$file)) && self::$recursive === true) {
			    /* *
			     * Recursive
			     */	
			    	self::getFiles($dir.$file.'/', self::$recursive);
			    }else{
			    /* *
			     * Get extension
			     */	
			    	$extension = substr(strrchr($file, '.'), 1);
			    /* *
			     * Store file
			     */	
			    	if(in_array($extension, self::$allowedExtensions)) {
			    		self::$files[] = $dir.$file;
			    	}
			    }
			}
		}
		closedir($dirHandler);		
	}
	private function createTableOfContent() {
	/* *
	 * Start
	 */
		print '<h1>Table Of Content:</h1><ul>';
	/* *
	 * Foreach file
	 */ 
		$i = 0;
		foreach (self::$files as $file) {
			$file = str_replace(self::$path, '', $file);
			print '<li><a href="#'.$i++.'">'.$file.'</a></li>';
		}
	/* *
	 * Close
	 */	
		print '</ul>';
	}
	private function highlightFiles() {
	/* *
	 * Foreach file
	 */
		$i = 0;
		foreach (self::$files as $file) {
			$filename = str_replace(self::$path, '', $file);
			echo '<a name="'.$i++.'"><h2>'.$filename.'</h2></a>';
			highlight_file($file);
			echo '<hr>';
		}
	}	
}
?>
Return current item: Display code