Scripts

FlexLoader __autoload

Gewoon de onderstaande code in een file plakken, bijvoorbeeld flexloader.php In je index flexloader.php includen en vrolijk je classes instatieren zonder je zorgen te maken waar ze staan! Meer info over wat autoload precies doet is hier te vinden http://php.net/autoload Opmerkingen zijn welkom!

flexloader-autoload
Een voorbeeld autload functie, ook kan je natuurlijk zelf een functie maken die in SPL te hangen valt
[code]
<?php
/**
 * Automagic PHP autoload function
 *
 * @param string $class_name
 * @return bool
 */
function __autoload($class_name)
{
	static $flexloader = null;
	
	if(!isset($flexloader)) {
		$cachepath = './cache/flexloader.cache.php';
		$classespath = realpath('./classes');
		// Cachefile path, directories to scan, extensions, ignore hidden files, follow symlinks
		$flexloader = new FlexLoader($cachepath, array($classespath));
	}
	return $flexloader->loadClass($class_name);
}
[/code]


[code]
<?php
/**
 * FlexLoader
 * 
 * @author Christiaan Baartse <[email protected]>
 * @version 0.0.2
 * @since 28/02/2008
 */
class FlexLoader {
	
	/**
	 * Directories to scan
	 *
	 * @var array
	 */
	protected $directories;
	
	/**
	 * Cache filename
	 *
	 * @var string
	 */
	protected $cache_file;
	
	/**
	 * Class to file mapping
	 *
	 * @var array
	 */
	protected $class2file;
	
	/**
	 * Extensions to scan
	 *
	 * @var array
	 */
	protected $extensions;
	
	/**
	 * Enable scanning of hidden files
	 *
	 * @var bool
	 */
	protected $hidden;
	
	/**
	 * Enable following of symlinks
	 *
	 * @var bool
	 */
	protected $symlinks;
		
	/**
	 * Construct
	 *
	 * @param string $cache_file
	 * @param array $directories
	 * @param array $extensions
	 * @param bool $hidden
	 * @param bool $symlinks
	 */
	public function __construct($cache_file = 'flexloader.cache.php', $directories = array('classes'), $extensions = array('.php'), $hidden = false, $symlinks = true)
	{
		$this->cache_file = $cache_file;
		$this->directories = $directories;
		$this->extensions = $extensions;
		$this->hidden = $hidden;
		$this->symlinks = $symlinks;
		
		if( ! file_exists($this->cache_file) && ! $this->createCache()) {
			throw new Exception("FlexLoader: Couldn't create cache '".$this->cache_file."'");
		}else{
			if(include($this->cache_file)) {
				$this->class2file = @$flexloader_classes;
			}else{
				throw new Exception("FlexLoader: Couldn't load cache from '".$this->cache_file."'");
			}
		} 	
	}
	
	/**
	 * Load file containing the given class
	 *
	 * @param string $class_name
	 * @return boolean
	 */
	public function loadClass($class)
	{
		$class = strtolower($class);
		
		if(isset($this->class2file[$class]) && include($this->class2file[$class])) {
			return TRUE;
		}
		else{ //Comment out this whole else on production sites
			$this->class2file = array();
			$this->createCache();
			if(isset($this->class2file[$class]) && include($this->class2file[$class])) {
				return TRUE;
			}
		}// So untill here

		throw new Exception(__CLASS__." couldn't ".__METHOD__." ".$class);
	}
	
	/**
	 * Create the cachefile
	 *
	 * @return bool
	 */
	public function createCache()
	{
		foreach($this->directories as $directory) {
			$this->parseDirectory($directory.'/');
		}
		
		$cache = "<?php\n\$flexloader_classes = ";
		$cache.= var_export($this->class2file, true);
		$cache.= ';';		
		file_put_contents($this->cache_file, $cache);
		
		return TRUE;
	}
	
	/**
	 * Parse Directory for files containing classes
	 *
	 * @param string $directory Pathname of directory
	 * @return bool
	 */
	protected function parseDirectory($directory)
	{
		if( ! is_dir($directory)) return FALSE;
		if( ! $dhandle = opendir($directory)) return FALSE;
		while(($file = readdir($dhandle)) !== false) {
			if($file[0] == '.' && ! $this->hidden) continue;

			$file_path = $directory.$file;
			if(is_dir($file_path)){
				if($file == '.' || $file == '..') continue;
				$this->parseDirectory($file_path.'/');			
			}elseif($this->symlinks && is_link($file_path)){
				$this->parseDirectory($file_path.'/');
			}elseif(is_file($file_path)){
				if(empty($this->extensions) || in_array(substr($file, strrpos($file, '.')), $this->extensions)) {
					preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file_path), $result);
					if( ! empty($result[1])){
						foreach($result[1] as $class_name) {
							$this->class2file[strtolower($class_name)] = $file_path;
						}
					}
				}
			}
		}				
		return TRUE;
	}
	
}
[/code]

Reacties

0
Nog geen reacties.