Location: PHPKode > projects > PHPOlait Extended > phpolait/phpolait.smarty.class.php
<?php
//insert your smarty library here

	/**
	 * DEVNOTE: when using blind cache, every output from rpc called is cached.
	 * 			if the result is dynamicly changing, you should turn this off.
	 * 			another method is by writing your own cache management to manage rpc result cache
	 * @author hide@address.com, Oktober 2007
	 * DEVNOTE: there is a possible cheap solution to create a cache handler for blind cache.
	 * 			by using smarty mysql cache function handler follow this step
	 * 			- modify smarty mysql_cache_function handler to subtitude methodName subtitude in the cacheId
	 * 			- then, create a trigger in each table you would like to control the output caching for insert, update and delete
	 * 				set the trigger to delete any cache with cacheId matching the method output for each table.
	 * 			  eg. You have method name GetCountry to get content from table Country.
	 * 				  you should create trigger for table Country to delete cacheId with method GetWorld in it,
	 * 				  so whenever table World content is updated, inserted or deleted, the cache for that table is flush out.
	 */

	if (!defined("LIB_PHPOLAIT")){
		define("LIB_PHPOLAIT",dirname(__FILE__).DIRECTORY_SEPARATOR);
	}

	require_once(LIB_SMARTY."Smarty.class.php");
	require_once('phpolaitext.php');

	/**
	 * Use php olait experimental blind cache. use with caution.
	 */
	$PHPOLAIT_BLIND_CACHE = false;

	/**
	 * PHPOlait Smarty class
	 *
	 */
	class PHPOlaitSmarty extends Smarty {
		function __construct(){
			$this->Smarty();
			$this->use_sub_dirs 	  = true;
			$this->caching 			  = true;
			$this->cache_lifetime	  = 120;
			$this->cache_dir		  = LIB_PHPOLAIT . "cache" . DS;
			$this->template_dir		  = LIB_PHPOLAIT . "templates" . DS;
			$this->compile_dir		  = LIB_PHPOLAIT . "templates_c" . DS;

			//require_once(LIB_COMMON."frog.smarty".DIRECTORY_SEPARATOR."smarty.mysqlhandler.func.inc");
			//$this->cache_handler_func = "mysql_cache_handler";

			if (extension_loaded("eaccelerator")){
				require_once(LIB_COMMON."frog.smarty".DIRECTORY_SEPARATOR."smarty.eaccelerator.func.inc");
				$this->cache_handler_func = "smarty_cache_eaccelerator";
			}
		}
	}

	class SmartyRpcMethodCaller extends RpcMethodCaller {
		/**
		 * smarty class object
		 * TODO: Implement cache limiter and automatic flusher.
		 * 		 Automaticly select which function is cached and wich function will cause the cache to flush.
		 * 		 You should cache function that would likely eat a lot of resource.
		 * TODO: test caching mechanisme under production site.
		 *
		 * @var PHPOlaitSmarty
		 */
		private $smarty;
		/**
		 * is blind cache used?
		 *
		 * @var boolean
		 */
		private $useBlindCache;
		/**
		 * json wrapper object
		 *
		 * @var PHPJsonWrapper
		 */
		private $json;

		function __construct(PHPOlaitSmarty $oSmarty){


			parent::__construct();
			$this->json = new PHPJsonWrapper(JSON_PHP_FILE);
			$this->useBlindCache = $GLOBALS['PHPOLAIT_BLIND_CACHE'];
			//only use caching if the flag is on
			if ($this->useBlindCache)
				$this->smarty = $oSmarty;

		}

		/**
		 * Call remote method using array
		 *
		 * @param object $object
		 * @param string $method
		 * @param array $params
		 */
		function methodCallArray($callID,&$object,$method,&$params){
			//experimental!!! limit cache to only cache output from getter method
			if ($this->useBlindCache &&
			   (stristr($method,'get')||stristr($method,'test'))){
				$cacheID = $method . $this->json->encode(&$params);
				$this->smarty->assign('methodCaller',new RpcMethodCaller());
				$this->smarty->assign_by_ref('object',&$object);
				$this->smarty->assign_by_ref('params',&$params);
				$this->smarty->assign('method',$method);
				$this->smarty->display('methodCallArray.tpl',$cacheID);
			}else{
				parent::methodCallArray($callID,&$object,$method,&$params);
			}
		}

		/**
		 * Call remote method by passing the method
		 *
		 * @param string $method
		 * @param array $params
		 */
		function methodCallMethod($callID,$method,&$params){
			//experimental!!! limit cache to only cache output from getter method
			if ($this->useBlindCache &&
			   (stristr($method,'get')||stristr($method,'test'))){

				$cacheID = $method . $this->json->encode(&$params,true);
				$this->smarty->assign('methodCaller',new RpcMethodCaller());
				$this->smarty->assign_by_ref('params',&$params);
				$this->smarty->assign('method',$method);
				$this->smarty->display('methodCallMethod.tpl',$cacheID);
			}else{
				parent::methodCallMethod($callID,$method,&$params);
			}

		}


	}

