Changeset 4246 in subversion


Ignore:
Timestamp:
Nov 22, 2010 5:10:42 AM (3 years ago)
Author:
thomasb
Message:

Allow plugins to depend on others by calling this->require_plugin()

Location:
branches/devel-addressbook/program/include
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/devel-addressbook/program/include/rcube_plugin.php

    r4189 r4246  
    8484   */ 
    8585  abstract function init(); 
    86    
     86 
     87 
     88  /** 
     89   * Attempt to load the given plugin which is required for the current plugin 
     90   * 
     91   * @param string Plugin name 
     92   * @return boolean True on success, false on failure 
     93   */ 
     94  public function require_plugin($plugin_name) 
     95  { 
     96    return $this->api->load_plugin($plugin_name); 
     97  } 
     98 
     99 
    87100  /** 
    88101   * Load local config file from plugins directory. 
  • branches/devel-addressbook/program/include/rcube_plugin_api.php

    r4154 r4246  
    110110    $this->config = $rcmail->config; 
    111111 
    112     $plugins_dir = dir($this->dir); 
    113     $plugins_dir = unslashify($plugins_dir->path); 
    114112    $plugins_enabled = (array)$rcmail->config->get('plugins', array()); 
    115  
    116113    foreach ($plugins_enabled as $plugin_name) { 
    117       $fn = $plugins_dir . DIRECTORY_SEPARATOR . $plugin_name . DIRECTORY_SEPARATOR . $plugin_name . '.php'; 
    118  
    119       if (file_exists($fn)) { 
    120         include($fn); 
    121  
    122         // instantiate class if exists 
    123         if (class_exists($plugin_name, false)) { 
    124           $plugin = new $plugin_name($this); 
    125           // check inheritance... 
    126           if (is_subclass_of($plugin, 'rcube_plugin')) { 
    127             // ... task, request type and framed mode 
    128             if ((!$plugin->task || preg_match('/^('.$plugin->task.')$/i', $rcmail->task)) 
    129                 && (!$plugin->noajax || is_a($this->output, 'rcube_template')) 
    130                 && (!$plugin->noframe || empty($_REQUEST['_framed'])) 
    131             ) { 
    132               $plugin->init(); 
    133               $this->plugins[] = $plugin; 
    134             } 
    135           } 
    136         } 
    137         else { 
    138           raise_error(array('code' => 520, 'type' => 'php', 
    139             'file' => __FILE__, 'line' => __LINE__, 
    140             'message' => "No plugin class $plugin_name found in $fn"), true, false); 
    141         } 
    142       } 
    143       else { 
    144         raise_error(array('code' => 520, 'type' => 'php', 
    145           'file' => __FILE__, 'line' => __LINE__, 
    146           'message' => "Failed to load plugin file $fn"), true, false); 
    147       } 
     114      $this->load_plugin($plugin_name); 
    148115    } 
    149116     
     
    159126       
    160127      // load required core plugin if no derivate was found 
    161       if (!$loaded) { 
    162         $fn = $plugins_dir . DIRECTORY_SEPARATOR . $plugin_name . DIRECTORY_SEPARATOR . $plugin_name . '.php'; 
    163  
    164         if (file_exists($fn)) { 
    165           include_once($fn); 
    166            
    167           if (class_exists($plugin_name, false)) { 
    168             $plugin = new $plugin_name($this); 
    169             // check inheritance 
    170             if (is_subclass_of($plugin, 'rcube_plugin')) { 
    171               if (!$plugin->task || preg_match('/('.$plugin->task.')/i', $rcmail->task)) { 
    172                 $plugin->init(); 
    173                 $this->plugins[] = $plugin; 
    174               } 
    175               $loaded = true; 
    176             } 
    177           } 
    178         } 
    179       } 
    180        
     128      if (!$loaded) 
     129        $loaded = $this->load_plugin($plugin_name); 
     130 
    181131      // trigger fatal error if still not loaded 
    182132      if (!$loaded) { 
    183133        raise_error(array('code' => 520, 'type' => 'php', 
    184           'file' => __FILE__, 'line' => __LINE__, 
    185           'message' => "Requried plugin $plugin_name was not loaded"), true, true); 
     134          'file' => __FILE__, 'line' => __LINE__, 
     135          'message' => "Requried plugin $plugin_name was not loaded"), true, true); 
    186136      } 
    187137    } 
     
    191141     
    192142    // maybe also register a shudown function which triggers shutdown functions of all plugin objects 
     143  } 
     144 
     145 
     146  /** 
     147   * Load the specified plugin 
     148   * 
     149   * @param string Plugin name 
     150   * @return boolean True on success, false if not loaded or failure 
     151   */ 
     152  public function load_plugin($plugin_name) 
     153  { 
     154    static $plugins_dir; 
     155     
     156    $rcmail = rcmail::get_instance(); 
     157     
     158    if (!$plugins_dir) { 
     159      $dir = dir($this->dir); 
     160      $plugins_dir = unslashify($dir->path); 
     161    } 
     162     
     163    // plugin already loaded 
     164    if ($this->plugins[$plugin_name] || class_exists($plugin_name, false)) 
     165      return true; 
     166     
     167    $fn = $plugins_dir . DIRECTORY_SEPARATOR . $plugin_name . DIRECTORY_SEPARATOR . $plugin_name . '.php'; 
     168 
     169    if (file_exists($fn)) { 
     170      include($fn); 
     171 
     172      // instantiate class if exists 
     173      if (class_exists($plugin_name, false)) { 
     174        $plugin = new $plugin_name($this); 
     175        // check inheritance... 
     176        if (is_subclass_of($plugin, 'rcube_plugin')) { 
     177          // ... task, request type and framed mode 
     178          if ((!$plugin->task || preg_match('/^('.$plugin->task.')$/i', $rcmail->task)) /* 
     179              && (!$plugin->noajax || is_a($rcmail->output, 'rcube_template')) 
     180              && (!$plugin->noframe || empty($_REQUEST['_framed']))*/ 
     181          ) { 
     182            $plugin->init(); 
     183            $this->plugins[$plugin_name] = $plugin; 
     184          } 
     185          return true; 
     186        } 
     187      } 
     188      else { 
     189        raise_error(array('code' => 520, 'type' => 'php', 
     190          'file' => __FILE__, 'line' => __LINE__, 
     191          'message' => "No plugin class $plugin_name found in $fn"), true, false); 
     192      } 
     193    } 
     194    else { 
     195      raise_error(array('code' => 520, 'type' => 'php', 
     196        'file' => __FILE__, 'line' => __LINE__, 
     197        'message' => "Failed to load plugin file $fn"), true, false); 
     198    } 
     199     
     200    return false; 
    193201  } 
    194202   
Note: See TracChangeset for help on using the changeset viewer.