Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
PluginRegistry.inc.php
1 <?php
2 
18 define('PLUGINS_PREFIX', 'plugins/');
19 
21  //
22  // Public methods
23  //
30  static function &getPlugins($category = null) {
31  $plugins =& Registry::get('plugins');
32  if ($category !== null) return $plugins[$category];
33  return $plugins;
34  }
35 
39  static function &getAllPlugins() {
40  $plugins =& PluginRegistry::getPlugins();
41  $allPlugins = array();
42  if (is_array($plugins)) foreach ($plugins as $list) {
43  if (is_array($list)) $allPlugins += $list;
44  }
45  return $allPlugins;
46  }
47 
55  static function register($category, &$plugin, $path) {
56  $pluginName = $plugin->getName();
57  $plugins =& PluginRegistry::getPlugins();
58  if (!$plugins) $plugins = array();
59 
60  // If the plugin was already loaded, do not load it again.
61  if (isset($plugins[$category][$pluginName])) return false;
62 
63  // Allow the plugin to register.
64  if (!$plugin->register($category, $path)) return false;
65 
66  if (isset($plugins[$category])) $plugins[$category][$pluginName] =& $plugin;
67  else $plugins[$category] = array($pluginName => &$plugin);
68  Registry::set('plugins', $plugins);
69  return true;
70  }
71 
77  static function &getPlugin ($category, $name) {
78  $plugins =& PluginRegistry::getPlugins();
79  $plugin = @$plugins[$category][$name];
80  return $plugin;
81  }
82 
95  static function &loadCategory ($category, $enabledOnly = false, $mainContextId = null) {
96  $plugins = array();
97  $categoryDir = PLUGINS_PREFIX . $category;
98  if (!is_dir($categoryDir)) return $plugins;
99 
100  if ($enabledOnly && Config::getVar('general', 'installed')) {
101  // Get enabled plug-ins from the database.
102  $application = PKPApplication::getApplication();
103  $products =& $application->getEnabledProducts('plugins.'.$category, $mainContextId);
104  foreach ($products as $product) {
105  $file = $product->getProduct();
106  $plugin =& PluginRegistry::_instantiatePlugin($category, $categoryDir, $file, $product->getProductClassname());
107  if ($plugin && is_object($plugin)) {
108  $plugins[$plugin->getSeq()]["$categoryDir/$file"] =& $plugin;
109  unset($plugin);
110  }
111  }
112  } else {
113  // Get all plug-ins from disk. This does not require
114  // any database access and can therefore be used during
115  // first-time installation.
116  $handle = opendir($categoryDir);
117  while (($file = readdir($handle)) !== false) {
118  if ($file == '.' || $file == '..') continue;
119  $plugin =& PluginRegistry::_instantiatePlugin($category, $categoryDir, $file);
120  if ($plugin && is_object($plugin)) {
121  $plugins[$plugin->getSeq()]["$categoryDir/$file"] =& $plugin;
122  unset($plugin);
123  }
124  }
125  closedir($handle);
126  }
127 
128  // If anyone else wants to jump category, here is the chance.
129  HookRegistry::call('PluginRegistry::loadCategory', array(&$category, &$plugins));
130 
131  // Register the plugins in sequence.
132  ksort($plugins);
133  foreach ($plugins as $seq => $junk1) {
134  foreach ($plugins[$seq] as $pluginPath => $junk2) {
135  PluginRegistry::register($category, $plugins[$seq][$pluginPath], $pluginPath);
136  }
137  }
138  unset($plugins);
139 
140  // Return the list of successfully-registered plugins.
141  $plugins =& PluginRegistry::getPlugins($category);
142  return $plugins;
143  }
144 
153  static function &loadPlugin($category, $pathName) {
154  $pluginPath = PLUGINS_PREFIX . $category . '/' . $pathName;
155  $plugin = null;
156  if (!file_exists($pluginPath . '/index.php')) return $plugin;
157 
158  $plugin = @include("$pluginPath/index.php");
159  if ($plugin && is_object($plugin)) {
160  PluginRegistry::register($category, $plugin, $pluginPath);
161  }
162  return $plugin;
163  }
164 
175  static function getCategories() {
176  $application = PKPApplication::getApplication();
177  $categories = $application->getPluginCategories();
178  HookRegistry::call('PluginRegistry::getCategories', array(&$categories));
179  return $categories;
180  }
181 
186  static function &loadAllPlugins($enabledOnly = false) {
187  // Retrieve and register categories (order is significant).
188  foreach (PluginRegistry::getCategories() as $category) {
189  PluginRegistry::loadCategory($category, $enabledOnly);
190  }
191  $allPlugins =& PluginRegistry::getAllPlugins();
192  return $allPlugins;
193  }
194 
195 
196  //
197  // Private helper methods
198  //
210  static function &_instantiatePlugin($category, $categoryDir, $file, $classToCheck = null) {
211  if(!is_null($classToCheck) && !preg_match('/[a-zA-Z0-9]+/', $file)) fatalError('Invalid product name "'.$file.'"!');
212 
213  $pluginPath = "$categoryDir/$file";
214  $plugin = null;
215 
216  // Try the plug-in wrapper first for backwards
217  // compatibility.
218  $pluginWrapper = "$pluginPath/index.php";
219  if (file_exists($pluginWrapper)) {
220  $plugin = include($pluginWrapper);
221  if ($classToCheck) {
222  assert(is_a($plugin, $classToCheck));
223  }
224  } else {
225  // Try the well-known plug-in class name next.
226  $pluginClassName = ucfirst($file).ucfirst($category).'Plugin';
227  $pluginClassFile = $pluginClassName.'.inc.php';
228  if (file_exists("$pluginPath/$pluginClassFile")) {
229  // Try to instantiate the plug-in class.
230  $pluginPackage = 'plugins.'.$category.'.'.$file;
231  $plugin =& instantiate($pluginPackage.'.'.$pluginClassName, $pluginClassName, $pluginPackage, 'register');
232  }
233  }
234 
235  // Make sure that the plug-in inherits from the right class.
236  if (is_object($plugin)) {
237  assert(is_a($plugin, 'Plugin'));
238  } else {
239  assert(is_null($plugin));
240  }
241 
242  return $plugin;
243  }
244 }
245 
246 ?>
static set($key, &$value)
static & getPlugins($category=null)
static & loadCategory($category, $enabledOnly=false, $mainContextId=null)
static & loadPlugin($category, $pathName)
static & _instantiatePlugin($category, $categoryDir, $file, $classToCheck=null)
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
static call($hookName, $args=null)
Registry class for managing plugins.
static & loadAllPlugins($enabledOnly=false)
static register($category, &$plugin, $path)
static & getPlugin($category, $name)
static & get($key, $createIfEmpty=false, $createWithDefault=null)
static & getAllPlugins()