[Svn] r4246 - branches/devel-addressbook/program/include

trac at roundcube.net trac at roundcube.net
Mon Nov 22 11:10:42 CET 2010


Author: thomasb
Date: 2010-11-22 04:10:42 -0600 (Mon, 22 Nov 2010)
New Revision: 4246

Modified:
   branches/devel-addressbook/program/include/rcube_plugin.php
   branches/devel-addressbook/program/include/rcube_plugin_api.php
Log:
Allow plugins to depend on others by calling this->require_plugin()

Modified: branches/devel-addressbook/program/include/rcube_plugin.php
===================================================================
--- branches/devel-addressbook/program/include/rcube_plugin.php	2010-11-21 17:01:22 UTC (rev 4245)
+++ branches/devel-addressbook/program/include/rcube_plugin.php	2010-11-22 10:10:42 UTC (rev 4246)
@@ -83,8 +83,21 @@
    * Initialization method, needs to be implemented by the plugin itself
    */
   abstract function init();
-  
+
+
   /**
+   * Attempt to load the given plugin which is required for the current plugin
+   *
+   * @param string Plugin name
+   * @return boolean True on success, false on failure
+   */
+  public function require_plugin($plugin_name)
+  {
+    return $this->api->load_plugin($plugin_name);
+  }
+
+
+  /**
    * Load local config file from plugins directory.
    * The loaded values are patched over the global configuration.
    *

Modified: branches/devel-addressbook/program/include/rcube_plugin_api.php
===================================================================
--- branches/devel-addressbook/program/include/rcube_plugin_api.php	2010-11-21 17:01:22 UTC (rev 4245)
+++ branches/devel-addressbook/program/include/rcube_plugin_api.php	2010-11-22 10:10:42 UTC (rev 4246)
@@ -109,42 +109,9 @@
     $this->output = $rcmail->output;
     $this->config = $rcmail->config;
 
-    $plugins_dir = dir($this->dir);
-    $plugins_dir = unslashify($plugins_dir->path);
     $plugins_enabled = (array)$rcmail->config->get('plugins', array());
-
     foreach ($plugins_enabled as $plugin_name) {
-      $fn = $plugins_dir . DIRECTORY_SEPARATOR . $plugin_name . DIRECTORY_SEPARATOR . $plugin_name . '.php';
-
-      if (file_exists($fn)) {
-        include($fn);
-
-        // instantiate class if exists
-        if (class_exists($plugin_name, false)) {
-          $plugin = new $plugin_name($this);
-          // check inheritance...
-          if (is_subclass_of($plugin, 'rcube_plugin')) {
-            // ... task, request type and framed mode
-            if ((!$plugin->task || preg_match('/^('.$plugin->task.')$/i', $rcmail->task))
-                && (!$plugin->noajax || is_a($this->output, 'rcube_template'))
-                && (!$plugin->noframe || empty($_REQUEST['_framed']))
-            ) {
-              $plugin->init();
-              $this->plugins[] = $plugin;
-            }
-          }
-        }
-        else {
-          raise_error(array('code' => 520, 'type' => 'php',
-	    'file' => __FILE__, 'line' => __LINE__,
-	    'message' => "No plugin class $plugin_name found in $fn"), true, false);
-        }
-      }
-      else {
-        raise_error(array('code' => 520, 'type' => 'php',
-	  'file' => __FILE__, 'line' => __LINE__,
-	  'message' => "Failed to load plugin file $fn"), true, false);
-      }
+      $this->load_plugin($plugin_name);
     }
     
     // check existance of all required core plugins
@@ -158,31 +125,14 @@
       }
       
       // load required core plugin if no derivate was found
-      if (!$loaded) {
-        $fn = $plugins_dir . DIRECTORY_SEPARATOR . $plugin_name . DIRECTORY_SEPARATOR . $plugin_name . '.php';
+      if (!$loaded)
+        $loaded = $this->load_plugin($plugin_name);
 
-        if (file_exists($fn)) {
-          include_once($fn);
-          
-          if (class_exists($plugin_name, false)) {
-            $plugin = new $plugin_name($this);
-            // check inheritance
-            if (is_subclass_of($plugin, 'rcube_plugin')) {
-	      if (!$plugin->task || preg_match('/('.$plugin->task.')/i', $rcmail->task)) {
-                $plugin->init();
-                $this->plugins[] = $plugin;
-              }
-	      $loaded = true;
-            }
-          }
-        }
-      }
-      
       // trigger fatal error if still not loaded
       if (!$loaded) {
         raise_error(array('code' => 520, 'type' => 'php',
-	  'file' => __FILE__, 'line' => __LINE__,
-	  'message' => "Requried plugin $plugin_name was not loaded"), true, true);
+          'file' => __FILE__, 'line' => __LINE__,
+          'message' => "Requried plugin $plugin_name was not loaded"), true, true);
       }
     }
 
@@ -191,6 +141,64 @@
     
     // maybe also register a shudown function which triggers shutdown functions of all plugin objects
   }
+
+
+  /**
+   * Load the specified plugin
+   *
+   * @param string Plugin name
+   * @return boolean True on success, false if not loaded or failure
+   */
+  public function load_plugin($plugin_name)
+  {
+    static $plugins_dir;
+    
+    $rcmail = rcmail::get_instance();
+    
+    if (!$plugins_dir) {
+      $dir = dir($this->dir);
+      $plugins_dir = unslashify($dir->path);
+    }
+    
+    // plugin already loaded
+    if ($this->plugins[$plugin_name] || class_exists($plugin_name, false))
+      return true;
+    
+    $fn = $plugins_dir . DIRECTORY_SEPARATOR . $plugin_name . DIRECTORY_SEPARATOR . $plugin_name . '.php';
+
+    if (file_exists($fn)) {
+      include($fn);
+
+      // instantiate class if exists
+      if (class_exists($plugin_name, false)) {
+        $plugin = new $plugin_name($this);
+        // check inheritance...
+        if (is_subclass_of($plugin, 'rcube_plugin')) {
+          // ... task, request type and framed mode
+          if ((!$plugin->task || preg_match('/^('.$plugin->task.')$/i', $rcmail->task)) /*
+              && (!$plugin->noajax || is_a($rcmail->output, 'rcube_template'))
+              && (!$plugin->noframe || empty($_REQUEST['_framed']))*/
+          ) {
+            $plugin->init();
+            $this->plugins[$plugin_name] = $plugin;
+          }
+          return true;
+        }
+      }
+      else {
+        raise_error(array('code' => 520, 'type' => 'php',
+          'file' => __FILE__, 'line' => __LINE__,
+          'message' => "No plugin class $plugin_name found in $fn"), true, false);
+      }
+    }
+    else {
+      raise_error(array('code' => 520, 'type' => 'php',
+        'file' => __FILE__, 'line' => __LINE__,
+        'message' => "Failed to load plugin file $fn"), true, false);
+    }
+    
+    return false;
+  }
   
   
   /**

_______________________________________________
http://lists.roundcube.net/mailman/listinfo/svn



More information about the Svn mailing list