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

/0.1/ccds_library/xajax/xajax_core/xajaxPluginManager.inc.php

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