10 * |
10 * |
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
13 */ |
13 */ |
14 |
14 |
|
15 /** |
|
16 * Class used to handle and process plugin requests and loading. Singleton. |
|
17 * @package Enano |
|
18 * @author Dan Fuhry <dan@enanocms.org> |
|
19 * @copyright (C) 2006-2008 Enano Project |
|
20 * @license GNU General Public License <http://enanocms.org/Special:GNU_General_Public_License> |
|
21 */ |
|
22 |
15 class pluginLoader { |
23 class pluginLoader { |
|
24 |
|
25 /** |
|
26 * The list of hooks registered. |
|
27 * @var array |
|
28 * @access private |
|
29 */ |
|
30 |
16 var $hook_list; |
31 var $hook_list; |
|
32 |
|
33 /** |
|
34 * The list of plugins that should be loaded. Used only by common.php. |
|
35 * @var array |
|
36 * @access private |
|
37 */ |
|
38 |
17 var $load_list; |
39 var $load_list; |
|
40 |
|
41 /** |
|
42 * The list of plugins that are loaded currently. This is only used by the loaded() method which in turn is |
|
43 * used by template files with the <!-- IFPLUGIN --> special tag. |
|
44 * @var array |
|
45 * @access private |
|
46 */ |
|
47 |
18 var $loaded_plugins; |
48 var $loaded_plugins; |
|
49 |
|
50 /** |
|
51 * The list of plugins that are always loaded because they're part of the Enano core. This cannot be modified |
|
52 * by any external code because user plugins are loaded after the load_list is calculated. Can be useful in |
|
53 * alternative administration panel frameworks that need the list of system plugins. |
|
54 * @var array |
|
55 */ |
|
56 |
19 var $system_plugins = Array('SpecialUserFuncs.php','SpecialUserPrefs.php','SpecialPageFuncs.php','SpecialAdmin.php','SpecialCSS.php','SpecialUpdownload.php','SpecialSearch.php','PrivateMessages.php','SpecialGroups.php', 'SpecialRecentChanges.php'); |
57 var $system_plugins = Array('SpecialUserFuncs.php','SpecialUserPrefs.php','SpecialPageFuncs.php','SpecialAdmin.php','SpecialCSS.php','SpecialUpdownload.php','SpecialSearch.php','PrivateMessages.php','SpecialGroups.php', 'SpecialRecentChanges.php'); |
|
58 |
|
59 /** |
|
60 * Name kept for compatibility. Effectively a constructor. Calculates the list of plugins that should be loaded |
|
61 * and puts that list in the $load_list property. Plugin developers have absolutely no use for this whatsoever. |
|
62 */ |
|
63 |
20 function loadAll() |
64 function loadAll() |
21 { |
65 { |
22 $dir = ENANO_ROOT.'/plugins/'; |
66 $dir = ENANO_ROOT.'/plugins/'; |
23 |
67 |
24 $this->load_list = Array(); |
68 $this->load_list = Array(); |
64 } |
108 } |
65 } |
109 } |
66 $this->loaded_plugins = $plugins; |
110 $this->loaded_plugins = $plugins; |
67 //die('<pre>'.htmlspecialchars(print_r($plugins, true)).'</pre>'); |
111 //die('<pre>'.htmlspecialchars(print_r($plugins, true)).'</pre>'); |
68 } |
112 } |
|
113 |
|
114 /** |
|
115 * Name kept for compatibility. This method is used to add a new hook into the code somewhere. Plugins are encouraged |
|
116 * to set hooks and hook into other plugins in a fail-safe way, this encourages reuse of code. Returns an array, whose |
|
117 * values should be eval'ed. |
|
118 * @example <code> |
|
119 $code = $plugins->setHook('my_hook_name'); |
|
120 foreach ( $code as $cmd ) |
|
121 { |
|
122 eval($cmd); |
|
123 } |
|
124 </code> |
|
125 * @param string The name of the hook. |
|
126 * @param array Deprecated. |
|
127 */ |
|
128 |
69 function setHook($name, $opts = Array()) { |
129 function setHook($name, $opts = Array()) { |
70 /* |
|
71 $r = Array(); |
|
72 if(isset($this->hook_list[$name])) { |
|
73 for($i=0;$i<sizeof($this->hook_list[$name]);$i++) { |
|
74 $ret = eval($this->hook_list[$name][$i]); |
|
75 if($ret !== null) $r[] = $ret; |
|
76 } |
|
77 } |
|
78 if(sizeof($r) > 0) return $r; |
|
79 else return false; |
|
80 */ |
|
81 if(isset($this->hook_list[$name]) && is_array($this->hook_list[$name])) |
130 if(isset($this->hook_list[$name]) && is_array($this->hook_list[$name])) |
82 { |
131 { |
83 return array(implode("\n", $this->hook_list[$name])); |
132 return array(implode("\n", $this->hook_list[$name])); |
84 } |
133 } |
85 else |
134 else |
86 { |
135 { |
87 return Array(); |
136 return Array(); |
88 } |
137 } |
89 } |
138 } |
|
139 |
|
140 /** |
|
141 * Attaches to a hook effectively scheduling some code to be run at that point. You should try to keep hooks clean by |
|
142 * making a function that has variables that need to be modified passed by reference. |
|
143 * @example Simple example: <code> |
|
144 $plugins->attachHook('render_wikiformat_pre', '$text = str_replace("Goodbye, Mr. Chips", "Hello, Mr. Carrots", $text);'); |
|
145 </code> |
|
146 * @example More complicated example: <code> |
|
147 $plugins->attachHook('render_wikiformat_pre', 'myplugin_parser_ext($text);'); |
|
148 |
|
149 // Notice that $text is passed by reference. |
|
150 function myplugin_parser_ext(&$text) |
|
151 { |
|
152 $text = str_replace("Goodbye, Mr. Chips", "Hello, Mr. Carrots", $text); |
|
153 } |
|
154 </code> |
|
155 */ |
|
156 |
90 function attachHook($name, $code) { |
157 function attachHook($name, $code) { |
91 if(!isset($this->hook_list[$name])) |
158 if(!isset($this->hook_list[$name])) |
92 { |
159 { |
93 $this->hook_list[$name] = Array(); |
160 $this->hook_list[$name] = Array(); |
94 } |
161 } |
95 $this->hook_list[$name][] = $code; |
162 $this->hook_list[$name][] = $code; |
96 } |
163 } |
|
164 |
|
165 /** |
|
166 * Tell whether a plugin is loaded or not. |
|
167 * @param string The filename of the plugin |
|
168 * @return bool |
|
169 */ |
|
170 |
97 function loaded($plugid) |
171 function loaded($plugid) |
98 { |
172 { |
99 return isset( $this->loaded_plugins[$plugid] ); |
173 return isset( $this->loaded_plugins[$plugid] ); |
100 } |
174 } |
101 } |
175 } |