[259] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | File: xajaxPluginManager.inc.php |
---|
| 4 | |
---|
| 5 | Contains the xajax plugin manager. |
---|
| 6 | |
---|
| 7 | Title: xajax plugin manager |
---|
| 8 | |
---|
| 9 | Please see <copyright.inc.php> for a detailed description, copyright |
---|
| 10 | and license information. |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /* |
---|
| 14 | @package xajax |
---|
| 15 | @version $Id: xajaxPluginManager.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $ |
---|
| 16 | @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson |
---|
| 17 | @license http://www.xajaxproject.org/bsd_license.txt BSD License |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | //SkipAIO |
---|
| 21 | require(dirname(__FILE__) . '/xajaxPlugin.inc.php'); |
---|
| 22 | //EndSkipAIO |
---|
| 23 | |
---|
| 24 | /* |
---|
| 25 | Class: xajaxPluginManager |
---|
| 26 | */ |
---|
| 27 | class xajaxPluginManager |
---|
| 28 | { |
---|
| 29 | /* |
---|
| 30 | Array: aRequestPlugins |
---|
| 31 | */ |
---|
| 32 | var $aRequestPlugins; |
---|
| 33 | |
---|
| 34 | /* |
---|
| 35 | Array: aResponsePlugins |
---|
| 36 | */ |
---|
| 37 | var $aResponsePlugins; |
---|
| 38 | |
---|
| 39 | /* |
---|
| 40 | Array: aConfigurable |
---|
| 41 | */ |
---|
| 42 | var $aConfigurable; |
---|
| 43 | |
---|
| 44 | /* |
---|
| 45 | Array: aRegistrars |
---|
| 46 | */ |
---|
| 47 | var $aRegistrars; |
---|
| 48 | |
---|
| 49 | /* |
---|
| 50 | Array: aProcessors |
---|
| 51 | */ |
---|
| 52 | var $aProcessors; |
---|
| 53 | |
---|
| 54 | /* |
---|
| 55 | Array: aClientScriptGenerators |
---|
| 56 | */ |
---|
| 57 | var $aClientScriptGenerators; |
---|
| 58 | |
---|
| 59 | /* |
---|
| 60 | Function: xajaxPluginManager |
---|
| 61 | |
---|
| 62 | Construct and initialize the one and only xajax plugin manager. |
---|
| 63 | */ |
---|
| 64 | function xajaxPluginManager() |
---|
| 65 | { |
---|
| 66 | $this->aRequestPlugins = array(); |
---|
| 67 | $this->aResponsePlugins = array(); |
---|
| 68 | |
---|
| 69 | $this->aConfigurable = array(); |
---|
| 70 | $this->aRegistrars = array(); |
---|
| 71 | $this->aProcessors = array(); |
---|
| 72 | $this->aClientScriptGenerators = array(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /* |
---|
| 76 | Function: getInstance |
---|
| 77 | |
---|
| 78 | Implementation of the singleton pattern: returns the one and only instance of the |
---|
| 79 | xajax plugin manager. |
---|
| 80 | |
---|
| 81 | Returns: |
---|
| 82 | |
---|
| 83 | object - a reference to the one and only instance of the |
---|
| 84 | plugin manager. |
---|
| 85 | */ |
---|
| 86 | function &getInstance() |
---|
| 87 | { |
---|
| 88 | static $obj; |
---|
| 89 | if (!$obj) { |
---|
| 90 | $obj = new xajaxPluginManager(); |
---|
| 91 | } |
---|
| 92 | return $obj; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /* |
---|
| 96 | Function: loadPlugins |
---|
| 97 | |
---|
| 98 | Loads plugins from the folders specified. |
---|
| 99 | */ |
---|
| 100 | function loadPlugins($aFolders) |
---|
| 101 | { |
---|
| 102 | foreach ($aFolders as $sFolder) { |
---|
| 103 | if ($handle = opendir($sFolder)) { |
---|
| 104 | while (!(false === ($sName = readdir($handle)))) { |
---|
| 105 | $nLength = strlen($sName); |
---|
| 106 | if (8 < $nLength) { |
---|
| 107 | $sFileName = substr($sName, 0, $nLength - 8); |
---|
| 108 | $sExtension = substr($sName, $nLength - 8, 8); |
---|
| 109 | if ('.inc.php' == $sExtension) { |
---|
| 110 | require $sFolder . '/' . $sFileName . $sExtension; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | closedir($handle); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /* |
---|
| 121 | Function: _insertIntoArray |
---|
| 122 | |
---|
| 123 | Inserts an entry into an array given the specified priority number. |
---|
| 124 | If a plugin already exists with the given priority, the priority is |
---|
| 125 | automatically incremented until a free spot is found. The plugin |
---|
| 126 | is then inserted into the empty spot in the array. |
---|
| 127 | |
---|
| 128 | nPriorityNumber - (number): The desired priority, used to order |
---|
| 129 | the plugins. |
---|
| 130 | */ |
---|
| 131 | function _insertIntoArray(&$aPlugins, &$objPlugin, $nPriority) |
---|
| 132 | { |
---|
| 133 | while (isset($aPlugins[$nPriority])) |
---|
| 134 | $nPriority++; |
---|
| 135 | |
---|
| 136 | $aPlugins[$nPriority] =& $objPlugin; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /* |
---|
| 140 | Function: registerPlugin |
---|
| 141 | |
---|
| 142 | Registers a plugin. |
---|
| 143 | |
---|
| 144 | objPlugin - (object): A reference to an instance of a plugin. |
---|
| 145 | |
---|
| 146 | Below is a table for priorities and their description: |
---|
| 147 | 0 thru 999: Plugins that are part of or extensions to the xajax core |
---|
| 148 | 1000 thru 8999: User created plugins, typically, these plugins don't care about order |
---|
| 149 | 9000 thru 9999: Plugins that generally need to be last or near the end of the plugin list |
---|
| 150 | */ |
---|
| 151 | function registerPlugin(&$objPlugin, $nPriority=1000) |
---|
| 152 | { |
---|
| 153 | if (is_a($objPlugin, 'xajaxRequestPlugin')) |
---|
| 154 | { |
---|
| 155 | $this->_insertIntoArray($this->aRequestPlugins, $objPlugin, $nPriority); |
---|
| 156 | |
---|
| 157 | if (method_exists($objPlugin, 'register')) |
---|
| 158 | $this->_insertIntoArray($this->aRegistrars, $objPlugin, $nPriority); |
---|
| 159 | |
---|
| 160 | if (method_exists($objPlugin, 'canProcessRequest')) |
---|
| 161 | if (method_exists($objPlugin, 'processRequest')) |
---|
| 162 | $this->_insertIntoArray($this->aProcessors, $objPlugin, $nPriority); |
---|
| 163 | } |
---|
| 164 | else if (is_a($objPlugin, 'xajaxResponsePlugin')) |
---|
| 165 | { |
---|
| 166 | $this->aResponsePlugins[] =& $objPlugin; |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | //SkipDebug |
---|
| 171 | $objLanguageManager =& xajaxLanguageManager::getInstance(); |
---|
| 172 | trigger_error( |
---|
| 173 | $objLanguageManager->getText('XJXPM:IPLGERR:01') |
---|
| 174 | . get_class($objPlugin) |
---|
| 175 | . $objLanguageManager->getText('XJXPM:IPLGERR:02') |
---|
| 176 | , E_USER_ERROR |
---|
| 177 | ); |
---|
| 178 | //EndSkipDebug |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | if (method_exists($objPlugin, 'configure')) |
---|
| 182 | $this->_insertIntoArray($this->aConfigurable, $objPlugin, $nPriority); |
---|
| 183 | |
---|
| 184 | if (method_exists($objPlugin, 'generateClientScript')) |
---|
| 185 | $this->_insertIntoArray($this->aClientScriptGenerators, $objPlugin, $nPriority); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /* |
---|
| 189 | Function: canProcessRequest |
---|
| 190 | |
---|
| 191 | Calls each of the request plugins and determines if the |
---|
| 192 | current request can be processed by one of them. If no processor identifies |
---|
| 193 | the current request, then the request must be for the initial page load. |
---|
| 194 | |
---|
| 195 | See <xajax->canProcessRequest> for more information. |
---|
| 196 | */ |
---|
| 197 | function canProcessRequest() |
---|
| 198 | { |
---|
| 199 | $bHandled = false; |
---|
| 200 | |
---|
| 201 | $aKeys = array_keys($this->aProcessors); |
---|
| 202 | sort($aKeys); |
---|
| 203 | foreach ($aKeys as $sKey) { |
---|
| 204 | $mResult = $this->aProcessors[$sKey]->canProcessRequest(); |
---|
| 205 | if (true === $mResult) |
---|
| 206 | $bHandled = true; |
---|
| 207 | else if (is_string($mResult)) |
---|
| 208 | return $mResult; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | return $bHandled; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | /* |
---|
| 215 | Function: processRequest |
---|
| 216 | |
---|
| 217 | Calls each of the request plugins to request that they process the |
---|
| 218 | current request. If the plugin processes the request, it will |
---|
| 219 | return true. |
---|
| 220 | */ |
---|
| 221 | function processRequest() |
---|
| 222 | { |
---|
| 223 | $bHandled = false; |
---|
| 224 | |
---|
| 225 | $aKeys = array_keys($this->aProcessors); |
---|
| 226 | sort($aKeys); |
---|
| 227 | foreach ($aKeys as $sKey) { |
---|
| 228 | $mResult = $this->aProcessors[$sKey]->processRequest(); |
---|
| 229 | if (true === $mResult) |
---|
| 230 | $bHandled = true; |
---|
| 231 | else if (is_string($mResult)) |
---|
| 232 | return $mResult; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | return $bHandled; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /* |
---|
| 239 | Function: configure |
---|
| 240 | |
---|
| 241 | Call each of the request plugins passing along the configuration |
---|
| 242 | setting specified. |
---|
| 243 | |
---|
| 244 | sName - (string): The name of the configuration setting to set. |
---|
| 245 | mValue - (mixed): The value to be set. |
---|
| 246 | */ |
---|
| 247 | function configure($sName, $mValue) |
---|
| 248 | { |
---|
| 249 | $aKeys = array_keys($this->aConfigurable); |
---|
| 250 | sort($aKeys); |
---|
| 251 | foreach ($aKeys as $sKey) |
---|
| 252 | $this->aConfigurable[$sKey]->configure($sName, $mValue); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | /* |
---|
| 256 | Function: register |
---|
| 257 | |
---|
| 258 | Call each of the request plugins and give them the opportunity to |
---|
| 259 | handle the registration of the specified function, event or callable object. |
---|
| 260 | */ |
---|
| 261 | function register($aArgs) |
---|
| 262 | { |
---|
| 263 | $aKeys = array_keys($this->aRegistrars); |
---|
| 264 | sort($aKeys); |
---|
| 265 | foreach ($aKeys as $sKey) |
---|
| 266 | { |
---|
| 267 | $objPlugin =& $this->aRegistrars[$sKey]; |
---|
| 268 | $mResult =& $objPlugin->register($aArgs); |
---|
| 269 | if (is_a($mResult, 'xajaxRequest')) |
---|
| 270 | return $mResult; |
---|
| 271 | if (is_array($mResult)) |
---|
| 272 | return $mResult; |
---|
| 273 | if (is_bool($mResult)) |
---|
| 274 | if (true === $mResult) |
---|
| 275 | return true; |
---|
| 276 | } |
---|
| 277 | //SkipDebug |
---|
| 278 | $objLanguageManager =& xajaxLanguageManager::getInstance(); |
---|
| 279 | trigger_error( |
---|
| 280 | $objLanguageManager->getText('XJXPM:MRMERR:01') |
---|
| 281 | . print_r($aArgs, true) |
---|
| 282 | , E_USER_ERROR |
---|
| 283 | ); |
---|
| 284 | //EndSkipDebug |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | /* |
---|
| 288 | Function: generateClientScript |
---|
| 289 | |
---|
| 290 | Call each of the request and response plugins giving them the |
---|
| 291 | opportunity to output some javascript to the page being generated. This |
---|
| 292 | is called only when the page is being loaded initially. This is not |
---|
| 293 | called when processing a request. |
---|
| 294 | */ |
---|
| 295 | function generateClientScript() |
---|
| 296 | { |
---|
| 297 | $aKeys = array_keys($this->aClientScriptGenerators); |
---|
| 298 | sort($aKeys); |
---|
| 299 | foreach ($aKeys as $sKey) |
---|
| 300 | $this->aClientScriptGenerators[$sKey]->generateClientScript(); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | /* |
---|
| 304 | Function: getPlugin |
---|
| 305 | |
---|
| 306 | Locate the specified response plugin by name and return |
---|
| 307 | a reference to it if one exists. |
---|
| 308 | */ |
---|
| 309 | function &getPlugin($sName) |
---|
| 310 | { |
---|
| 311 | $aKeys = array_keys($this->aResponsePlugins); |
---|
| 312 | sort($aKeys); |
---|
| 313 | foreach ($aKeys as $sKey) |
---|
| 314 | if (is_a($this->aResponsePlugins[$sKey], $sName)) |
---|
| 315 | return $this->aResponsePlugins[$sKey]; |
---|
| 316 | |
---|
| 317 | $bFailure = false; |
---|
| 318 | return $bFailure; |
---|
| 319 | } |
---|
| 320 | } |
---|