[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/plugin/ -> helper.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Plugin
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  /**
  13   * Plugin helper class
  14   *
  15   * @package     Joomla.Platform
  16   * @subpackage  Plugin
  17   * @since       11.1
  18   */
  19  abstract class JPluginHelper
  20  {
  21      /**
  22       * A persistent cache of the loaded plugins.
  23       *
  24       * @var    array
  25       * @since  11.3
  26       */
  27      protected static $plugins = null;
  28  
  29      /**
  30       * Get the plugin data of a specific type if no specific plugin is specified
  31       * otherwise only the specific plugin data is returned.
  32       *
  33       * @param   string  $type    The plugin type, relates to the sub-directory in the plugins directory.
  34       * @param   string  $plugin  The plugin name.
  35       *
  36       * @return  mixed  An array of plugin data objects, or a plugin data object.
  37       *
  38       * @since   11.1
  39       */
  40  	public static function getPlugin($type, $plugin = null)
  41      {
  42          $result = array();
  43          $plugins = self::_load();
  44  
  45          // Find the correct plugin(s) to return.
  46          if (!$plugin)
  47          {
  48              foreach ($plugins as $p)
  49              {
  50                  // Is this the right plugin?
  51                  if ($p->type == $type)
  52                  {
  53                      $result[] = $p;
  54                  }
  55              }
  56          }
  57          else
  58          {
  59              foreach ($plugins as $p)
  60              {
  61                  // Is this plugin in the right group?
  62                  if ($p->type == $type && $p->name == $plugin)
  63                  {
  64                      $result = $p;
  65                      break;
  66                  }
  67              }
  68          }
  69  
  70          return $result;
  71      }
  72  
  73      /**
  74       * Checks if a plugin is enabled.
  75       *
  76       * @param   string  $type    The plugin type, relates to the sub-directory in the plugins directory.
  77       * @param   string  $plugin  The plugin name.
  78       *
  79       * @return  boolean
  80       *
  81       * @since   11.1
  82       */
  83  	public static function isEnabled($type, $plugin = null)
  84      {
  85          $result = self::getPlugin($type, $plugin);
  86          return (!empty($result));
  87      }
  88  
  89      /**
  90       * Loads all the plugin files for a particular type if no specific plugin is specified
  91       * otherwise only the specific plugin is loaded.
  92       *
  93       * @param   string       $type        The plugin type, relates to the sub-directory in the plugins directory.
  94       * @param   string       $plugin      The plugin name.
  95       * @param   boolean      $autocreate  Autocreate the plugin.
  96       * @param   JDispatcher  $dispatcher  Optionally allows the plugin to use a different dispatcher.
  97       *
  98       * @return  boolean  True on success.
  99       *
 100       * @since   11.1
 101       */
 102  	public static function importPlugin($type, $plugin = null, $autocreate = true, $dispatcher = null)
 103      {
 104          static $loaded = array();
 105  
 106          // check for the default args, if so we can optimise cheaply
 107          $defaults = false;
 108          if (is_null($plugin) && $autocreate == true && is_null($dispatcher))
 109          {
 110              $defaults = true;
 111          }
 112  
 113          if (!isset($loaded[$type]) || !$defaults)
 114          {
 115              $results = null;
 116  
 117              // Load the plugins from the database.
 118              $plugins = self::_load();
 119  
 120              // Get the specified plugin(s).
 121              for ($i = 0, $t = count($plugins); $i < $t; $i++)
 122              {
 123                  if ($plugins[$i]->type == $type && ($plugin === null || $plugins[$i]->name == $plugin))
 124                  {
 125                      self::_import($plugins[$i], $autocreate, $dispatcher);
 126                      $results = true;
 127                  }
 128              }
 129  
 130              // Bail out early if we're not using default args
 131              if (!$defaults)
 132              {
 133                  return $results;
 134              }
 135              $loaded[$type] = $results;
 136          }
 137  
 138          return $loaded[$type];
 139      }
 140  
 141      /**
 142       * Loads the plugin file.
 143       *
 144       * @param   JPlugin      &$plugin     The plugin.
 145       * @param   boolean      $autocreate  True to autocreate.
 146       * @param   JDispatcher  $dispatcher  Optionally allows the plugin to use a different dispatcher.
 147       *
 148       * @return  boolean  True on success.
 149       *
 150       * @since   11.1
 151       */
 152  	protected static function _import(&$plugin, $autocreate = true, $dispatcher = null)
 153      {
 154          static $paths = array();
 155  
 156          $plugin->type = preg_replace('/[^A-Z0-9_\.-]/i', '', $plugin->type);
 157          $plugin->name = preg_replace('/[^A-Z0-9_\.-]/i', '', $plugin->name);
 158  
 159          $legacypath = JPATH_PLUGINS . '/' . $plugin->type . '/' . $plugin->name . '.php';
 160          $path = JPATH_PLUGINS . '/' . $plugin->type . '/' . $plugin->name . '/' . $plugin->name . '.php';
 161  
 162          if (!isset($paths[$path]) || !isset($paths[$legacypath]))
 163          {
 164              $pathExists = file_exists($path);
 165              if ($pathExists || file_exists($legacypath))
 166              {
 167                  $path = $pathExists ? $path : $legacypath;
 168  
 169                  if (!isset($paths[$path]))
 170                  {
 171                      require_once $path;
 172                  }
 173                  $paths[$path] = true;
 174  
 175                  if ($autocreate)
 176                  {
 177                      // Makes sure we have an event dispatcher
 178                      if (!is_object($dispatcher))
 179                      {
 180                          $dispatcher = JDispatcher::getInstance();
 181                      }
 182  
 183                      $className = 'plg' . $plugin->type . $plugin->name;
 184                      if (class_exists($className))
 185                      {
 186                          // Load the plugin from the database.
 187                          if (!isset($plugin->params))
 188                          {
 189                              // Seems like this could just go bye bye completely
 190                              $plugin = self::getPlugin($plugin->type, $plugin->name);
 191                          }
 192  
 193                          // Instantiate and register the plugin.
 194                          new $className($dispatcher, (array) ($plugin));
 195                      }
 196                  }
 197              }
 198              else
 199              {
 200                  $paths[$path] = false;
 201              }
 202          }
 203      }
 204  
 205      /**
 206       * Loads the published plugins.
 207       *
 208       * @return  array  An array of published plugins
 209       *
 210       * @since   11.1
 211       */
 212  	protected static function _load()
 213      {
 214          if (self::$plugins !== null)
 215          {
 216              return self::$plugins;
 217          }
 218  
 219          $user = JFactory::getUser();
 220          $cache = JFactory::getCache('com_plugins', '');
 221  
 222          $levels = implode(',', $user->getAuthorisedViewLevels());
 223  
 224          if (!self::$plugins = $cache->get($levels))
 225          {
 226              $db = JFactory::getDbo();
 227              $query = $db->getQuery(true);
 228  
 229              $query->select('folder AS type, element AS name, params')
 230                  ->from('#__extensions')
 231                  ->where('enabled >= 1')
 232                  ->where('type =' . $db->Quote('plugin'))
 233                  ->where('state >= 0')
 234                  ->where('access IN (' . $levels . ')')
 235                  ->order('ordering');
 236  
 237              self::$plugins = $db->setQuery($query)->loadObjectList();
 238  
 239              if ($error = $db->getErrorMsg())
 240              {
 241                  JError::raiseWarning(500, $error);
 242                  return false;
 243              }
 244  
 245              $cache->store(self::$plugins, $levels);
 246          }
 247  
 248          return self::$plugins;
 249      }
 250  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1