Location: PHPKode > projects > web.framework > web.framework-1.0.0/web.framework/AJAX/WebAJAX.class.php
<?php
/**
 * Project:     web.framework: the PHP5 MVC framework
 * File:		WebAJAX.class.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the authors of web.framework by e-mail at:
 * hide@address.com
 *
 * The latest version of web.framework can be obtained from:
 * http://sourceforge.net/projects/webframework
 *
 * @link http://sourceforge.net/projects/webframework
 * @copyright 2005 Marcin Staniszczak
 * @author Marcin Staniszczak <hide@address.com>
 * @version 1.0.0
 */


/**
 * The main class
 *
 * @name WebAJAX
 * @version 1.0.0
 * @package web.framework
 * @subpackage web.AJAX
 *
 * @author Marcin Staniszczak
 * @copyright 2005 Marcin Staniszczak
 */
class WebAJAX {
	/**
	 * @var array configuration
	 */
	private $arrConfiguration = array();

	/**
	 * @var string directory for AJAX cache
	 */
	private $strCachePath = '';

	/**
	 * @var string URL to AJAX engine
	 */
	private $strURL;

	/**
	 * @var string URL to AJAX engine
	 */
	private $blnAddAJAXJS = true;

	/**
	 * The class constructor
	 *
	 * @access public
	 * @param array array with AJAX configuration
	 * @param string URL to AJAX engine
	 * @param boolean true - add AJAX JS code, false - do not add AJAX JS (you can include it - see JS subdirectory)
	 */
	public function __construct($arrConfiguration, $strURL, $blnAddAJAXJS=true) {
		$this->arrConfiguration = $arrConfiguration;
		$this->strCachePath = isset($arrConfiguration['config']['cachepath']) ? $arrConfiguration['config']['cachepath'] : false;
		$this->strURL = $strURL;
		$this->blnAddAJAXJS = $blnAddAJAXJS;
	}

	/**
	 * Make JavaScript frame from PHP classes for usage from client side
	 *
	 * @access private
	 * @return string JavaScript code
	 */
	private function makeMethodsJS($arrClasses) {
		$strJSCode = '';
		$blnCaching = false;


		if (isset($this->arrConfiguration['config']['cachepath'])) {
			$strCacheDriver = isset($this->arrConfiguration['config']['cachdriver']) ? $this->arrConfiguration['config']['cachdriver'] : 'WADefaultCache';
			require_once(WEBFRAMEWORK_CORE_DIR.'AJAX'.DIRECTORY_SEPARATOR.'CacheDriver'.DIRECTORY_SEPARATOR.$strCacheDriver.'.class.php');

			$objCacheDriver = new $strCacheDriver($this->arrConfiguration['config']['cachepath']);
			$blnCaching = true;
		}

		foreach ($arrClasses as $strClass=>$strPath) {

			if (!isset($this->arrConfiguration['classes'][$strClass])) {
				throw new WF_AJAX_Exception();
			}

			$strPath = '';
			if (strlen($this->arrConfiguration['classes'][$strClass])>0) {
				$strPath = $this->arrConfiguration['classes'][$strClass];
			} elseif (isset($this->arrConfiguration['config']) && isset($this->arrConfiguration['config']['path'])) {
				$strPath = $this->arrConfiguration['config']['path'];
			}

			$intClassCDate = filectime($strPath.$strClass.'.class.php');
			$strJSCode = $objCacheDriver->load($strClass, $intClassCDate);
//echo $strJSCode;
			if ($blnCaching && $strJSCode===false) {

				require_once($strPath.$strClass.'.class.php');

				$strClassJSCode = '';
				$strClassJSCode .= "function $strClass() {}\n";

				$arrMethods = get_class_methods($strClass);

				foreach ($arrMethods as $strMethodName) {
					$objReflect = new ReflectionMethod($strClass, $strMethodName);

					if ($objReflect->isPublic() && !in_array($strMethodName, array('__construct', '__destruct', '__set', '__get', '__isset', '__call', '__unset'))) {
						$strClassJSCode .= "$strClass.prototype.$strMethodName = function() {\n";
						$strClassJSCode .= "\tstrURL = '';\n";
						$strClassJSCode .= "\tfor (i=0;i<arguments.length;i++) {\n";
						$strClassJSCode .= "\t\tstrURL += 'margs[]='+escape(arguments[i])+'&';\n";
						$strClassJSCode .= "\t}\n";
						$strClassJSCode .= "\tajax.callMethod('$strClass' ,'$strMethodName', strURL, {$strMethodName}_result);\n";
						$strClassJSCode .= "}\n\n";


					}
				}
				$strJSCode .= $strClassJSCode;
				$objCacheDriver->save($strClass, $strJSCode);
			}

		}


		return $strJSCode;
	}

	/**
	 * Generate code to make web.AJAX JS object
	 *
	 * @access private
	 * @return string JS code
	 */
	private function makeHeaderJS() {
		return "\najax = new WebAJAX('{$this->strURL}');\n\n";
	}

	/**
	 * Generate main web.AJAX JavaScript code
	 *
	 * @access private
	 * @return string JS code
	 */
	private function makeWebAJAXJS() {
		return implode("", file(WEBFRAMEWORK_CORE_DIR.'AJAX'.DIRECTORY_SEPARATOR.'JS'.DIRECTORY_SEPARATOR.'WebAJAX.js'));
	}

	/**
	 * Return JavaScript code
	 *
	 * @access public
	 * @param array array with PHP classes to access from JS
	 * @return string JS code
	 */
	public function getJS($arrClasses) {
		$strJS = '';

		if ($this->blnAddAJAXJS) {
			$strJS .= $this->makeWebAJAXJS();
		}

		$strJS .= $this->makeHeaderJS();
		$strJS .= $this->makeMethodsJS($arrClasses);

		return $strJS;
	}

	/**
	 * Call PHP method
	 *
	 * @access public
	 * @param string class name
	 * @param string method name
	 * @param array array with method arguments
	 */
	public function callMethod($strClassName, $strMethodName, $arrArguments) {
		$strPath = '';
		if (strlen($this->arrConfiguration['classes'][$strClassName])>0) {
			$strPath = $this->arrConfiguration['classes'][$strClassName];
		} elseif (isset($this->arrConfiguration['config']) && isset($this->arrConfiguration['config']['path'])) {
			$strPath = $this->arrConfiguration['config']['path'];
		}

		require_once($strPath.$strClassName.'.class.php');

		$objClass = new $strClassName();

		echo call_user_func_array(array($objClass, $strMethodName), $arrArguments);
	}
}

/**
 * Exception of web.framework - no page (404)
 * @subpackage exceptions
 */
class WF_AJAX_Exception extends Exception {}
?>
Return current item: web.framework