[pLog-svn] r2321 - plog/trunk/class/plugin

oscar at devel.plogworld.net oscar at devel.plogworld.net
Thu Jul 14 14:06:19 GMT 2005


Author: oscar
Date: 2005-07-14 14:06:18 +0000 (Thu, 14 Jul 2005)
New Revision: 2321

Modified:
   plog/trunk/class/plugin/pluginbase.class.php
   plog/trunk/class/plugin/pluginmanager.class.php
Log:
added PluginBase::install() which is only called from PluginBase::refreshPluginList() and that should be used by plugins to create their tables or do any
arrangements for the first time they are used. 
Since the constructor of the plugin is called twice (once in PluginBase::loadPlugins() and once again in PluginBase::refresh...) some plugins will do 
things like create an entry in the menus twice and so on, so we should take a look at those later on to prevent this kind of behaviour.
This also fixes feature request http://bugs.plogworld.net/view.php?id=624


Modified: plog/trunk/class/plugin/pluginbase.class.php
===================================================================
--- plog/trunk/class/plugin/pluginbase.class.php	2005-07-14 07:29:20 UTC (rev 2320)
+++ plog/trunk/class/plugin/pluginbase.class.php	2005-07-14 14:06:18 UTC (rev 2321)
@@ -353,5 +353,18 @@
             throw( new Exception( "Plugin ".$this->id." registered for event $eventType but did not provide its own process() method!" ));
             die();
         }
+        
+        /**
+         * Please use this method to perform any tasks that should be done only once when the plugin is installed
+         * for the first time such as creation of new database tables, etc. 
+         * This method will be called every time the "Plugin Centre" page is refreshed, so you should
+         * not assume that this method will be called only once. 
+         *
+         * @return Always true
+         */
+        function install()
+        {
+	     	return true;
+        }
     }
 ?>

Modified: plog/trunk/class/plugin/pluginmanager.class.php
===================================================================
--- plog/trunk/class/plugin/pluginmanager.class.php	2005-07-14 07:29:20 UTC (rev 2320)
+++ plog/trunk/class/plugin/pluginmanager.class.php	2005-07-14 14:06:18 UTC (rev 2321)
@@ -158,29 +158,10 @@
 		
 			foreach( $this->_pluginList as $plugin ) {
 				$pluginFile = "./plugins/$plugin";
-              	if( File::isDir($pluginFile)) {
-					// build up the name of the file
-                  	$pluginFileName = "plugin{$plugin}.class.php";
-                  	$pluginFullPath = PLOG_CLASS_PATH."$pluginFile/$pluginFileName";
-                  	// and try to include it
-                  	if( File::isReadable( $pluginFile."/".$pluginFileName )) {
-						$className = "Plugin".$plugin;
-						include_once( $pluginFullPath );
-						$classInstance = new $className();
-						$name = $classInstance->getId();						
-
-						$classInstance->setPluginFolder( PLOG_CLASS_PATH.$pluginFile."/" );
-						
-						// tell the resource loader that it should try to load actions from this folder
-						$classLoader->addSearchFolder( PLOG_CLASS_PATH."$pluginFile/class/action/" );
-						
-						if( $name == "" ) {
-							throw( new Exception( "Plugin file $pluginFile has no identifier defined!" ));
-							die();
-						}
-							
-						$this->_pluginInstances["$plugin"] = $classInstance;
-                   	}
+				$classInstance = $this->_createPluginInstance( $plugin );
+				if( $classInstance ) {
+					// tell the resource loader that it should try to load actions from this folder
+					$classLoader->addSearchFolder( PLOG_CLASS_PATH."$pluginFile/class/action/" );
 				}
 			}
 			
@@ -226,10 +207,56 @@
 		
 		/**
 		 * @private
+		 *
+		 * Given a plugin identifier, creates a new instance of it and returns a reference ot the instance.
+		 * The instance is also saved in the PluginBase::_pluginInstances array
+		 *
+		 * @return Returns a reference to a plugin class extending PluginBase or null if there was an error
 		 */
+		function _createPluginInstance( $plugin )
+		{
+			// return null by default
+			$classInstance = null;
+			
+			$pluginFile = "./plugins/$plugin";
+            if( File::isDir($pluginFile)) {
+				// build up the name of the file
+               	$pluginFileName = "plugin{$plugin}.class.php";
+               	$pluginFullPath = PLOG_CLASS_PATH."$pluginFile/$pluginFileName";
+               	// and try to include it
+               	if( File::isReadable( $pluginFile."/".$pluginFileName )) {
+					$className = "Plugin".$plugin;
+					include_once( $pluginFullPath );
+					$classInstance = new $className();						
+					$classInstance->setPluginFolder( PLOG_CLASS_PATH.$pluginFile."/" );	
+					$name = $classInstance->getId();			
+					
+					if( $name == "" ) {
+						throw( new Exception( "Plugin file $pluginFile has no identifier defined!" ));
+						die();
+					}					
+					
+					$this->_pluginInstances["$name"] = &$classInstance;										
+				}
+			}
+			
+			return( $classInstance );
+		}
+		
+		/**
+		 * @private
+		 */
 		function refreshPluginList()
 		{
 			$this->_pluginList = $this->getPluginListFromFolder();
+			
+			foreach( $this->_pluginList as $plugin ) {
+				$classInstance = $this->_createPluginInstance( $plugin );
+				if( $classInstance ) {
+					$classInstance->install();										
+				}					
+			}
+			
 			$this->savePluginList( $this->_pluginList );
 			
 			return true;




More information about the pLog-svn mailing list