/**
 * JSON RPC Server Wrapper.this class extends JSONRpcServer with smarty template engine for better performance
 */

	class JSONRpcServerSmart extends JSONRpcServerEx {

		private $smarty;

		function __construct($object,$methodMap=null,$config=null){
			$this->smarty = new PHPOlaitSmarty();

			//IF Blind cache flag is on, use our experimental smarty rpc method caller to cache function output
			if ($GLOBALS['PHPOLAIT_BLIND_CACHE']){
				//parent::RPCMethodCall(new SmartyRpcMethodCaller(&$this->smarty));
				$this->RpcMethodCallerObject = new SmartyRpcMethodCaller(&$this->smarty);
			}
			parent::__construct($object,$methodMap,$config);

		}

		//PHP5 Code only
	/**
	 * function to write javascript as pure code.
	 * all necessary libraries must be included manually in the calling page
	 * smarty version
	 * Prepares the javascript wrappers that will be presented on the client side.
 	* @param string $proxyvar The name of the proxy variable for accessing the JSON-RPC methods.
	 *
	 */
		function srcJavaScript($proxyvar, $proxyClassName = "PHPOlait"){
		    if ($this->methodMap==null) {	// This is the easy case
		        $methods = get_class_methods( $this->object );
				} else {
						$methods = array_keys( $this->methodMap );
				}

				$methodList = "'" . implode($methods, "','") . "'";

				//smarty start here

				$smarty = &$this->smarty;
				$pageUrl = $_SERVER["PHP_SELF"];
				$smarty->assign('pageURL', $pageUrl );
				$smarty->assign('jsolaitPath',$this->jsolaitlib);
				$smarty->assign('methodList',$methodList);
				$smarty->assign('methods',$methods);
				$smarty->assign('varName',$proxyvar);
				$smarty->assign('proxyClassName',$proxyClassName);

				$smarty->display('srcJavaScript.tpl',$pageUrl);

				/*
				$this->jsWrapperHeaderEx( $_SERVER["PHP_SELF"], $methods, $this->jsolaitlib , $proxyClassName );
				foreach ($methods as $name) {
						$this->jsWrapperMethod($name);
				}
				$this->jsWrapperFooterEx($proxyvar , $proxyClassName);
				*/

		}

		/**
		 * write DOJO's SMD generator
		 * @param string $proxyvar The name of the proxy variable for accessing the JSON-RPC methods.
		 */
		function srcSMD(){
			$strObjName = get_class($this->object);
			$reflectObj;
			if ($strObjName == ""){
				//this is error. throw proper smd error
			}else{
				$reflectObj = new ReflectionClass($strObjName);
				//$this->djSMDWrapperHeader($_SERVER["PHP_SELF"],$strObjName);

				//$aMethod = new ReflectionMethod();
				$blnFirstRun = true;
				//walk trough methods and its parameters
				$arrMethods = $reflectObj->getMethods();
				$pageUrl = $_SERVER["PHP_SELF"];

				$sm = &$this->smarty;

				$sm->assign('pageUrl',$pageUrl);
				$sm->assign('objectName',$strObjName);
				$sm->assign('arrMethods',$arrMethods);

	/*
				foreach ($reflectObj->getMethods() as $aMethod) {
					if (!$aMethod->isConstructor() && !$aMethod->isDestructor()){
						$strMethodCode =  $this->djSMDWrapperMethod($aMethod);
						if ($blnFirstRun){
							$blnFirstRun = false;
						}else{
							$strMethodCode = "," . $strMethodCode;
						}
						print($strMethodCode);
					}
				}
	*/

				//$this->djSMDWrapperFooter();

				$sm->display('srcSmd.tpl',$pageUrl);
			}
		}


	}


?>
Return current item: PHPOlait Extended