Scripts

Weergeef je source code

Deze class weergeeft de inhoud van alle bestanden met de aangegeven extensie in een opgegeven map. Het is mogelijk om recursieve aan en uit te zetten (true/false) en om onzichtbare bestanden te weergeven of niet (true/false). Er wordt automatisch inhoudsopgaven gemaakt! voorbeeld (afbeelding op) : http://img228.imageshack.us/img228/4084/6948a0364013e467b1dd85e.png

class.displayCode.php
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * *
 * Author 		: Anthony Huebers			
 * Email	   	: [email protected]
 * 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>';
		}
	}	
}
?>
example.displayCode.php
<?php
/* *
 * Include class
 */
	require_once('class.displayCode.php');
/* *
 * Path you want to display
 */
	$path = realpath( dirname(__FILE__).'/..').'/test/';
/* *
 * Initialize class
 */	
	$displayCode = new displayCode();
/* *
 * Display code by path
 */	
	$displayCode->displayCodeByPath($path);
?>

Reacties

0
Nog geen reacties.