PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/WebContent/Chapter13-AjaxFrameworks/JPSpan/JPSpan/Generator.php

https://gitlab.com/bruce.ng/Professional-Ajax
PHP | 240 lines | 101 code | 34 blank | 105 comment | 6 complexity | 96241a419fee365b0544361d5fb3f853 MD5 | raw file
  1. <?php
  2. /**
  3. * @package JPSpan
  4. * @subpackage Generator
  5. * @version $Id: Generator.php,v 1.4 2005/06/02 23:47:15 harryf Exp $
  6. */
  7. //--------------------------------------------------------------------------------
  8. /**
  9. * Define
  10. */
  11. if ( !defined('JPSPAN') ) {
  12. define ('JPSPAN',dirname(__FILE__).'/');
  13. }
  14. /**
  15. * Generaters client-side Javascript primed to access a server
  16. * Works with JPSpan_HandleDescription to generate
  17. * client primed for a server
  18. * @todo Review this - may be worth eliminating - not serving much useful purpose
  19. * @see JPSpan_Server::getGenerator()
  20. * @package JPSpan
  21. * @subpackage Generator
  22. * @access public
  23. */
  24. class JPSpan_Generator {
  25. /**
  26. * Object responsible for generating client
  27. * @var object
  28. * @access private
  29. */
  30. var $ClientGenerator;
  31. /**
  32. * Initialize the generator
  33. * @param Object responsible for generating client
  34. * @param array of JPSpan_HandleDescription objects
  35. * @param string URL of the server
  36. * @param string encoding to use when making requests (e.g. xml or php)
  37. * @access public
  38. * @todo This method needs to die - just setup the ClientGenerator object
  39. */
  40. function init(& $ClientGenerator, & $descriptions, $serverUrl, $encoding) {
  41. $this->ClientGenerator = & $ClientGenerator;
  42. $this->ClientGenerator->descriptions = & $descriptions;
  43. $this->ClientGenerator->serverUrl = $serverUrl;
  44. $this->ClientGenerator->RequestEncoding = $encoding;
  45. }
  46. /**
  47. * Return the Javascript client for the server
  48. * @return string Javascript
  49. * @access public
  50. */
  51. function getClient() {
  52. require_once JPSPAN . 'CodeWriter.php';
  53. $Code = & new JPSpan_CodeWriter();
  54. $this->ClientGenerator->generate($Code);
  55. return $Code->toString();
  56. }
  57. }
  58. //--------------------------------------------------------------------------------
  59. /**
  60. * @package JPSpan
  61. * @subpackage Generator
  62. * @access public
  63. */
  64. class JPSpan_Generator_AdHoc {
  65. var $descriptions = array();
  66. var $RequestEncoding = 'xml';
  67. var $RequestMethod = 'rawpost';
  68. var $jsRequestClass = 'JPSpan_Request_RawPost';
  69. var $jsEncodingClass = 'JPSpan_Encode_Xml';
  70. function addDescription($description) {
  71. $this->descriptions[$description->jsClass] = $description;
  72. }
  73. /**
  74. * Invokes code generator
  75. * @param JPSpan_CodeWriter
  76. * @return void
  77. * @access public
  78. */
  79. function generate(& $Code) {
  80. switch ( $this->RequestMethod ) {
  81. case 'rawpost':
  82. $this->jsRequestClass = 'JPSpan_Request_RawPost';
  83. break;
  84. case 'post':
  85. $this->jsRequestClass = 'JPSpan_Request_Post';
  86. break;
  87. case 'get':
  88. // The JPSpan JS GetRequest object has bugs plus
  89. // changing state via GET is bad idea
  90. // http://www.intertwingly.net/blog/2005/03/16/AJAX-Considered-Harmful
  91. trigger_error('Sending data via GET vars not supported',E_USER_ERROR);
  92. break;
  93. default:
  94. trigger_error('Request method unknown: '.$this->RequestMethod,E_USER_ERROR);
  95. break;
  96. }
  97. if ( $this->RequestEncoding == 'xml' ) {
  98. $this->jsEncodingClass = 'JPSpan_Encode_Xml';
  99. } else {
  100. $this->jsEncodingClass = 'JPSpan_Encode_PHP';
  101. }
  102. $this->generateScriptHeader($Code);
  103. foreach ( array_keys($this->descriptions) as $key ) {
  104. $this->generateJsClass($Code, $this->descriptions[$key]);
  105. }
  106. }
  107. /**
  108. * Generate the starting includes section of the script
  109. * @param JPSpan_CodeWriter
  110. * @return void
  111. * @access private
  112. */
  113. function generateScriptHeader(& $Code) {
  114. ob_start();
  115. ?>
  116. /**@
  117. * include 'remoteobject.js';
  118. <?php
  119. switch ( $this->RequestMethod ) {
  120. case 'rawpost':
  121. ?>
  122. * include 'request/rawpost.js';
  123. <?php
  124. break;
  125. case 'post':
  126. ?>
  127. * include 'request/rawpost.js';
  128. <?php
  129. break;
  130. }
  131. if ( $this->RequestEncoding == 'xml' ) {
  132. ?>
  133. * include 'encode/xml.js';
  134. <?php
  135. } else {
  136. ?>
  137. * include 'encode/php.js';
  138. <?php
  139. }
  140. ?>
  141. */
  142. <?php
  143. $Code->append(ob_get_contents());
  144. ob_end_clean();
  145. }
  146. /**
  147. * Generate code for a single description (a single PHP class)
  148. * @param JPSpan_CodeWriter
  149. * @param JPSpan_HandleDescription
  150. * @return void
  151. * @access private
  152. */
  153. function generateJsClass(& $Code, & $Description) {
  154. ob_start();
  155. ?>
  156. function <?php echo $Description->Class; ?>() {
  157. var oParent = new JPSpan_RemoteObject();
  158. if ( arguments[0] ) {
  159. oParent.Async(arguments[0]);
  160. }
  161. oParent.__remoteClass = '<?php echo $Description->Class; ?>';
  162. oParent.__request = new <?php echo $this->jsRequestClass;
  163. ?>(new <?php echo $this->jsEncodingClass; ?>());
  164. <?php
  165. foreach ( $Description->methods as $method => $url ) {
  166. ?>
  167. // @access public
  168. oParent.<?php echo $method; ?> = function() {
  169. return this.__call('<?php echo $url; ?>',arguments,'<?php echo $method; ?>');
  170. };
  171. <?php
  172. }
  173. ?>
  174. return oParent;
  175. }
  176. <?php
  177. $Code->append(ob_get_contents());
  178. ob_end_clean();
  179. }
  180. function getClient() {
  181. require_once JPSPAN . 'CodeWriter.php';
  182. $Code = & new JPSpan_CodeWriter();
  183. $this->generate($Code);
  184. $client = $Code->toString();
  185. require_once JPSPAN . 'Include.php';
  186. $I = & JPSpan_Include::instance();
  187. // HACK - this needs to change
  188. $I->loadString(__FILE__,$client);
  189. return $I->getCode();
  190. }
  191. }
  192. //--------------------------------------------------------------------------------
  193. /**
  194. * @package JPSpan
  195. * @subpackage Generator
  196. * @access public
  197. */
  198. class JPSpan_Generator_AdHoc_Description {
  199. var $Class;
  200. /**
  201. * Map of method name to URL endpoint for method
  202. */
  203. var $methods = array();
  204. }