Source for file plugins.php

Documentation is available at plugins.php

  1. <?php
  2. /**
  3. * Defines a library class for managing SLOODLE Plugins.
  4. * It is constructed and used through a SloodleSession object.
  5. *
  6. @package sloodle
  7. @copyright Copyright (c) 2009 Sloodle (various contributors)
  8. @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. @contributor Peter R. Bloomfield
  11. */
  12.  
  13.  
  14. // This library expects that the Sloodle config file has already been included
  15. //  (along with the Moodle libraries)
  16.  
  17. /** Include the general Sloodle functionality. */
  18. require_once(SLOODLE_DIRROOT.'/lib/general.php');
  19.  
  20.  
  21. /**
  22. * A class to load SLOODLE plugins, and provide a means to access them.
  23. @package sloodle
  24. */
  25. {
  26. // DATA //
  27.  
  28.     /**
  29.     * Internal only - reference to the containing {@link SloodleSession} object.
  30.     * Note: always check that it is not null before use!
  31.     * @var object 
  32.     * @access protected
  33.     */
  34.     var $_session = null;
  35.  
  36.     /**
  37.     * Plugin instance cache.
  38.     * Stores the plugins which have already been created, to prevent new ones being instantiated unnecessarily.
  39.     * Is an associative array of plugin names to plugin objects.
  40.     * @var array 
  41.     * @access protected
  42.     */
  43.     var $plugin_cache = array();
  44.  
  45.     
  46. // FUNCTIONS //
  47.  
  48.     /**
  49.     * Class constructor.
  50.     * @param object &$_session Reference to the containing {@link SloodleSession} object, if available.
  51.     * @access public
  52.     */
  53.     function SloodlePluginManager(&$_session)
  54.     {
  55.         if (!is_null($_session)) $this->_session = &$_session;
  56.     }
  57.  
  58.     /**
  59.     * Loads all available plugins from the specified folder.
  60.     * @param string $folder Name of the folder to load plugins from. This is a sub-folder of the 'sloodle/plugin' folder. It will only load files which are directly contained inside it.
  61.     * @return bool True if successful, or false if it fails. (It will only report failure if the folder does not exist, or there is an error accessing it.)
  62.     */
  63.     function load_plugins($folder)
  64.     {
  65.         if (empty($folder)) return false;
  66.  
  67.         // Get a list of all the files in the specified folder
  68.         $pluginFolder SLOODLE_DIRROOT.'/plugin/'.$folder;
  69.         $files sloodle_get_files($pluginFoldertrue);
  70.         if (!$filesreturn false;
  71.         if (count($files== 0return true;
  72.  
  73.         // Start by including the relevant base class files, if they are available
  74.         @include_once(SLOODLE_DIRROOT.'/plugin/_base.php');
  75.         @include_once($pluginFolder.'/_base.php');
  76.  
  77.         // Go through each filename
  78.         foreach ($files as $file{
  79.             // Include the specified file
  80.             include_once($pluginFolder.'/'.$file);
  81.         }
  82.  
  83.         return true;
  84.     }
  85.     
  86.     /**
  87.     * Gets an array of the names of all SLOODLE plugins derived from the specified type.
  88.     * By default, this gets all plugins. Specify a different base class to get others.
  89.     * NOTE: this will search all plugins loaded by all plugin managers in the current PHP script.
  90.     * (There is no way to tell which manager loaded which plugins.)
  91.     * Plugin names correspond to class names, with the 'SloodlePlugin' prefix.
  92.     * @param string $type Name of a plugin base class.
  93.     * @return array Numeric array of plugin names. These names correspond to class names.
  94.     */
  95.     function get_plugin_names($type 'SloodlePluginBase')
  96.     {
  97.         // We want to create an array of plugin names
  98.         $plugins array();
  99.  
  100.         // Go through all declared classes
  101.         $classes get_declared_classes();
  102.         foreach ($classes as $className{
  103.             // Make sure this is a SLOODLE plugin by checking that it starts "SloodlePlugin" but not "SloodlePluginbase"
  104.             if (strpos($className'SloodlePlugin'!== || strpos($className'SloodlePluginBase'=== 0continue;
  105.             // Make sure this is not one of the supporting classes
  106.             if ($className == 'SloodlePluginBase' || $className == 'SloodlePluginManager'continue;
  107.  
  108.             // Make sure it is in fact a plugin by ensuring it is appropriately derived from the given base plugin class
  109.             $tempPlugin @new $className();
  110.             if (!is_subclass_of($tempPlugin$type)) continue;
  111.  
  112.             // Remove the 'SloodlePlugin' prefix from the class name
  113.             $className substr($className13);
  114.             $plugins[$className;
  115.         }
  116.  
  117.         return $plugins;
  118.     }
  119.  
  120.     /**
  121.     * Gets an instance of the specified plugin type.
  122.     * This only works if the plugin has been loaded, and if it is derived from SloodlePluginBase.
  123.     * @param string $name Name of the plugin type to get. If it does not start with "SloodlePlugin", then that is added to the start.
  124.     * @param bool $forcenew If false (default) then a cached instance of the plugin will be returned. Set this to true to force the manager to create a new plugin instance.
  125.     * @return object|boolAn object descended from SloodlePluginBase if successful, or false on failure.
  126.     */
  127.     function get_plugin($name$forcenew false)
  128.     {
  129.         // Prepend 'SloodlePlugin' if necessary
  130.         if (strpos($name'SloodlePlugin'!== 0$name 'SloodlePlugin'.$name;
  131.         // Make sure the specified class exists
  132.         if (!class_exists($name)) return false;
  133.         // Do we have a cached plugin of this type?
  134.         if ($forcenew == false && !empty($this->plugin_cache[$name]&& is_a($this->plugin_cache[$name]$name)) return $this->plugin_cache[$name];
  135.  
  136.         // Attempt to construct an instance of the plugin
  137.         $plugin new $name();
  138.         // Make sure it is a valid plugin
  139.         if (is_subclass_of($plugin'SloodlePluginBase')) {
  140.             $this->plugin_cache[$name$plugin;
  141.             return $plugin;
  142.         }
  143.         return false;
  144.     }
  145.  
  146. }
  147.  
  148. ?>

Documentation generated on Fri, 17 Jul 2009 11:02:22 +0100 by phpDocumentor 1.4.0