<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/

class core_autoload
{
	private static $_gz_class = array(
		'TPL'				=> 'class/cls_template.inc.php',	
		'FORMAT'			=> 'class/cls_format.inc.php',
		'HTTP'				=> 'class/cls_http.inc.php',
		'H'					=> 'class/cls_helper.inc.php',
		'USER'				=> 'class/cls_user.inc.php',
		'ACTION_LOG'		=> 'class/cls_action_log_class.inc.php',
	);
	
	public function __construct()
	{
		set_include_path(ASK_PATH);
		
		foreach (self::$_gz_class AS $key => $val)
		{
			self::$_gz_class[$key] = ASK_PATH . $val;
		}
		
		spl_autoload_register(array($this, 'loader'));
	}
    
    private static function loader($class_name)
	{
		$require_file = ASK_PATH . preg_replace('#_+#', '/', $class_name) . '.php';
		
		if (file_exists($require_file))
		{
			return require $require_file;
		}
		
		if (class_exists('ASK_APP', false))
		{
			self::$_gz_class = array_merge(self::$_gz_class, ASK_APP::plugins()->model());
		}
		
		// Displays are attending if the built-in
		if (isset(self::$_gz_class[$class_name]))
		{
			return require(self::$_gz_class[$class_name]);
		}
		// Find models Directory
		else if (file_exists(ROOT_PATH . 'models/' . str_replace(array('_class', '_'), array('', '/'), $class_name) . '.php'))
		{
			return require(ROOT_PATH . 'models/' . str_replace(array('_class', '_'), array('', '/'), $class_name) . '.php');
		}
		// Find includes
		else if (file_exists(ASK_PATH . 'class/' . $class_name . '.inc.php'))
		{
			return require(ASK_PATH . 'class/' . $class_name . '.inc.php');
		}
	}
}