[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/plugin/ -> pluginmanager.class.php (source)

   1  <?php
   2  
   3      
   4      lt_include( PLOG_CLASS_PATH."class/file/file.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
   6      lt_include( PLOG_CLASS_PATH."class/misc/glob.class.php" );
   7  //    lt_include( PLOG_CLASS_PATH."class/controller/blogcontroller.class.php" );
   8  //    lt_include( PLOG_CLASS_PATH."class/controller/admincontroller.class.php" );
   9      lt_include( PLOG_CLASS_PATH."class/plugin/eventlist.properties.php" );
  10  //    lt_include( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
  11      lt_include( PLOG_CLASS_PATH."class/controller/resourceclassloader.class.php" );
  12  
  13      /**
  14       * other various constants
  15       */
  16      define( "PLUGIN_MANAGER_DEFAULT_PLUGIN_FOLDER", "./plugins/" );
  17      define( "PLUGIN_MANAGER_DEFAULT_PLUGIN_FILE_PATTERN", "plugin*.class.php" );
  18  
  19      /**
  20       * \ingroup Plugin
  21       * 
  22       * Implements a 'plugin manager' a class that takes care of loading plugins, initializing
  23       * them, allowing plugins to register for certain events, etc.
  24       *
  25       * This class will rarely be called by user classes, since all the plugin initialization and
  26       * delivery of messages is handled by the core classes.
  27       *
  28       * You should not create objects of this class. In case you need a handle to the global 
  29       * PluginManager, please use the singlethod getPluginManager as follows:
  30       *
  31       * <pre>
  32       *  $pm =& PluginManager::getPluginManager();
  33       *  $pm->notifyEvent( ... );
  34       * </pre>
  35       */
  36      class PluginManager 
  37      {
  38  
  39          var $_pluginDir;
  40          var $_filePattern;
  41          var $_blogInfo;
  42          var $_userInfo;
  43          var $_pluginInstances;
  44          var $_source;
  45  
  46          /**
  47           * global variable to save the list of plugins registered so far
  48           */
  49          var $_pluginList;
  50  
  51          /**
  52           * global variable to save which plugins registered which events
  53           */
  54          var $_pluginEventList;
  55  
  56          /**
  57           * Constructor.
  58           *
  59           * @param pluginDir Specifies from which folders templates should
  60           * be loaded from.
  61           * @param filePattern
  62           */
  63          function PluginManager( $pluginDir = PLUGIN_MANAGER_DEFAULT_PLUGIN_FOLDER, $filePattern = PLUGIN_MANAGER_DEFAULT_PLUGIN_FILE_PATTERN )
  64          {        
  65              $config =& Config::getConfig();
  66              
  67              // initialize the arrays used to keep track of plugins and events
  68              $this->_pluginEventList = Array();
  69              $this->_pluginInstances = Array();
  70              
  71              $this->_enabled = $config->getValue( "plugin_manager_enabled" );
  72              $this->_pluginDir = $pluginDir;
  73              $this->_filePattern = $filePattern;
  74              
  75              $this->_pluginList = $config->getValue( "plugin_list" );
  76              // just in case there is something wrong...
  77              if( $this->_pluginList == "" )
  78                  $this->_pluginList = Array();
  79          }
  80          
  81          function getPluginList()
  82          {
  83              return( $this->_pluginList );
  84          }
  85  
  86          /**
  87           * Sets the blog info
  88           *
  89           * @param blogInfo
  90           */
  91          function setBlogInfo( &$blogInfo )
  92          {
  93              $this->_blogInfo = $blogInfo;
  94          }
  95  
  96          /**
  97           * Sets the user info
  98           *
  99           * @param userInfo
 100           */
 101          function setUserInfo( &$userInfo )
 102          {
 103              $this->_userInfo = $userInfo;
 104          }
 105  
 106          /**
 107           * Returns the current instance of the plugin manager.
 108           *
 109           * @param pluginDir
 110           * @param filePattern
 111           * @see PluginManager
 112           * @return Returns an instance of the PluginManager, so that there is only one plugin
 113           * manager at any given time
 114           */
 115          function &getPluginManager( $pluginDir = PLUGIN_MANAGER_DEFAULT_PLUGIN_FOLDER, $filePattern = PLUGIN_MANAGER_DEFAULT_PLUGIN_FILE_PATTERN )
 116          {
 117              static $managerInstance;
 118  
 119              // check if there was an instance of the Config class already created
 120              if( !isset($managerInstance)) {
 121                  // if there wasn't, then create a new one
 122                  $managerInstance = new PluginManager( $pluginDir, $filePattern );
 123              }
 124  
 125              return $managerInstance;
 126          }
 127  
 128          /**
 129           * Returns wether the plugin manager is enabled or not.
 130           *
 131           * @return A boolean value telling wether the plugin manager is enabled or not.
 132           */
 133          function isEnabled()
 134          {
 135              return $this->_enabled;
 136          }
 137          
 138          /**
 139           * returns true if the given plugin has been registered as a such
 140           *
 141           * @param pluginId the identifier of the plugin
 142           * @return Returns true if the plugin is registered or false otherwise
 143           */
 144  		function pluginIsRegistered( $pluginId )
 145          {
 146              return( array_key_exists( $pluginId, $this->_pluginList ));
 147          }
 148          
 149          /**
 150           * Checks whether the version reported by the plugin is compatible with the current version of LT
 151           *
 152           * @return True if compatible or false otherwise.
 153           */
 154  		function _pluginCanRun( $version )
 155          {
 156              /**
 157               * :TODO:
 158               * 
 159               * to define this!!
 160               */
 161              return true;        
 162          }
 163  
 164          /**
 165           * Loads all the plugins from disk
 166           *
 167           * @param source
 168           */
 169          function loadPlugins( $source = "" )
 170          {                
 171              $classLoader =& ResourceClassLoader::getLoader();
 172              
 173              $this->_source = $source;
 174          
 175              foreach( $this->_pluginList as $plugin ) {
 176                  $pluginFile = "./plugins/$plugin";
 177                  $classInstance = $this->_createPluginInstance( $plugin );
 178                  if( $classInstance ) {
 179                      // tell the resource loader that it should try to load actions from this folder
 180                      $classLoader->addSearchFolder( PLOG_CLASS_PATH."$pluginFile/class/action/" );
 181                  }
 182              }
 183              
 184              return true;
 185          }
 186  
 187          /**
 188           * refreshes the list of folders from disk
 189           */
 190  		function getPluginListFromFolder()
 191          {
 192              $pluginList = Array();
 193              
 194              $pluginFiles = Glob::glob( $this->_pluginDir, "*" );
 195              if( !is_array( $pluginFiles ))
 196                  return $pluginList;
 197              
 198              foreach( $pluginFiles as $pluginFile ) {
 199                  if( File::isDir($pluginFile)) {
 200                      // build up the name of the file
 201                      $pluginIdElements = explode("/", $pluginFile);
 202                      $pluginId = array_pop($pluginIdElements);
 203                      $pluginFileName = "plugin".$pluginId.".class.php";
 204                      $pluginFullPath = PLOG_CLASS_PATH."$pluginFile/$pluginFileName";
 205                      // and try to include it
 206                      if( File::isReadable( $pluginFullPath )) {
 207                          $pluginList[] = $pluginId;
 208                      }
 209                  }
 210              }
 211              
 212              return $pluginList;
 213          }
 214          
 215          /**
 216           * saves the list of plugins to the config backend
 217           */
 218  		function savePluginList( $list )
 219          {
 220              $config =& Config::getConfig();
 221              $config->setValue( "plugin_list", $list );
 222              $config->save();
 223          }
 224          
 225          /**
 226           * @private
 227           *
 228           * Given a plugin identifier, creates a new instance of it and returns a reference ot the instance.
 229           * The instance is also saved in the PluginBase::_pluginInstances array
 230           *
 231           * @return Returns a reference to a plugin class extending PluginBase or null if there was an error
 232           */
 233  		function _createPluginInstance( $plugin )
 234          {
 235              // return null by default
 236              $classInstance = null;
 237              
 238              $pluginFile = "./plugins/$plugin";
 239              if( File::isDir($pluginFile)) {
 240                  // build up the name of the file
 241                     $pluginFileName = "plugin{$plugin}.class.php";
 242                     $pluginFullPath = PLOG_CLASS_PATH."$pluginFile/$pluginFileName";
 243                     // and try to include it
 244                     if( File::isReadable( $pluginFile."/".$pluginFileName )) {
 245                      $className = "Plugin".$plugin;
 246                      lt_include( $pluginFullPath );
 247                      $classInstance =& new $className( $this->_source );                        
 248                      $classInstance->setPluginFolder( PLOG_CLASS_PATH.$pluginFile."/" );    
 249                      $name = $classInstance->getId();
 250                      
 251                      if( $name == "" ) {
 252                          throw( new Exception( "Plugin file $pluginFile has no identifier defined!" ));
 253                          die();
 254                      }                                    
 255  
 256                      // create an instance only if the plugin is allowed to run in this version of LT                    
 257                      if( $this->_pluginCanRun( $classInstance->getVersion())) {
 258                          $this->_pluginInstances["$name"] = &$classInstance;
 259                      }
 260                  }
 261              }
 262              
 263              return( $classInstance );
 264          }
 265          
 266          /**
 267           * @private
 268           */
 269  		function refreshPluginList()
 270          {
 271              $this->_pluginList = $this->getPluginListFromFolder();
 272              
 273              foreach( $this->_pluginList as $plugin ) {
 274                  $classInstance = $this->_createPluginInstance( $plugin );
 275                  if( $classInstance ) {
 276                      $classInstance->install();                                        
 277                  }                    
 278              }
 279              
 280              $this->savePluginList( $this->_pluginList );
 281              
 282              return true;
 283          }
 284  
 285          /**
 286           * @private
 287           */
 288          function _loadPluginLocale( $pluginId, $locale )
 289          {
 290              lt_include( PLOG_CLASS_PATH . "class/locale/locales.class.php" );
 291  
 292              return( Locales::getPluginLocale( $pluginId, $locale ));
 293          }
 294  
 295          /**
 296           * Returns the folder used to store the plugins.
 297           *
 298           * @return The folder used to store the plugins.
 299           */
 300          function getPluginDir()
 301          {
 302              return $this->_pluginDir;
 303          }
 304  
 305          /**
 306           * Returns the array of plugins.
 307           *
 308           * @return An array of PluginBase objects.
 309           */
 310          function getPlugins()
 311          {
 312              foreach( $this->_pluginList as $name ) {
 313                  if( array_key_exists( $name, $this->_pluginInstances ) ) {
 314                      $this->_pluginInstances["$name"]->setBlogInfo( $this->_blogInfo );
 315                      $this->_pluginInstances["$name"]->setUserInfo( $this->_userInfo );
 316                      $this->_pluginInstances["$name"]->register();
 317                  }
 318              }
 319  
 320              return $this->_pluginInstances;
 321          }
 322  
 323          /**
 324           * notifies all the event plugins about an event
 325           *
 326           * @param eventType
 327           * @param params
 328           * @return
 329           */
 330          function notifyEvent( $eventType, $params = Array())
 331          {
 332              // check if there is any plugin that wants to be notified about this event
 333              $plugins = Array();
 334              if(array_key_exists( $eventType, $this->_pluginEventList ))
 335                  $plugins = $this->_pluginEventList["$eventType"];
 336  
 337              if( !is_array($plugins) || empty($plugins))
 338                  return $params;
 339  
 340              // fill in the parameters array with some other useful information
 341              $params[ "blogInfo" ] = $this->_blogInfo;
 342              $params[ "userInfo" ] = $this->_userInfo;
 343  
 344              // if so, loop through the plugins
 345              foreach( $plugins as $plugin ) {
 346                  $plugin->setBlogInfo( $this->_blogInfo );
 347                  $plugin->setUserInfo( $this->_userInfo );
 348                  $plugin->process( $eventType, $params );
 349              }
 350  
 351              return $params;
 352          }
 353  
 354          /**
 355           * tells the plugin manager that a certain event plugin class wants to be notified about
 356           * a certain event.
 357           *
 358           * @param eventType
 359           * @param pluginClass
 360           * @return Always true
 361           */
 362          function registerNotification( $eventType, &$pluginClass )
 363          {
 364              // there can be more than one plugin registered for the same event,
 365              // so we need an array of plugin classes
 366              if( !isset($this->_pluginEventList["$eventType"]) || !is_array($this->_pluginEventList["$eventType"])) {
 367                  $this->_pluginEventList["$eventType"] = Array();
 368              }
 369  
 370              // and then add the plugin to the list
 371              $this->_pluginEventList["$eventType"][] =& $pluginClass;
 372  
 373              return true;
 374          }
 375  
 376          /**
 377           * returns the events that have been defined
 378           *
 379           * @return An array with the names of the events that have been defined so far
 380           */
 381          function getDefinedEvents()
 382          {
 383              // get all the constants defined so far
 384              $constants = get_defined_constants();
 385              $events = Array();
 386  
 387              foreach( $constants as $constant => $value ) {
 388                  // if the constant starts with "EVENT_", it's one of us
 389                  if( substr( $constant, 0, 6 ) == "EVENT_" ) {
 390                      $events[ "$constant" ] = $value;
 391                  }
 392              }
 393  
 394              return $events;
 395          }
 396          
 397          /**
 398           * returns true if the plugin provides the requested locale
 399           *
 400           * @param pluginId The plugin identifier that we're like to check
 401           * @param localeCode the locale code
 402           * @return True if the plugin has this locale or false otherwise
 403           * @static
 404           */
 405          function pluginHasLocale( $pluginId, $localeCode ) 
 406          {
 407              return( File::isReadable( "plugins/$pluginId/locale/locale_{$localeCode}.php" ));
 408          }
 409      }
 410  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics