Unifix knowledge database - The original one

Dashboard - News - Search - List - About - Admin


SOURCE: Generic PHP Autoloader

Author:mike - Mon Oct 29 04:00:00 2007

<?
	// COPYRIGHT (c) 2007 MC Solutions
	/**
	 * Class to handle autoloading of Php class files
	 */
	Class AutoLoader
	{

		private $files;
		private $error;
		
		private $use_session_cache = "true";

		
		/**
		 *	Tells the AutoLoader what to do when duplicate class files are found, I_ALL Include all,  I_FIRST Include first,  I_LAST Include last, I_FAIL Fail with error message
		 * 
		 *  @var [I_ALL,I_FIRST,I_LAST,I_FAIL]
		 */
		public $on_duplicate = I_ALL;		

		/**
		 * Tells the Autoloader how to cache the directory structure, C_SESSION for each session, C_PAGEVIEW for each pageview, C_NEVER dont cache
		 *
		 * @var [C_SESSION,C_PAGEVIEW,C_NEVER]
		 */
		public $on_cache = C_SESSION;	
		
		/**
		 * Tells AutoLoader which dir to look for class files in
		 *
		 * @var string
		 */
		
		public $on_classes_dir = './';											
		
		/**
		 * Used to recurse the directory structure containing the 
		 * 
		 * @param string $dir
		 * @return array of files in the directory
		 */
		private function RecurseDir($dir)
		{	
			$dh=opendir($dir);
			while ($file=readdir($dh))
			{
				if (($file != ".") and ($file != ".."))
				{
					if (filetype($dir.$file) == 'dir')	
					{
						$return[$dir.$file]= $this->RecurseDir($dir.$file.'/');
					}
					else 
					{
						$return[]=$file;
						if ($this->files[$file])
						{
							//print "Duplicate detected $dir$file and $this->files[$file]<br>";
							
							if (!is_array($this->files[$file]))
							{
								$tmp=$this->files[$file];
								$this->files[$file]=array();
								$this->files[$file][]=$tmp;
							} 
							
							$this->files[$file][]=$dir.$file;
							
						} else {
							$this->files[$file]="$dir$file";
						}
					}
				}	
			}
		
			closedir($dh);
			
			return $this->files;
		}		
	
		/**
		 * Checks if sessions are needed, and tries to initiate them if they are not allready
		 *
		 */
		public function __construct()
		{
			if ($this->on_cache == "C_SESSION")
			{
				if (!headers_sent())
				{
					session_start();
				} else {
					// Output er sent, se om sessions er startet
					if (isset($_SESSION))
					{
						// Sessions er startet, vi koerer videre
					} else {
						// Sessions er ikke startet, session-cache deaktiveres
						$this->use_session_cache="false";	
					}
				}
			}	
		}
		
		/**
		 * Tries to include a PHP File
		 *
		 * @param string $file 
		 * @return boolean true if inclusions succeded
		 */
		private function try_include($file)
		{
			if (@include($file))
			{
				return true;
			} else {
				return false;	
			}
		}
		
		/**
		 * This function contains the semantics required to determine what file to load
		 *
		 * @param string $classname Name of wanted class
		 * @return true when file found 
		 */
		private function performloadclass($classname)
		{
			if ($this->try_include($classname.".php")) { return true;}
			if ($this->try_include($classname.".class.php")) { return true;}
					
			if (($this->use_session_cache=="false") and ($this->on_cache=="C_SESSION"))
			{
				print "Defaulting";
				$this->on_cache=C_PAGEVIEW;
			}
			
			switch ($this->on_cache)
			{
				case C_SESSION:
					
					if (!$_SESSION['_CLASS_CACHE'])	
					{
						$this->files='';
						$_SESSION['_CLASS_CACHE']=$this->RecurseDir($this->on_classes_dir);	
					} 
					$this->files=$_SESSION['_CLASS_CACHE'];						
				break;

				case C_PAGEVIEW:
					if (!is_array($this->files))
					{
						$this->files='';
						$this->files=$this->RecurseDir($this->on_classes_dir);	
					}
				break;
					
				case C_NEVER:
					$this->files='';
					$this->files=$this->RecurseDir($this->on_classes_dir);	
				break;				
			}
			
			if ($this->files[$classname.".php"])
			{
				$filename=$classname.".php";
			}
			else if ($this->files[$classname.".class.php"])
			{
				$filename=$classname.".class.php";
			} else {
				$this->error="Not found";
				return false;
			}
			if (is_array($this->files[$filename]))
			{
				switch ($this->on_duplicate)
				{
					case "I_ALL":
						foreach ($this->files[$filename] as $k)
						{
							print "Include $k<br>";
							include($k);
						}
						return true;
					break;
					
					case "I_FIRST":
						$this->try_include($this->files[$filename][0]);					
						return true;
					break;
					
					case "I_LAST":
						$this->try_include($this->files[$filename][count($this->files[$filename])-1]);					
						return true;
					break;
					
					case "I_FAIL":
						$this->error="Duplicate files found";
						return false;
					break;
					
					default:
						$this->error="Wrong on_duplicate settings";	
						return false;
					break;	
				}
			}
			else 
			{
				$this->try_include($this->files[$filename]);
				return true;
			}
		}
		
		/**
		 * This method are invoked by __autoload
		 *
		 * @param string $classname Name of wanted class
		 */
		public function loadclass($classname)
		{
			if (!$this->performloadclass($classname))
			{
				print "<strong>Klassefejl: $classname</strong><br><br>Kunne ikke findes i:<br>";
				print "<ul>";
				print "<li>$classname.php i php include_path samt i $this->on_classes_dir og dennes undermapper</li>";
				print "<li>$classname.class.php i php include_path samt i $this->on_classes_dir og dennes undermapper</li>";
				print "</ul>";
				if ($this->error)
				{
					print $this->error;
				}
			}
		}
		
	}	

	$_AutoLoader = new AutoLoader();
	$_AutoLoader->on_classes_dir='./classes/';
	$_AutoLoader->on_duplicate=I_ALL;
	
	function __autoload($classname)
	{
		global $_AutoLoader;
		$_AutoLoader->loadclass($classname);
	}
	
?>



Copyright(c) Unifix.org 2002-2011


Dashboard - News - Search - List - About - Admin
 
Powered by Mikjaer Consulting Solutions - Hosted by Specialhosting.dk