PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/edem-alpha/edem/inc/Pajax.class.php

https://bitbucket.org/piratihr/edem
PHP | 172 lines | 68 code | 16 blank | 88 comment | 14 complexity | a1c1d63348c72fe56f578863dad57922 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MIT
  1. <?php
  2. /*
  3. Pajax - Remote (a)synchronous PHP objects in JavaScript.
  4. (c) Copyright 2005-2011 by Georges Auberger
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation
  7. files (the "Software"), to deal in the Software without
  8. restriction, including without limitation the rights to use,
  9. copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the
  11. Software is furnished to do so, subject to the following
  12. conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. define('CLASS_PATH_DELIMITER', ":");
  25. /*
  26. Class: Pajax
  27. Main Class for Pajax system
  28. Create JavaScript stubs and takes care of class loading of php objects
  29. */
  30. class Pajax {
  31. /*
  32. Constructor: Pajax
  33. Parameters:
  34. uriPath - URI path to the pajax system
  35. dispatcher - Name of the script used by the http callback
  36. classPath - Paths of remote class, seperated by ":"
  37. */
  38. function Pajax( $uriPath="/pajax/",
  39. $dispatcher="pajax_call_dispatcher.php",
  40. $classPath=".:../sys/classes") {
  41. $this->uriPath = $uriPath;
  42. $this->dispatcher = $dispatcher;
  43. $this->classPath = $classPath;
  44. }
  45. /*
  46. Method: loadClass
  47. Dynamically load a class file
  48. Parameters:
  49. className - Class to be loaded. It will look for a file that is
  50. sufixed by ".class.php"
  51. Returns:
  52. true - Class loaded successfully
  53. false - Failed to load class
  54. */
  55. function loadClass($className) {
  56. // Strip path chars from classname
  57. $className = str_replace(array(".", "/", "\\", ".."), "", $className);
  58. $paths = split(CLASS_PATH_DELIMITER, $this->classPath);
  59. foreach ($paths as $path) {
  60. $classPath = $path . "/" . $className . ".class.php";
  61. if (file_exists ($classPath)) {
  62. require_once($classPath);
  63. return class_exists($className);
  64. }
  65. }
  66. return false;
  67. }
  68. /*
  69. Method: isRemotable
  70. Determines if a class can be invoked remotly
  71. Parameters:
  72. className - Class name to check. It expects the class to be loaded already
  73. Returns:
  74. true - Class is a subclass of PajaxRemote
  75. false - Otherwise
  76. */
  77. function isRemotable($className) {
  78. if (class_exists($className)) {
  79. eval("\$obj = new $className();");
  80. return strtolower(get_parent_class($obj)) == strtolower("PajaxRemote");
  81. } else {
  82. return false;
  83. }
  84. }
  85. /*
  86. Method: getJavaScriptStub
  87. Return the JavaScript stub class equivalent for a php class
  88. Parameters:
  89. className - Class name to build stub for. It expects the class to be loaded already.
  90. Returns:
  91. String contain JavaScript stub for the Class. If the class is not remotable,
  92. it will return a string with an error surrounded by html comments tag.
  93. */
  94. function getJavaScriptStub($className) {
  95. if (! $this->isRemotable($className)) {
  96. return "<!-- Class '". $className . "' is not remotable -->";
  97. }
  98. $classMethods = get_class_methods($className);
  99. ob_start();
  100. ?>
  101. function <?php echo $className ?>(listener) {
  102. this.__pajax_object_id = "<?php echo md5(uniqid(rand(), true))?>" + __pajax_get_next_id();
  103. this.__pajax_listener = listener;
  104. }
  105. function <?php echo $className ?>Listener() { };
  106. <?php echo $className."Listener.prototype = new PajaxListener();" ?>
  107. <?php
  108. // Create a stub function for each method
  109. foreach ($classMethods as $methodName) {
  110. // Skip the constructors
  111. if (strtolower($methodName) != strtolower($className) && strtolower($methodName) != strtolower("PajaxRemote")) {
  112. /*
  113. In each method, copy the argument because arguments scope is
  114. lost if passed to another function. There seems to always
  115. be an argument present even if invoked with none, take that into
  116. account.
  117. Create an empty stub for call back if the class is to be used
  118. asynchronously. These methods are meant to be overriden in the
  119. client
  120. */
  121. ?>
  122. <?php echo $className."Listener.prototype.on".ucfirst($methodName)?>=function(result) {};
  123. <?php echo $className.".prototype.".$methodName?> = function() {
  124. if (arguments.length > 0 && typeof arguments[0] != 'undefined' ) {
  125. params = new Array();
  126. for (var i = 0; i < arguments.length; i++) {
  127. params[i] = arguments[i];
  128. }
  129. } else {
  130. params = null;
  131. }
  132. return new PajaxConnection("<?php echo $this->uriPath . $this->dispatcher?>").remoteCall(this.__pajax_object_id, "<?php echo $className ?>", "<?php echo $methodName ?>", params, this.__pajax_listener);
  133. }
  134. <?php
  135. }
  136. }
  137. $html = ob_get_contents();
  138. ob_end_clean();
  139. return $html;
  140. }
  141. }
  142. ?>