PageRenderTime 2ms CodeModel.GetById 16ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  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
 21require(dirname(__FILE__) . '/xajaxPlugin.inc.php');
 22//EndSkipAIO
 23
 24/*
 25	Class: xajaxPluginManager
 26*/
 27class 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 (is_dir($sFolder))
104			if ($handle = opendir($sFolder)) {
105				while (!(false === ($sName = readdir($handle)))) {
106					$nLength = strlen($sName);
107					if (8 < $nLength) {
108						$sFileName = substr($sName, 0, $nLength - 8);
109						$sExtension = substr($sName, $nLength - 8, 8);
110						if ('.inc.php' == $sExtension) {
111							require $sFolder . '/' . $sFileName . $sExtension;
112						}
113					}
114				}
115				
116				closedir($handle);
117			}
118		}
119	}
120	
121	/*
122		Function: _insertIntoArray
123		
124		Inserts an entry into an array given the specified priority number. 
125		If a plugin already exists with the given priority, the priority is
126		automatically incremented until a free spot is found.  The plugin
127		is then inserted into the empty spot in the array.
128		
129		nPriorityNumber - (number):  The desired priority, used to order
130			the plugins.
131	*/
132	function _insertIntoArray(&$aPlugins, &$objPlugin, $nPriority)
133	{
134		while (isset($aPlugins[$nPriority]))
135			$nPriority++;
136		
137		$aPlugins[$nPriority] =& $objPlugin;
138	}
139	
140	/*
141		Function: registerPlugin
142		
143		Registers a plugin.
144		
145		objPlugin - (object):  A reference to an instance of a plugin.
146		
147		Below is a table for priorities and their description:
148		0 thru 999: Plugins that are part of or extensions to the xajax core
149		1000 thru 8999: User created plugins, typically, these plugins don't care about order
150		9000 thru 9999: Plugins that generally need to be last or near the end of the plugin list
151	*/
152	function registerPlugin(&$objPlugin, $nPriority=1000)
153	{
154		if (is_a($objPlugin, 'xajaxRequestPlugin'))
155		{
156			$this->_insertIntoArray($this->aRequestPlugins, $objPlugin, $nPriority);
157			
158			if (method_exists($objPlugin, 'register'))
159				$this->_insertIntoArray($this->aRegistrars, $objPlugin, $nPriority);
160			
161			if (method_exists($objPlugin, 'canProcessRequest'))
162				if (method_exists($objPlugin, 'processRequest'))
163					$this->_insertIntoArray($this->aProcessors, $objPlugin, $nPriority);
164		}
165		else if (is_a($objPlugin, 'xajaxResponsePlugin'))
166		{
167			$this->aResponsePlugins[] =& $objPlugin;
168		}
169		else
170		{
171//SkipDebug
172			$objLanguageManager =& xajaxLanguageManager::getInstance();
173			trigger_error(
174				$objLanguageManager->getText('XJXPM:IPLGERR:01') 
175				. get_class($objPlugin) 
176				. $objLanguageManager->getText('XJXPM:IPLGERR:02')
177				, E_USER_ERROR
178				);
179//EndSkipDebug
180		}
181		
182		if (method_exists($objPlugin, 'configure'))
183			$this->_insertIntoArray($this->aConfigurable, $objPlugin, $nPriority);
184
185		if (method_exists($objPlugin, 'generateClientScript'))
186			$this->_insertIntoArray($this->aClientScriptGenerators, $objPlugin, $nPriority);
187	}
188
189	/*
190		Function: canProcessRequest
191		
192		Calls each of the request plugins and determines if the
193		current request can be processed by one of them.  If no processor identifies
194		the current request, then the request must be for the initial page load.
195		
196		See <xajax->canProcessRequest> for more information.
197	*/
198	function canProcessRequest()
199	{
200		$bHandled = false;
201		
202		$aKeys = array_keys($this->aProcessors);
203		sort($aKeys);
204		foreach ($aKeys as $sKey) {
205			$mResult = $this->aProcessors[$sKey]->canProcessRequest();
206			if (true === $mResult)
207				$bHandled = true;
208			else if (is_string($mResult))
209				return $mResult;
210		}
211
212		return $bHandled;
213	}
214
215	/*
216		Function: processRequest
217		
218		Calls each of the request plugins to request that they process the
219		current request.  If the plugin processes the request, it will
220		return true.
221	*/
222	function processRequest()
223	{
224		$bHandled = false;
225		
226		$aKeys = array_keys($this->aProcessors);
227		sort($aKeys);
228		foreach ($aKeys as $sKey) {
229			$mResult = $this->aProcessors[$sKey]->processRequest();
230			if (true === $mResult)
231				$bHandled = true;
232			else if (is_string($mResult))
233				return $mResult;
234		}
235
236		return $bHandled;
237	}
238	
239	/*
240		Function: configure
241		
242		Call each of the request plugins passing along the configuration
243		setting specified.
244		
245		sName - (string):  The name of the configuration setting to set.
246		mValue - (mixed):  The value to be set.
247	*/
248	function configure($sName, $mValue)
249	{
250		$aKeys = array_keys($this->aConfigurable);
251		sort($aKeys);
252		foreach ($aKeys as $sKey)
253			$this->aConfigurable[$sKey]->configure($sName, $mValue);
254	}
255	
256	/*
257		Function: register
258		
259		Call each of the request plugins and give them the opportunity to 
260		handle the registration of the specified function, event or callable object.
261	*/
262	function register($aArgs)
263	{
264		$aKeys = array_keys($this->aRegistrars);
265		sort($aKeys);
266		foreach ($aKeys as $sKey)
267		{
268			$objPlugin =& $this->aRegistrars[$sKey];
269			$mResult =& $objPlugin->register($aArgs);
270			if (is_a($mResult, 'xajaxRequest'))
271				return $mResult;
272			if (is_array($mResult))
273				return $mResult;
274			if (is_bool($mResult))
275				if (true === $mResult)
276					return true;
277		}
278//SkipDebug
279		$objLanguageManager =& xajaxLanguageManager::getInstance();
280		trigger_error(
281			$objLanguageManager->getText('XJXPM:MRMERR:01') 
282			. print_r($aArgs, true)
283			, E_USER_ERROR
284			);
285//EndSkipDebug
286	}
287	
288	/*
289		Function: generateClientScript
290		
291		Call each of the request and response plugins giving them the
292		opportunity to output some javascript to the page being generated.  This
293		is called only when the page is being loaded initially.  This is not 
294		called when processing a request.
295	*/
296	function generateClientScript()
297	{
298		$aKeys = array_keys($this->aClientScriptGenerators);
299		sort($aKeys);
300		foreach ($aKeys as $sKey)
301			$this->aClientScriptGenerators[$sKey]->generateClientScript();
302	}
303	
304	/*
305		Function: getPlugin
306		
307		Locate the specified response plugin by name and return
308		a reference to it if one exists.
309	*/
310	function &getPlugin($sName)
311	{
312		$aKeys = array_keys($this->aResponsePlugins);
313		sort($aKeys);
314		foreach ($aKeys as $sKey)
315			if (is_a($this->aResponsePlugins[$sKey], $sName))
316				return $this->aResponsePlugins[$sKey];
317
318		$bFailure = false;
319		return $bFailure;
320	}
321}