PageRenderTime 31ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/didactilab/gwt/phprpc/phpderpc/phprpc/derpc/WebModeClientOracle.php

http://gwtphp-derpc.googlecode.com/
PHP | 317 lines | 235 code | 48 blank | 34 comment | 57 complexity | 7cd6999c776a27c5b8654d69d485eeff MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2011 DidactiLab SAS
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. * use this file except in compliance with the License. You may obtain a copy of
  7. * the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. *
  17. * Date: 30 avr. 2011
  18. * Author: Mathieu LIGOCKI
  19. */
  20. require_once PHPRPC_ROOT . 'classes.php';
  21. require_once PHPRPC_ROOT . 'derpc/ClientOracle.php';
  22. require_once PHPRPC_ROOT . 'derpc/rpcphptools.php';
  23. require_once PHPRPC_ROOT . 'stream.php';
  24. require_once PHPRPC_ROOT . 'derpc/WebModePayloadSink.php';
  25. class ClassData {
  26. public $castableTypeData; // CastableTypeData
  27. public $fieldIdentsToNames = array(); // Map<String, String>
  28. public $fieldNamesToIdents = array(); // Map<String, String>
  29. public $methodJsniNamesToIdents = array(); // Map<String, String>
  30. public $queryId; // int
  31. public $seedName; // String
  32. public $serializableFields = array(); // List<String>
  33. public $typeName; // String
  34. }
  35. class WebModeClientOracle extends ClientOracle {
  36. protected $classData = array(); // Map<String, ClassData>
  37. protected $idents = array(); //Set<String>
  38. protected $seedNamesToClassData = array(); //Map<String, ClassData>
  39. private $operableFieldMap;
  40. protected function __construct() {
  41. $this->operableFieldMap = new ObjectMap();
  42. }
  43. public function __sleep() {
  44. return array('classData', 'idents', 'seedNamesToClassData');
  45. }
  46. public function __wakeup() {
  47. $this->operableFieldMap = new ObjectMap();
  48. }
  49. public static function load($stream) {
  50. // execution time = 32 ms
  51. //$error = error_reporting(E_ALL);
  52. //$time = microtime(true);
  53. $obj = unserialize($stream);
  54. if ($obj === false) {
  55. throw new IncompatibleRemoteServiceException('The unserialization of WebModeClientOracle has failed');
  56. }
  57. //$t2 = microtime(true);
  58. //echo '[time ' . (($t2 - $time) * 1000 ) . ']';
  59. //error_reporting($error);
  60. return $obj;
  61. }
  62. public static function jsniName(Clazz $clazz) {
  63. if ($clazz->isPrimitive()) {
  64. if ($clazz === Boolean::typeClass()) {
  65. return 'Z';
  66. }
  67. else if ($clazz === Byte::typeClass()) {
  68. return 'B';
  69. }
  70. else if ($clazz === Character::typeClass()) {
  71. return 'C';
  72. }
  73. else if ($clazz === Short::typeClass()) {
  74. return 'S';
  75. }
  76. else if ($clazz === Integer::typeClass()) {
  77. return 'I';
  78. }
  79. else if ($clazz === Long::typeClass()) {
  80. return 'J';
  81. }
  82. else if ($clazz === Float::typeClass()) {
  83. return 'F';
  84. }
  85. else if ($clazz === Double::typeClass()) {
  86. return 'D';
  87. }
  88. throw new RuntimeException('Unhandled primitive tye ' + $clazz->getName());
  89. }
  90. else if ($clazz->isArray()) {
  91. return '[' . self::jsniName($clazz->getComponentType());
  92. }
  93. else {
  94. return 'L' . str_replace('.', '/', $clazz->getFullName()) . ';';
  95. }
  96. }
  97. public function createCommandSink(OutputStream $out) {
  98. return new WebModePayloadSink($this, $out);
  99. }
  100. public function createUnusedIdent($ident) {
  101. while (in_array($ident, $this->idents, true)) {
  102. $ident .= '$';
  103. }
  104. return $ident;
  105. }
  106. public function getCastableTypeData(Clazz $clazz) {
  107. while ($clazz != null) {
  108. $name = $this->canonicalName($clazz);
  109. //echo '[for ' . $clazz->getName() . ' => ' . $name . ']';
  110. $toReturn = $this->getCastableTypeDataByName($name);
  111. if ($toReturn != null) {
  112. return $toReturn;
  113. }
  114. $clazz = $clazz->getSuperClass();
  115. }
  116. return null;
  117. }
  118. public function getFieldId(Clazz $clazz, $fieldName) {
  119. while ($clazz != null) {
  120. $className = $clazz->getFullName();
  121. $data = $this->getClassData($className);
  122. if (isset($data->fieldNamesToIdents[$fieldName])) {
  123. return $data->fieldNamesToIdents[$fieldName];
  124. }
  125. $clazz = $clazz->getSuperClass();
  126. }
  127. return null;
  128. }
  129. public function getFieldIdByClassName($className, $fieldName) {
  130. $data = $this->getClassData($className);
  131. return $data->fieldNamesToIdents[$fieldName];
  132. }
  133. public function getFieldName(Clazz $clazz, $fieldId) {
  134. while ($clazz != null) {
  135. $data = $this->getClassData($clazz->getFullName());
  136. $fieldName = $data->fieldIdentsToNames[$fieldId];
  137. if ($fieldName == null) {
  138. $clazz = $clazz->getSuperClass();
  139. }
  140. else {
  141. return (object) array('class' => $clazz, 'fieldName' => $fieldName);
  142. }
  143. }
  144. return null;
  145. }
  146. public function getMethodId(Clazz $clazz, $methodName, array $argsClass) {
  147. while ($clazz != null) {
  148. $toReturn = $this->getMethodIdByClass($clazz->getFullName(), $methodName, $argsClass);
  149. if ($toReturn != null) {
  150. return $toReturn;
  151. }
  152. $clazz = $clazz->getSuperClass();
  153. }
  154. return null;
  155. }
  156. public function getMethodIdByClassName($clazzName, $methodName, array $jsniArgTypes) {
  157. $sb = $methodName . '(';
  158. foreach ($jsniArgTypes as $jsniArg) {
  159. $sb .= $jsniArg;
  160. }
  161. $sb .= ')';
  162. ///echo "[methodId '$sb']";
  163. $data = $this->getClassData($clazzName);
  164. //echo '{' . var_export($this->classData, true) . '}';
  165. //echo '{' . var_export($data->methodJsniNamesToIdents, true) . '}';
  166. if (isset($data->methodJsniNamesToIdents[$sb])) {
  167. return $data->methodJsniNamesToIdents[$sb];
  168. }
  169. else {
  170. return null;
  171. }
  172. }
  173. public function getOperableFields(Clazz $clazz) {
  174. $toReturn = $this->operableFieldMap->get($clazz);
  175. if (!is_null($toReturn)) {
  176. return $toReturn;
  177. }
  178. $data = $this->getClassData($clazz->getFullName());
  179. $toReturn = array_new(count($data->serializableFields), null);
  180. for ($i = 0, $c = count($data->serializableFields); $i < $c; $i++) {
  181. $fieldName = $data->serializableFields[$i];
  182. $toReturn[$i] = $clazz->getField($fieldName);
  183. if (is_null($toReturn[$i])) {
  184. throw new IncompatibleRemoteServiceException('No field ' . $fieldName);
  185. }
  186. }
  187. $this->operableFieldMap->put($clazz, $toReturn);
  188. return $toReturn;
  189. }
  190. public function getQueryId(Clazz $clazz) {
  191. while (!is_null($clazz)) {
  192. $toReturn = $this->getQueryIdByClassName($this->canonicalName($clazz));
  193. if ($toReturn != 0) {
  194. return $toReturn;
  195. }
  196. $clazz = $clazz->getSuperClass();
  197. }
  198. return 0;
  199. }
  200. public function getSeedName(Clazz $clazz) {
  201. $data = $this->getClassData($clazz->getFullName());
  202. return $data->seedName;
  203. }
  204. public function getTypeName($seedName) {
  205. // TODO: Decide how to handle the no-metadata case
  206. if (mb_strpos($seedName, 'Class$') !== FALSE) {
  207. $seedName = mb_substr($seedName, 6);
  208. }
  209. if (isset($this->seedNamesToClassData[$seedName])) {
  210. $data = $this->seedNamesToClassData[$seedName];
  211. return $data->typeName;
  212. }
  213. else {
  214. return null;
  215. }
  216. }
  217. public function isScript() {
  218. return true;
  219. }
  220. private function canonicalName(Clazz $clazz) {
  221. if ($clazz->isArray()) {
  222. $leafType = $clazz;
  223. do {
  224. $leafType = $leafType->getComponentType();
  225. } while ($leafType->isArray());
  226. $enclosing = $leafType->getEnclosingClass();
  227. if (!is_null($enclosing)) {
  228. // com.foo.Enclosing$Name[]
  229. return $this->canonicalName($enclosing) . '$' . $clazz->getName();
  230. }
  231. else if ($leafType->getPackage() === '') {
  232. // Name0[
  233. return $clazz->getName();
  234. }
  235. else {
  236. // com.foo.Name[]
  237. return $leafType->getPackage() . '.' . $clazz->getName();
  238. }
  239. }
  240. else {
  241. return $clazz->getFullName();
  242. }
  243. }
  244. private function getCastableTypeDataByName($className) {
  245. $data = $this->getClassData($className);
  246. return $data->castableTypeData;
  247. }
  248. private function getQueryIdByClassName($className) {
  249. $data = $this->getClassData($className);
  250. return $data->queryId;
  251. }
  252. private function getClassData($className) {
  253. //echo '[getClassData ' . $className . ']';
  254. if (!isset($this->classData[$className])) {
  255. //echo '[not set]';
  256. $toReturn = new ClassData();
  257. $this->classData[$className] = $toReturn;
  258. return $toReturn;
  259. }
  260. return $this->classData[$className];
  261. }
  262. private function getMethodIdByClass($className, $methodName, array $args) {
  263. $jsniArgTypes = array_new(count($args), null);
  264. for ($i=0, $c=count($args); $i < $c; $i++) {
  265. $jsniArgTypes[$i] = $this->jsniName($args[$i]);
  266. }
  267. return $this->getMethodIdByClassName($className, $methodName, $jsniArgTypes);
  268. }
  269. }