baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / plugins / PluginRegistry.inc.php
blob22561768528a155d179684764ae5991219442aeb
1 <?php
3 /**
4 * @file classes/plugins/PluginRegistry.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class PluginRegistry
10 * @ingroup plugins
11 * @see Plugin
13 * @brief Registry class for managing plugins.
16 // $Id: PluginRegistry.inc.php,v 1.8 2009/08/31 16:29:56 asmecher Exp $
19 define('PLUGINS_PREFIX', 'plugins/');
21 class PluginRegistry {
22 /**
23 * Return all plugins in the given category as an array, or, if the
24 * category is not specified, all plugins in an associative array of
25 * arrays by category.
26 * @param $category String the name of the category to retrieve
28 function &getPlugins($category = null) {
29 $plugins =& Registry::get('plugins');
30 if ($category !== null) return $plugins[$category];
31 return $plugins;
34 /**
35 * Get all plugins in a single array.
37 function &getAllPlugins() {
38 $plugins =& PluginRegistry::getPlugins();
39 $allPlugins = array();
40 if (is_array($plugins)) foreach ($plugins as $category => $list) {
41 if (is_array($list)) $allPlugins += $list;
43 return $allPlugins;
46 /**
47 * Register a plugin with the registry in the given category.
48 * @param $category String the name of the category to extend
49 * @param $plugin The instantiated plugin to add
50 * @param $path The path the plugin was found in
51 * @return boolean True IFF the plugin was registered successfully
53 function register($category, &$plugin, $path) {
54 $pluginName = $plugin->getName();
55 $plugins =& PluginRegistry::getPlugins();
56 if (!$plugins) $plugins = array();
58 // If the plugin was already loaded, do not load it again.
59 if (isset($plugins[$category][$pluginName])) return false;
61 // Allow the plugin to register.
62 if (!$plugin->register($category, $path)) return false;
64 if (isset($plugins[$category])) $plugins[$category][$plugin->getName()] =& $plugin;
65 else $plugins[$category] = array($plugin->getName() => &$plugin);
66 Registry::set('plugins', $plugins);
67 return true;
70 /**
71 * Get a plugin by name.
72 * @param $category String category name
73 * @param $name String plugin name
75 function &getPlugin ($category, $name) {
76 $plugins =& PluginRegistry::getPlugins();
77 $plugin = @$plugins[$category][$name];
78 return $plugin;
81 /**
82 * Load all plugins for a given category.
83 * @param $category String The name of the category to load
84 * @param $forceLoad boolean Whether or not to force loading of the
85 * category (since if e.g. a single plugin is already registered, the
86 * current set will be returned rather than attempting to load others)
88 function &loadCategory ($category, $forceLoad = false) {
89 $plugins = array();
90 $categoryDir = PLUGINS_PREFIX . $category;
91 if (!is_dir($categoryDir)) return $plugins;
93 $handle = opendir($categoryDir);
94 while (($file = readdir($handle)) !== false) {
95 if ($file == '.' || $file == '..') continue;
96 $pluginPath = "$categoryDir/$file";
97 $pluginWrapper = "$pluginPath/index.php";
99 if (!file_exists($pluginWrapper)) continue;
100 $plugin = include($pluginWrapper);
101 if ($plugin && is_object($plugin)) {
102 $plugins[$plugin->getSeq()][$pluginPath] =& $plugin;
103 unset($plugin);
106 closedir($handle);
108 // If anyone else wants to jump category, here is the chance.
109 HookRegistry::call('PluginRegistry::loadCategory', array(&$category, &$plugins));
111 // Register the plugins in sequence.
112 ksort($plugins);
113 foreach ($plugins as $seq => $junk1) {
114 foreach ($plugins[$seq] as $pluginPath => $junk2) {
115 PluginRegistry::register($category, $plugins[$seq][$pluginPath], $pluginPath);
118 unset($plugins);
120 // Return the list of successfully-registered plugins.
121 $plugins =& PluginRegistry::getPlugins($category);
122 return $plugins;
126 * Load a specific plugin from a category by path name.
127 * Similar to loadCategory, except that it only loads a single plugin
128 * within a category rather than loading all.
129 * @param $category string
130 * @param $pathName string
131 * @return object
133 function &loadPlugin($category, $pathName) {
134 $pluginPath = PLUGINS_PREFIX . $category . '/' . $pathName;
135 $plugin = null;
136 if (!file_exists($pluginPath . '/index.php')) return $plugin;
138 $plugin = @include("$pluginPath/index.php");
139 if ($plugin && is_object($plugin)) {
140 PluginRegistry::register($category, $plugin, $pluginPath);
142 return $plugin;
146 * Get a list of the various plugin categories available.
148 function getCategories() {
149 $application =& PKPApplication::getApplication();
150 $categories = $application->getPluginCategories();
151 HookRegistry::call('PluginRegistry::getCategories', array(&$categories));
152 return $categories;
156 * Load all plugins in the system and return them in a single array.
158 function &loadAllPlugins() {
159 foreach (PluginRegistry::getCategories() as $category) {
160 PluginRegistry::loadCategory($category);
162 $allPlugins =& PluginRegistry::getAllPlugins();
163 return $allPlugins;