<?php

class Autoload {

    private $_options;
	public  static $_instance;
	
    public static function getInstance($options=null)
    {
        if( ! (self::$_instance instanceof self) ) {
            self::$_instance = new self($options);
        }
        return self::$_instance;
    }

    public function __construct($options=null)
    {
		spl_autoload_register(array($this,'engine'));
		spl_autoload_register(array($this,'database'));
		spl_autoload_register(array($this,'library_template'));
		spl_autoload_register(array($this,'library_engine'));
		$this->setOptions($options);
    }
	
	private function setOptions($options)  
    {
       return $this->_options =	$options;
    }
	
    private function engine($class)
    {  
		set_include_path(__SYSTEM_PATH.'/engine/');
        spl_autoload_extensions('.engine.php');
        spl_autoload($class);
    }
	
	private function database($class)
    {  
		set_include_path(__SYSTEM_PATH.'/database/');
        spl_autoload_extensions('.'.strtolower(__DATABASE_ENGINE).'.php');
        spl_autoload($class);
    }
	
	private function library_template($class)
    {  
		$autoload_include_path 	= __LIBRARY_PATH.'/template/';
        $autoload_extension   	= '.class.php' ;
        $autoload_class_file	= $autoload_include_path.$class.$autoload_extension;
		if (trim($autoload_class_file)!="" && (substr_count($class,"Smarty")>0)) {
		    if(file_exists($autoload_class_file)) {
				include_once($autoload_class_file);
			} else {
				return false;
			}
		} else {
			return false;
		}
    }
	
	private function library_engine($class)
    {   
        $autoload_include_path 	= __LIBRARY_PATH.'/'.strtolower($class).'/';
        $autoload_extension   	= '.class.php' ;
        $autoload_class_file	= $autoload_include_path.$class.$autoload_extension;
		if (trim($autoload_class_file)!="" && (substr_count($class,"Controller")<1) && (substr_count($class,"Model")<1)) { 
			if(file_exists($autoload_class_file)) {
				include_once($autoload_class_file);
			} else {
				return false;
			}
		} else {
			return false;
		}
    }
}

?>