PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/common/xajax/xajax_core/xajaxPluginManager.inc.php

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