Location: PHPKode > projects > Gemibloo > Gemibloo1_0_alpha1/application/core/class/class.PluginLoader.php
<?php
	define(EXPORT_PLUGINS_DIR,dirname(__FILE__).'/../../plugins/export/');
	define(IMPORT_PLUGINS_DIR,dirname(__FILE__).'/../../plugins/import/');

	/**
	* __autoload function
	*
	* @author Olivier G <hide@address.com>
	* @version 1.0
	* @license http://opensource.org/licenses/gpl-license.php GNU Public License
	* @link http://gemibloo.fr
	*/
	function __autoload($aClassname){
		$fullpath = EXPORT_PLUGINS_DIR.'class.'.$aClassname.'.php';
		//try export plugin folder
		if(file_exists($fullpath))
			require_once($fullpath);
		else{
			//try import plugin folder
			$fullpath =IMPORT_PLUGINS_DIR.'class.'.$aClassname.'.php';
			if(file_exists($fullpath))
				require_once($fullpath);
		}
	}//__autoload
	
	/**
	* LoaderException class
	*
	* @author Olivier G <hide@address.com>
	* @version 1.0
	* @license http://opensource.org/licenses/gpl-license.php GNU Public License
	* @link http://gemibloo.fr
	*/
	class LoaderException extends Exception {
		const NO_PLUGINS = 1001;
		const PLUGINS_EXECUTION_FAILED = 1002;
		
		public $response;
		
		/**
		* __construct method
		*/
		function __construct($msg, $code, $response=null) {
			parent::__construct($msg, $code);
			$this->response = $response;
		}//__construct
	}//LoaderException
		
	/**
	* PluginLoader class
	*
	*This abstract class manages 
	*
	* @author Olivier G <hide@address.com>
	* @version 1.0
	* @license http://opensource.org/licenses/gpl-license.php GNU Public License
	* @link http://gemibloo.fr
	*/	
	class PluginLoader{
		private  $_classnames = array();
		private $_pluginDir = '';
		/**
		* __construct method: 
		* 
		* Loop through files in plugin folder to get a list of class names
		*/
		public  function __construct($aPluginDir) {
			$this->_pluginDir = $aPluginDir;
			if(FALSE === $filenames = scandir($aPluginDir))
				throw new ImportLoaderException("No import plugins to load.", LoaderException::NO_PLUGINS);
			foreach($filenames as $filename){
				$filenamePart = explode('.',$filename);
				if( (3 == count($filenamePart)) && (strtolower($filenamePart[0]) == 'class') && (strtolower($filenamePart[2]) == 'php')){
					$this->_classnames[] = $filenamePart[1];//store filename
				}//if
			}//foreach
		}//__construct

		/**
		* fExecutePlugin method
		* 
		* Iterator through all plugins (singletons) to execute a function (use __autoload)
		*/
		public function fExecutePlugin($aFunctionName, $aFunctionArgs = ''){
			try{
				if( ($aFunctionArgs) && (is_array($aFunctionArgs)) )
					$aFunctionArgs = implode(",", $aFunctionArgs);
				$returnCode = false;
				foreach($this->_classnames as $classname){
					$returnCode = $returnCode || eval($classname.'::fGetInstance()->'.$aFunctionName.'('.$functionArgs.');');
				}//foreach
				return $returnCode;
			}catch (Exception $e) {
				throw new LoaderException("fExecutePlugin() failed: ".$e->getMessage(), LoaderException::PLUGINS_EXECUTION_FAILED);
			}
		}//fExecutePlugin

		/**
		* fGetParametersTemplateFile method
		* 
		* Concate all parameters file found in the current plugin folder
		*/
		public function fGetParametersTemplateFile(&$aContent){
			foreach($this->_classnames as $classname){
				$fullpath = $this->_pluginDir.$classname.'.param';
				if(file_exists($fullpath)){
					if( FALSE === ($handle = fopen($fullpath, 'r')))
						return FALSE;
					if( FALSE === ($aContent .= fread($handle, filesize($fullpath)))){
						fclose($handle); return FALSE;}
					$aContent .= "\n";
					fclose($handle);
				}//if
			}//if
			return TRUE;
		}//fGetParametersTemplateFile
	}//PluginLoader
?>
Return current item: Gemibloo