示例#1
0
 /** Initializes a plugin.
  * This function is called from the plugin file, after the plugin class definition. It loads the plugin from the data given in argument, includes dependencies,
  * loads the class, automatically binds the events to correctly named methods.
  * 
  * \param $params Loading parameters for the plugin. This associative array allows these values :
  * 			\li \b name : The name to be used for the plugin (i.e. the file name). String.
  * 			\li \b className : The main class to be instancied for the plugin. String.
  * 			\li \b dependencies : The dependencies the plugin needs for functionning. Array.
  * 			\li \b autoload : To let know if the function has also to do automatic binding of events. Boolean.
  * \param $manual Manual loading indicator. It permits the bot to know if a plugin has been loaded manually or automatically, by dependence.
  * \return TRUE if plugin initialized successfully, FALSE otherwise (and throws many warnings).
  */
 public function initPlugin($params, $manual)
 {
     //Checking that we have everything needed to load the plugin
     if (!is_array($params) || !isset($params['name']) || !isset($params['className'])) {
         Leelabot::message('Cannot load plugin with given data : $0', array(Leelabot::dumpArray($params)), E_WARNING);
         return FALSE;
     }
     //Load dependencies if necessary
     if (isset($params['dependencies']) && is_array($params['dependencies'])) {
         Leelabot::message('Loading plugin dependencies for $0.', array($params['name']));
         $ret = $this->loadPlugins($params['dependencies']);
         if (!$ret) {
             Leelabot::message('Cannot load plugin dependencies, loading aborted.', array(), E_WARNING);
             return FALSE;
         }
         Leelabot::message('Loaded plugin dependencies for $0.', array($params['name']));
     } elseif (isset($params['dependencies'])) {
         Leelabot::message('Dependencies list is not an array.', array(), E_WARNING);
     }
     //Init of plugin data array, and plugin instanciation
     $this->_plugins[$params['name']] = array('obj' => NULL, 'name' => $params['name'], 'display' => isset($params['display']) ? $params['display'] : $params['name'], 'dependencies' => isset($params['dependencies']) ? $params['dependencies'] : array(), 'className' => $params['className'], 'manual' => $manual);
     $this->_plugins[$params['name']]['obj'] = new $params['className']($this, $this->_main);
     //Autoloading !!1!
     if (isset($params['autoload']) && $params['autoload']) {
         Leelabot::message('Using automatic events recognition...');
         $methods = get_class_methods($params['className']);
         //Get all class methods for plugin
         //Analyse all class methods
         foreach ($methods as $method) {
             //Checks for routines
             if (preg_match('#^Routine#', $method)) {
                 $this->addRoutine($this->_plugins[$params['name']]['obj'], $method);
             }
             //Checks for Webservice methods
             if (preg_match('#^WSMethod#', $method)) {
                 $this->addWSMethod(lcfirst(preg_replace('#WSMethod(.+)#', '$1', $method)), $this->_plugins[$params['name']]['obj'], $method);
             }
             //Checks for Webadmin pages
             if (preg_match('#^WAPage#', $method)) {
                 $this->addWAPage(strtolower(preg_replace('#WAPage(.+)#', '$1', $method)), $this->_plugins[$params['name']]['obj'], $method);
             }
             //Checks for plugin-defined events
             foreach ($this->_autoMethods as $listener => $prefix) {
                 if (preg_match('#^' . $prefix . '#', $method)) {
                     $event = strtolower(preg_replace('#' . $prefix . '(.+)#', '$1', $method));
                     Leelabot::message('Adding method $0, on event $1/$2', array($params['className'] . '::' . $method, $listener, $event), E_DEBUG);
                     $this->addEvent($listener, $params['name'], $event, array($this->_plugins[$params['name']]['obj'], $method));
                 }
             }
         }
     }
     //Call to init() method of loaded plugin, for internal initializations and such. If it returns FALSE, the plugin is unloaded.
     if ($this->_plugins[$params['name']]['obj']->init() === FALSE) {
         $this->unloadPlugin($params['name']);
         return FALSE;
     }
     Leelabot::message('Loaded plugin $0', array($params['name']));
     //Now that the plugin is loaded, we update the list of all plugins' classes names
     $this->_reloadPluginsClasses();
     return TRUE;
 }